Skip to content

Directives (3/4): draw a self-contained call as a glyph - #158

Merged
luca-chen198 merged 3 commits into
nodes-app:mainfrom
wildthink:feat/directives-glyphs
Sep 29, 2026
Merged

luca-chen198 merged 3 commits into
nodes-app:mainfrom
wildthink:feat/directives-glyphs

Conversation

@wildthink

Copy link
Copy Markdown
Contributor

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 @pagebreak looks exactly like the text @pagebreak. #155's README says as much:

Self-contained calls parse and claim their span … but they currently render as their literal source, and no self-contained directive ships yet.

A directive now returns a DirectivePresentation — an SF Symbol, replacement text, or an NSImage — 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. 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 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 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.

Scope

file change
MarkdownDirective.swift DirectivePresentation + the presentation requirement, defaulting to .literal
MarkdownASTStyler+Directives.swift the glyph pass
DirectiveScanner.swift comment-only — it already emitted the geometry this needs
BuiltinDirectives.swift header only; its text still said presentation is "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.selfContainedStaysVisible asserted 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

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, 30–60 lines each — curated data and print semantics are app concerns, not engine primitives. @flag computes 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

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>
@wildthink
wildthink force-pushed the feat/directives-glyphs branch from f8b2d77 to 315111d Compare August 23, 2026 12:26
@luca-chen198

Copy link
Copy Markdown
Member

Reviewed this against current main rather than against the branch base — the branch sits 4 commits behind it (the 0.13.0 release plus three merges). It merges clean, and I re-ran the suite on the merge result: 520 green. So the "463 tests pass" claim holds on the newer base too.

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 run

The rest-of-the-call run is given a single kern equal to the whole run's width. .kern applies to every character in the range, so an n-character run ends up (n−1) widths too negative. This is the same bug the table work hit and fixed — there is already a helper in the styler for exactly this, added with that fix, with the reasoning written up in its doc comment.

Measured on the merge result (TextKit 2, one-variable change):

document as-is with the per-character helper
@pagebreak tail 32.996pt 33.017pt
a @pagebreak("z̄") b 23.45pt 33.65pt

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:

line-2 argument usageBoundsForTextContainer line fragment frames
none (plain ASCII call) x = 0.0, w = 30.7 all x = 0.0
5 × z̄ x = −7.2, w = 37.9 all x = 0.0
30 × z̄ x = −93.3, w = 124.0 all x = 0.0
30 × z̄, directives unregistered x = 0.0, w = 326.6 all x = 0.0

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

.symbol and .text are sized to the inherited font and fit. .image takes whatever the directive hands over — a 60pt image measures into a 17pt line and overdraws its neighbours. The LaTeX path grows the line to fit its image; this one doesn't. Either clamp it, raise the line the way the block path does, or say plainly in the doc comment that sizing is the directive's problem.

3. The changelog entry lands inside a released version after the merge

On the branch it sits under [Unreleased], which was correct when the branch was cut. main has since renamed that heading to [0.13.0] - 2026-09-20, and the three-way merge drops the entry cleanly inside it — verified on a trial merge, where [Unreleased] ends up empty. Nothing that is visible from the branch, so flagging it to be moved at merge time.

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.

wildthink-pub and others added 2 commits September 24, 2026 10:55
…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>
@wildthink

Copy link
Copy Markdown
Contributor Author

Thanks for the deep dive — all three addressed:

  1. Kern-per-run bug: fixed. The rest-run now goes through MarkdownStyler.hiddenRunKern, the same per-character helper the table fix added — I'd missed reusing it here.
  2. .image sizing: this branch's own tests (DirectiveGlyphTests, e.g. @swatch(size: 32)) deliberately assert a directive controls its own image size, so clamping or growing the line would break an intentional feature rather than fix a bug. Went with your third option instead — DirectivePresentation.image's doc comment now says plainly that, unlike .symbol/.text, it draws at its own size and an oversized image is the supplying directive's responsibility.
  3. Changelog placement: merged current main in and moved the glyph-presentation entry into a fresh [Unreleased] section rather than letting it land inside [0.13.0].

Also re-ran the full suite (523 tests) and the Demo target build after the merge — both clean.

wildthink pushed a commit to wildthink/swift-markdown-engine that referenced this pull request Sep 24, 2026
…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>

@luca-chen198 luca-chen198 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@luca-chen198
luca-chen198 merged commit 120bb49 into nodes-app:main Sep 29, 2026
1 check passed
@wildthink
wildthink deleted the feat/directives-glyphs branch October 7, 2026 20:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants