Repository navigation
Directives (3/4): draw a self-contained call as a glyph - #158
Conversation
Phase 3 of the directive seam. A self-contained call has no body, so until now it parsed, claimed its span, and then rendered as its own literal source — `@pagebreak` looked exactly like the text `@pagebreak`. This gives it something to draw. A directive returns a `DirectivePresentation` — an SF Symbol, replacement text, or an NSImage — and the styler collapses the source behind it. The mechanism is the one inline LaTeX already uses, not a new one: the characters stay in the storage, the first carries the image plus enough kern to occupy its width, the rest collapse to zero width via clear colour and the shrunk marker font. `MarkdownTextLayoutFragment` draws it. That is what keeps "markers shrink, they don't disappear" true here. Selection, find, copy, and undo all still see the real characters, and the caret entering the call reveals the source muted — the same flip every other construct does. Failure is visible rather than silent: `.literal`, or a symbol name the system doesn't know, leaves the source on screen instead of collapsing it to a gap the user can neither see nor fix. Rasterised glyphs are cached in an NSCache keyed by everything that determines the pixels — presentation re-runs for every visible directive on every keystroke, and rasterising text each time is the one part of this path expensive enough to matter. The parser is untouched: this is styling only, and the 4000-input corpus fingerprint is unchanged. `DirectiveScanner`'s diff is comment-only — it already emitted the geometry this needs, and its comments pointed forward to this change. DirectiveStylingTests asserted that a self-contained call renders as literal text with nothing collapsing it. That was Phase 1 stating its own limit, and it is exactly what this changes, so it becomes a collapse/reveal pair rather than being deleted. `FontDirective` and `ColorDirective` are both containers and draw no glyph, so the engine still ships no self-contained directive. Demo/ gains `@icon`, `@flag`, `@emoji`, and `@pagebreak` as embedder-side examples — curated data and print semantics are app concerns. `@flag` computes its glyph from regional-indicator scalars and carries no dataset. 463 tests pass, demo builds. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
f8b2d77 to
315111d
Compare
|
Reviewed this against current Inertness checks out. With no directives registered the parse path is untouched and the glyph pass is unreachable — the styler bails out before any of the new code on every non-directive node. Nothing here can reach an embedder who hasn't opted in. Verified rather than assumed. Three things before this lands. 1. The collapsed-source run kerns per character, not per runThe rest-of-the-call run is given a single kern equal to the whole run's width. Measured on the merge result (TextKit 2, one-variable change):
ASCII-only runs look fine because AppKit clamps a negative advance to zero. Composed character sequences don't clamp — which is exactly what made the table version visible — and a directive's arguments are arbitrary user text. And it doesn't stay on the line. Four-line document, directive on line 2, realistic base paragraph style applied:
Every individual fragment frame stays at x = 0, so nothing looks wrong on the line itself — the damage lands in a document-wide metric. The engine's own height math reads fragment frames and is immune; AppKit's own sizing reads the usage bounds. That is the same poisoning signature the table fix was chasing, just smaller in magnitude. 2. A supplied image doesn't grow its line
3. The changelog entry lands inside a released version after the mergeOn the branch it sits under Nothing else. The presentation/reveal split, the cache key, the failure-visible-rather-than-silent choice, and the deliberately flagged test change all read right, and the baseline maths for both the symbol and text glyphs check out against the drawing convention. |
…nt .image sizing The rest-of-the-call run's kern was computed for the whole run's width but applied per-character by AppKit, over-negating an n-character run by (n-1) widths — the same poisoning of usageBoundsForTextContainer the table work hit and fixed. Reuse that fix's hiddenRunKern helper. Also document that .image, unlike .symbol/.text, is drawn at its own size rather than fit to the inherited font, so an oversized image is the supplying directive's responsibility — existing tests (DirectiveGlyphTests) already rely on a directive controlling its own image size (@Swatch), so clamping or growing the line here would break an intentional feature rather than a bug. Also merges upstream/main (0.13.0 release + nodes-app#146/nodes-app#152/nodes-app#153/nodes-app#160-nodes-app#162) so the changelog entry lands under a fresh [Unreleased] section instead of inside the already-released [0.13.0]. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
|
Thanks for the deep dive — all three addressed:
Also re-ran the full suite (523 tests) and the Demo target build after the merge — both clean. |
…on branch Takes the post-review directive sources, tests, and docs from feat/directives-completion; keeps AGENTS.md and AUTHORING.md, which only this branch carries. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Phase 3 of the directive seam, on top of #120 and #155. Styling only — the parser is untouched.
The gap this closes
A self-contained call (
@pagebreak) has no body. It parses, claims its span, and then renders as its own literal source, so@pagebreaklooks exactly like the text@pagebreak. #155's README says as much:A directive now returns a
DirectivePresentation— an SF Symbol, replacement text, or anNSImage— and the styler draws it in place of the source.How it draws
The mechanism is the one inline LaTeX already uses, not a new one. The characters stay in the storage: the first carries the image plus enough kern to occupy the glyph's width, the rest collapse to zero width via clear colour and the shrunk marker font.
MarkdownTextLayoutFragmentdraws it.That is what keeps "markers shrink, they don't disappear" true here. Selection, find, copy, and undo all still see the real characters, and the caret entering the call reveals the source muted — the same flip every other construct performs.
Failure is visible rather than silent:
.literal, or a symbol name the system doesn't know, leaves the source on screen rather than collapsing it to a gap the user can neither see nor fix.Rasterised glyphs are cached in an
NSCachekeyed by everything that determines the pixels. Presentation re-runs for every visible directive on every keystroke, and rasterising text each time is the one part of this path expensive enough to matter.Scope
MarkdownDirective.swiftDirectivePresentation+ thepresentationrequirement, defaulting to.literalMarkdownASTStyler+Directives.swiftDirectiveScanner.swiftBuiltinDirectives.swiftpresentationis "not yet consulted by the styler"Autocomplete is deliberately not here — it's Phase 4, and after your note on #120 about shipping a directive that parses but doesn't render, I'd rather not repeat the shape in reverse.
One test changes meaning
DirectiveStylingTests.selfContainedStaysVisibleasserted that a self-contained call renders as literal text with nothing collapsing it. That was Phase 1 pinning its own limit, and it is precisely what this PR changes — so it becomes a collapse/reveal pair rather than being quietly deleted, and the suite is no longer named "Phase 1 styling". Flagging it explicitly since a green diff would otherwise hide a deliberate behaviour change.Reference directives
FontDirectiveandColorDirectiveare both containers and draw no glyph, so the engine still ships no self-contained directive.Demo/gains@icon,@flag,@emoji, and@pagebreakas embedder-side examples, 30–60 lines each — curated data and print semantics are app concerns, not engine primitives.@flagcomputes its glyph from the code's regional-indicator scalars and carries no dataset.Verification
Phase 4 (name and argument-value autocomplete) is ready behind this and I'll open it once this lands. #154 is still mine to take whenever you'd like it.
🤖 Generated with Claude Code