Public: anyone can read this; the crew can change it.

src/tui/screens/workspace.ts and src/web/highlight.ts split by concept, highlighter grammars loaded only for the file kind opened #16784

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

This pull splits src/web/highlight.ts (2,508 lines) by concept and loads each grammar only when a file of that kind is scanned. I did not touch src/tui/screens/workspace.ts. The issue's first step is two pulls, and this is the highlight half.

Notes

opened this

This pull splits src/web/highlight.ts (2,508 lines) by concept and loads each grammar only when a file of that kind is scanned. I did not touch src/tui/screens/workspace.ts. The issue's first step is two pulls, and this is the highlight half.

What moved where

  • src/web/highlight.ts is now a 42-line door. It keeps the header comment and re-exports the same public names, so no importer changed.
  • The work is in src/web/highlight/, one module per concept:
    • tokens: the token types and the Out sink.
    • chars: the character-class helpers.
    • state: ScanState and INITIAL_SCAN.
    • specs: the language rule tables.
    • detect: the extension, filename, shebang and modeline logic.
    • lexing: the shared string, number and indent helpers.
    • generic: the table-driven scanner.
    • markdown, yaml, toml (TOML and INI), html, css, diff, makefile and dockerfile: one scanner each.
    • scanners: scanOneLine, highlightLines, and the grammar loader.
  • The code was moved verbatim; the only edits are export keywords and imports.

Lazy loading

  • scanners.ts loads a grammar with require the first time a file of its kind is scanned, then caches it. Scanning stays synchronous, which the viewer, the diff view and the editor's per-keystroke re-scan depend on.
  • test/highlight-lazy.test.ts runs a fresh process and checks that scanning TypeScript loads neither the Markdown, CSS, HTML nor YAML scanner.
  • That test and the Markdown and plain-text cases in it are new; I did not run it against the old code to confirm it fails there.
  • The browser bundle still contains every grammar. The code is only fetched once, so the lazy loading helps the server, not the download size.

Checks

  • bun x tsc --noEmit is clean.
  • The existing highlight, code, diff-syntax, editor and web tests pass unchanged: 194 pass, 4 skip (the Markdown table look, no chromium here), 0 fail. web-editor.test.ts builds the client bundle and compares its output with the server's.
  • I ran only the tests that cover this change, not the full suite. The forge runs that on push.

Left out

  • The gate's similarity notes (scanJsxTag against scanHtmlAttributes, scanTomlValue against scanYamlValue) are about code that already existed. Folding them together would change behaviour, so I left them as they are.
  • I did not split specs.ts per language, because it is one table-shaped concept.
  • The commit lines say No door and No look, because no page, route or tool changed and nothing a reader sees changes.

To try it Open any .ts, .md or .css file in a repository's code view; it highlights as before. Run bun test test/highlight-lazy.test.ts test/code.test.ts.

🤖 Generated with Claude Code

Closes #15369

3 earlier comments
commented

This pull splits src/web/highlight.ts (2,508 lines) by concept and loads each grammar only when a file of that kind is scanned. I did not touch src/tui/screens/workspace.ts. The issue's first step is two pulls, and this is the highlight half.

What moved where

  • src/web/highlight.ts is now a 42-line door. It keeps the header comment and re-exports the same public names, so no importer changed.
  • The work is in src/web/highlight/, one module per concept:
    • tokens: the token types and the Out sink.
    • chars: the character-class helpers.
    • state: ScanState and INITIAL_SCAN.
    • specs: the language rule tables.
    • detect: the extension, filename, shebang and modeline logic.
    • lexing: the shared string, number and indent helpers.
    • generic: the table-driven scanner.
    • markdown, yaml, toml (TOML and INI), html, css, diff, makefile and dockerfile: one scanner each.
    • scanners: scanOneLine, highlightLines, and the grammar loader.
  • The code was moved verbatim; the only edits are export keywords and imports.

Lazy loading

  • scanners.ts loads a grammar with require the first time a file of its kind is scanned, then caches it. Scanning stays synchronous, which the viewer, the diff view and the editor's per-keystroke re-scan depend on.
  • test/highlight-lazy.test.ts runs a fresh process and checks that scanning TypeScript loads neither the Markdown, CSS, HTML nor YAML scanner.
  • That test and the Markdown and plain-text cases in it are new; I did not run it against the old code to confirm it fails there.
  • The browser bundle still contains every grammar. The code is only fetched once, so the lazy loading helps the server, not the download size.

Checks

  • bun x tsc --noEmit is clean.
  • The existing highlight, code, diff-syntax, editor and web tests pass unchanged: 194 pass, 4 skip (the Markdown table look, no chromium here), 0 fail. web-editor.test.ts builds the client bundle and compares its output with the server's.
  • I ran only the tests that cover this change, not the full suite. The forge runs that on push.

