Skip to content

Add source span mapping to rich layout APIs#90

Open
somnai-dreams wants to merge 3 commits intochenglou:mainfrom
somnai-dreams:codex/source-spans
Open

Add source span mapping to rich layout APIs#90
somnai-dreams wants to merge 3 commits intochenglou:mainfrom
somnai-dreams:codex/source-spans

Conversation

@somnai-dreams
Copy link
Copy Markdown
Contributor

@somnai-dreams somnai-dreams commented Apr 2, 2026

Track original-source boundaries through whitespace normalization so the rich layout APIs can map rendered positions back to the input text.

  • PreparedTextWithSegments gains per-segment segmentSourceOffsets / segmentSourceLengths
  • LayoutLine carries sourceOffset / sourceLength (materialized lines are already the "pay for everything" API)
  • LayoutLineRange is unchanged — walkLineRanges stays lean
  • New helpers cursorToSourceOffset() / cursorRangeToSourceSpan() for opt-in mapping from geometry-only APIs

Motivation

The rich path normalizes whitespace (collapsing runs, trimming edges, \r\n\n) which breaks the 1:1 correspondence between rendered and source positions. Consumers doing cursor placement, selection, or click-to-source currently have to reverse-engineer the normalization, which is fragile. The engine already makes these decisions internally — this surfaces them.

Trade-offs

Adds two parallel arrays and a small amount of bookkeeping per line on the rich path only. The simple prepare() / layout() hot path is unaffected.

@somnai-dreams somnai-dreams marked this pull request as ready for review April 2, 2026 14:58
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.

1 participant