fix(editor-state): make block-highlight parsing linear by aazminu · Pull Request #1106 · 8f4e/8f4e · GitHub
Skip to content

fix(editor-state): make block-highlight parsing linear - #1106

Open
aazminu wants to merge 1 commit into
8f4e:mainfrom
aazminu:fix/linear-block-highlight-parsing-1011
Open

aazminu wants to merge 1 commit into
8f4e:mainfrom
aazminu:fix/linear-block-highlight-parsing-1011

Conversation

@aazminu

@aazminu aazminu commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

Closes #1011

Summary

Replaces per-line full-history reduce scans and .slice().reverse().find(...) searches in parseCodeBlocks (packages/editor/packages/editor-core/packages/editor-state/src/features/code-blocks/features/blockHighlights/codeParser.ts) with incrementally maintained per-parse lookup state:

  • Tracks openBlockCount directly (+1 on block open, -1 on closing match, 0 net change on else transition) instead of reducing over all previously discovered blocks on every source line.
  • Maintains a Map<string, CodeBlock[]> of open-block stacks keyed by endInstruction (openBlocksByEndInstruction) and a separate openIfBlocks stack for open if blocks so closing instructions and else locate their nearest open target in $O(1)$ amortized time without copying or reversing blocks.
  • Preserves tolerant highlight parsing for incomplete blocks, repeated else instructions, inner if ... ifEnd blocks nested inside an outer if ... else ... ifEnd, stray closers, and mismatched nesting (if ... loop ... ifEnd ... loopEnd).
  • Adds unit tests in codeParser.test.ts covering inner if blocks closed before an outer else, repeated else instructions, and closing instructions matched behind differently typed open blocks.

Benchmark Results

Measured parseCodeBlocks on blocks containing module + $N$ sibling if / else / loop / loopEnd / ifEnd constructs (median of 11 alternating samples after 4 warmups on Node v24.16.0):

Sibling constructs (lines) Previous (reduce + slice().reverse().find()) Incremental stacks
500 (3,502) 2.811 ms 0.200 ms
1,000 (7,002) 11.157 ms 0.367 ms
2,500 (17,502) 65.275 ms 0.909 ms

Verification

  • biome check on codeParser.ts and codeParser.test.ts
  • tsc --noEmit on @8f4e/editor-state
  • vitest run on codeParser.test.ts and updateGraphicData.test.ts (15 passed)

@irimias57 irimias57 added contributor editor Editor, web UI, VS Code integration, and interactive experience work labels Oct 19, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor editor Editor, web UI, VS Code integration, and interactive experience work

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make editor block-highlight parsing linear

2 participants