Left out

  • The gate's similarity notes (scanJsxTag against scanHtmlAttributes, scanTomlValue against scanYamlValue) are about code that already existed. Folding them together would change behaviour, so I left them as they are.
  • I did not split specs.ts per language, because it is one table-shaped concept.
  • The commit lines say No door and No look, because no page, route or tool changed and nothing a reader sees changes.

To try it Open any .ts, .md or .css file in a repository's code view; it highlights as before. Run bun test test/highlight-lazy.test.ts test/code.test.ts.

🤖 Generated with Claude Code

commented

This push fixes the two failures that came from this pull. It drops the lazy grammar loading I added when I split src/web/highlight.ts, and the scanners are imported statically again.

  • Cause: src/web/highlight/scanners.ts loaded each grammar with require() on first use and kept the loaded scanners in a module-level Map. test/launch-no-tla.test.ts forbids require() of another module in src/, and test/module-state.test.ts forbids that kind of cache. Those are the two failures.
  • Change: scanners.ts now imports every scanner statically and picks one with a switch. There is no cache and no require(). Highlighting output is unchanged.
  • Removed test: I deleted test/highlight-lazy.test.ts. It only checked that grammars load lazily, which no longer applies.
  • Cost: a process now loads all grammar modules when it first imports the highlighter, instead of only the ones it scans.
  • Not mine: /llms.txt stays under its byte budget is also red on main (518ce94), so I left it alone.

Checks run locally: bun x tsc --noEmit is clean. The highlight tests, test/launch-no-tla.test.ts and test/module-state.test.ts all pass. scripts/gate.ts --fix only printed advisory "one implementation?" similarity notes between the scanJsxTag/scanHtmlAttributes and scanTomlValue/scanYamlValue pairs, so I left them.

Try it: open a .md file and a .ts file in a repository's code view. Both should be highlighted as before.

commented

The gate failed because the pull changes src/web/highlight* without adding or editing a test. This is a pure code move, so I pushed an empty commit with a No test: line instead of a new test. The line says behaviour is unchanged and the existing highlight snapshot tests cover every moved scanner.

bun scripts/gate.ts passed locally with that commit, apart from its "one implementation?" similarity notes. Those were already in the log and the gate treats them as advisory. I haven't seen the forge's verdict on this push yet.

What the pull does: it splits src/web/highlight.ts into one module per concept under src/web/highlight/ (scanners, tokens, state, and one file per grammar). Scanners are imported statically: the lazy require() loading and its cache were removed, and test/highlight-lazy.test.ts went with them. Behaviour is unchanged.

What I left alone:

  • Scanner similarity notes: I did not merge scanJsxTag with scanHtmlAttributes, or scanTomlValue with scanYamlValue, because that would change behaviour.
  • /llms.txt budget test: it is red on main as well.

Try it: open a .md file and a .ts file in a repository's code view. Both should be highlighted as before.

commented

This pull is now only the split of src/web/highlight.ts by concept, and its title and the issue should be retitled to say so. I can't edit them from here. Lazy grammar loading and the src/tui/screens/workspace.ts split are not in it.

What changed

  • src/web/highlight.ts stays as the door. The work is in src/web/highlight/, one module per concept: tokens, state, detect, scanners, specs, generic, and one per grammar (Markdown, YAML, TOML/INI, HTML, CSS, diff, Makefile, Dockerfile).
  • scanners.ts imports every scanner statically and picks one with a switch.
  • Behaviour is unchanged. The existing highlight tests pass unedited.
  • Comments in highlight.ts and scanners.ts now say only what the modules do.

Why the lazy loading is out

  • Scans must stay synchronous because the viewer, the diff and the editor's per-keystroke re-scan depend on that.
  • A synchronous lazy load needs require(), which test/launch-no-tla.test.ts forbids, or a module-level cache, which test/module-state.test.ts forbids.
  • I chose not to turn scans async in this pull. That reshapes every caller and is not a pure move.
  • A process now loads all grammar modules on first import of the highlighter.

Tests

  • I deleted test/highlight-lazy.test.ts because it only checked the lazy loading.
  • The new test/highlight-entry.test.ts checks that src/web/highlight.ts keeps its public names. For every Language, it also checks that highlightLines and scanOneLine return tokens whose text joins back to the input. A missing case in the scanner switch would fail it.
  • tsc and the highlight, launch-no-tla and module-state tests pass locally.

Follow-up issues

  • #16802: merge scanJsxTag and scanHtmlAttributes.
  • #16803: merge scanTomlValue and scanYamlValue.
  • #16804: lazy grammar loading with a design both guard tests accept (for example an async boundary with dynamic import()), plus the workspace.ts split.

/llms.txt byte budget test is red on main as well, so I left it alone.

Try it: open a .md file and a .ts file in a repository's code view. Both should be highlighted as before.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs