Public: anyone can read this; the crew can change it.
Load highlighter grammars lazily behind an async loader (#16804 follow-up) #17216
Closed 44h 14m ago
mason/forge:mason/17107-load-highlighter-grammars-lazily-behind → main 8 commits fork 3 threads · 3 unresolved
10 files
changed +120
−34
src/web/diff-syntax.ts +12 −9
| @@ -11,8 +11,9 @@ import { blobForRender } from "../git/read.ts"; | ||
| 11 | 11 | import { |
| 12 | 12 | INITIAL_SCAN, |
| 13 | 13 | detectLanguage, |
| 14 | highlightLines, | |
| 15 | scanOneLine, | |
| 14 | highlightWith, | |
| 15 | loadScanner, | |
| 16 | scanWith, | |
| 16 | 17 | type ScanState, |
| 17 | 18 | type Token, |
| 18 | 19 | } from "./highlight.ts"; |
| @@ -41,18 +42,18 @@ export async function diffSyntax( | ||
| 41 | 42 | if (file.oldPath) { |
| 42 | 43 | const blob = await blobForRender(dir, oldRev, file.oldPath, CAP_BYTES); |
| 43 | 44 | if (blob.kind === "text") { |
| 44 | entry.old = highlightLines( | |
| 45 | entry.old = highlightWith( | |
| 46 | await loadScanner(detectLanguage(file.oldPath)), | |
| 45 | 47 | new TextDecoder().decode(blob.body), |
| 46 | detectLanguage(file.oldPath), | |
| 47 | 48 | ); |
| 48 | 49 | } |
| 49 | 50 | } |
| 50 | 51 | if (file.newPath) { |
| 51 | 52 | const blob = await blobForRender(dir, newRev, file.newPath, CAP_BYTES); |
| 52 | 53 | if (blob.kind === "text") { |
| 53 | entry.new = highlightLines( | |
| 54 | entry.new = highlightWith( | |
| 55 | await loadScanner(detectLanguage(file.newPath)), | |
| 54 | 56 | new TextDecoder().decode(blob.body), |
| 55 | detectLanguage(file.newPath), | |
| 56 | 57 | ); |
| 57 | 58 | } |
| 58 | 59 | } |
| @@ -72,13 +73,15 @@ export async function diffSyntax( | ||
| 72 | 73 | * plain state, so its first lines may colour differently from a whole-file |
| 73 | 74 | * pass. `diffSyntax` keeps that exactness for the pull page. |
| 74 | 75 | */ |
| 75 | export function hunkSyntax(diff: Diff): SyntaxMap { | |
| 76 | export async function hunkSyntax(diff: Diff): Promise<SyntaxMap> { | |
| 76 | 77 | const map: SyntaxMap = new Map(); |
| 77 | 78 | for (const file of diff.files.slice(0, MAX_DIFF_FILES)) { |
| 78 | 79 | if (file.binary || stubFor(file) !== null) continue; |
| 79 | 80 | const entry: FileTokens = {}; |
| 80 | 81 | const oldLang = file.oldPath ? detectLanguage(file.oldPath) : null; |
| 81 | 82 | const newLang = file.newPath ? detectLanguage(file.newPath) : null; |
| 83 | const oldScan = oldLang ? await loadScanner(oldLang) : null; | |
| 84 | const newScan = newLang ? await loadScanner(newLang) : null; | |
| 82 | 85 | for (const hunk of file.hunks) { |
| 83 | 86 | let oldState = INITIAL_SCAN; |
| 84 | 87 | let newState = INITIAL_SCAN; |
| @@ -93,7 +96,7 @@ export function hunkSyntax(diff: Diff): SyntaxMap { | ||
| 93 | 96 | isPlain(oldState) && |
| 94 | 97 | isPlain(newState); |
| 95 | 98 | if (line.oldNumber !== null && oldLang) { |
| 96 | const scanned = scanOneLine(line.content, oldLang, oldState); | |
| 99 | const scanned = scanWith(oldScan, line.content, oldState); | |
| 97 | 100 | oldTokens = scanned.tokens; |
| 98 | 101 | oldState = scanned.state; |
| 99 | 102 | if (shared) newTokens = oldTokens; |
| @@ -102,7 +105,7 @@ export function hunkSyntax(diff: Diff): SyntaxMap { | ||
| 102 | 105 | if (newTokens) { |
| 103 | 106 | newState = oldState; |
| 104 | 107 | } else { |
| 105 | const scanned = scanOneLine(line.content, newLang, newState); | |
| 108 | const scanned = scanWith(newScan, line.content, newState); | |
| 106 | 109 | newTokens = scanned.tokens; |
| 107 | 110 | newState = scanned.state; |
| 108 | 111 | } |
src/web/highlight.ts +11 −4
| @@ -28,9 +28,9 @@ | ||
| 28 | 28 | * |
| 29 | 29 | * This file is the door; the work is in `highlight/`, one module a concept: |
| 30 | 30 | * `tokens` (the vocabulary and the sink), `state` (what a line hands the |
| 31 | * next), `detect` (which language a file is), `scanners` (the entry points), `specs` and | |
| 32 | * `generic` (the table-driven scanner), and one module for each of Markdown, | |
| 33 | * YAML, TOML/INI, HTML, CSS, diff, Makefile and Dockerfile. | |
| 31 | * next), `detect` (which language a file is), `scanners` (the entry points), `loader` (one grammar, | |
| 32 | * loaded on demand), `specs` and `generic` (the table-driven scanner), and one module for each of | |
| 33 | * Markdown, YAML, TOML/INI, HTML, CSS, diff, Makefile and Dockerfile. | |
| 34 | 34 | */ |
| 35 | 35 | |
| 36 | 36 | export type { TokenClass, TokenKind, Token, Language } from "./highlight/tokens.ts"; |
| @@ -41,4 +41,11 @@ export { | ||
| 41 | 41 | detectLanguage, |
| 42 | 42 | languageFromFirstLine, |
| 43 | 43 | } from "./highlight/detect.ts"; |
| 44 | export { scanOneLine, highlightLines } from "./highlight/scanners.ts"; | |
| 44 | export { | |
| 45 | scanOneLine, | |
| 46 | scanWith, | |
| 47 | highlightLines, | |
| 48 | highlightWith, | |
| 49 | } from "./highlight/scanners.ts"; | |
| 50 | export { loadScanner } from "./highlight/loader.ts"; | |
| 51 | export type { Scanner } from "./highlight/state.ts"; |
src/web/highlight/generic.ts +7 −1
| @@ -3,7 +3,7 @@ import { isAlphaCh, isDigitCh, isIdentCh, isSpaceCh, isUpperCh, set } from "./ch | ||
| 3 | 3 | import { consumeStringBody, pushString, scanNumber, spaceRun } from "./lexing.ts"; |
| 4 | 4 | import { scanTagAttributes } from "./tagattrs.ts"; |
| 5 | 5 | import { type LangSpec } from "./specs.ts"; |
| 6 | import { type Heredoc, type ScanState, type Scanned } from "./state.ts"; | |
| 6 | import { type Heredoc, type ScanState, type Scanned, type Scanner } from "./state.ts"; | |
| 7 | 7 | import { Out, type TokenClass, type TokenKind } from "./tokens.ts"; |
| 8 | 8 | |
| 9 | 9 | /** Shell words after which the next word is a command again. */ |
| @@ -451,3 +451,9 @@ export function scanJsxTag(line: string, at: number, out: Out): number { | ||
| 451 | 451 | } |
| 452 | 452 | |
| 453 | 453 | // ---- Markdown ---- |
| 454 | ||
| 455 | /** The table-driven scanner bound to one language's spec. */ | |
| 456 | export const genericScanner = | |
| 457 | (spec: LangSpec): Scanner => | |
| 458 | (line, state) => | |
| 459 | scanGeneric(line, spec, state); |
src/web/highlight/loader.ts added +39
| @@ -0,0 +1,39 @@ | ||
| 1 | /** | |
| 2 | * Grammars on demand. A file of one kind needs one grammar, so each family is | |
| 3 | * its own `import()`: the module system holds the loaded grammar (nothing is | |
| 4 | * kept here), and a process that never opens a Dockerfile never parses the | |
| 5 | * Dockerfile scanner. The scan itself stays synchronous: load once, then | |
| 6 | * `scanWith` / `highlightWith` as often as needed. | |
| 7 | */ | |
| 8 | import type { Scanner } from "./state.ts"; | |
| 9 | import type { Language } from "./tokens.ts"; | |
| 10 | ||
| 11 | /** The scanner for `lang`, or null where the language is plain or unknown. */ | |
| 12 | export async function loadScanner(lang: Language): Promise<Scanner | null> { | |
| 13 | switch (lang) { | |
| 14 | case "markdown": | |
| 15 | return (await import("./markdown.ts")).scanMarkdown; | |
| 16 | case "yaml": | |
| 17 | return (await import("./yaml.ts")).scanYaml; | |
| 18 | case "toml": | |
| 19 | case "ini": | |
| 20 | return (await import("./toml.ts")).tomlScanner(lang === "toml"); | |
| 21 | case "html": | |
| 22 | return (await import("./html.ts")).scanHtml; | |
| 23 | case "css": | |
| 24 | return (await import("./css.ts")).scanCss; | |
| 25 | case "diff": | |
| 26 | return (await import("./diff.ts")).scanDiff; | |
| 27 | case "makefile": | |
| 28 | return (await import("./makefile.ts")).scanMakefile; | |
| 29 | case "dockerfile": | |
| 30 | return (await import("./dockerfile.ts")).scanDockerfile; | |
| 31 | case "plain": | |
| 32 | return null; | |
| 33 | default: { | |
| 34 | const spec = (await import("./specs.ts")).SPECS[lang]; | |
| 35 | if (!spec) return null; | |
| 36 | return (await import("./generic.ts")).genericScanner(spec); | |
| 37 | } | |
| 38 | } | |
| 39 | } |
src/web/highlight/scanners.ts +20 −12
| @@ -5,18 +5,16 @@ | ||
| 5 | 5 | import { scanCss } from "./css.ts"; |
| 6 | 6 | import { scanDiff } from "./diff.ts"; |
| 7 | 7 | import { scanDockerfile } from "./dockerfile.ts"; |
| 8 | import { scanGeneric } from "./generic.ts"; | |
| 8 | import { genericScanner } from "./generic.ts"; | |
| 9 | 9 | import { scanHtml } from "./html.ts"; |
| 10 | 10 | import { scanMakefile } from "./makefile.ts"; |
| 11 | 11 | import { scanMarkdown } from "./markdown.ts"; |
| 12 | 12 | import { SPECS } from "./specs.ts"; |
| 13 | import { INITIAL_SCAN, type ScanState, type Scanned } from "./state.ts"; | |
| 14 | import { scanToml } from "./toml.ts"; | |
| 13 | import { INITIAL_SCAN, type ScanState, type Scanned, type Scanner } from "./state.ts"; | |
| 14 | import { tomlScanner } from "./toml.ts"; | |
| 15 | 15 | import { tok, type Language, type Token } from "./tokens.ts"; |
| 16 | 16 | import { scanYaml } from "./yaml.ts"; |
| 17 | 17 | |
| 18 | type Scanner = (line: string, state: ScanState) => Scanned; | |
| 19 | ||
| 20 | 18 | function scannerFor(lang: Language): Scanner | null { |
| 21 | 19 | switch (lang) { |
| 22 | 20 | case "markdown": |
| @@ -24,10 +22,8 @@ function scannerFor(lang: Language): Scanner | null { | ||
| 24 | 22 | case "yaml": |
| 25 | 23 | return scanYaml; |
| 26 | 24 | case "toml": |
| 27 | case "ini": { | |
| 28 | const toml = lang === "toml"; | |
| 29 | return (line, state) => scanToml(line, state, toml); | |
| 30 | } | |
| 25 | case "ini": | |
| 26 | return tomlScanner(lang === "toml"); | |
| 31 | 27 | case "html": |
| 32 | 28 | return scanHtml; |
| 33 | 29 | case "css": |
| @@ -43,7 +39,7 @@ function scannerFor(lang: Language): Scanner | null { | ||
| 43 | 39 | default: { |
| 44 | 40 | const spec = SPECS[lang]; |
| 45 | 41 | if (!spec) return null; |
| 46 | return (line, state) => scanGeneric(line, spec, state); | |
| 42 | return genericScanner(spec); | |
| 47 | 43 | } |
| 48 | 44 | } |
| 49 | 45 | } |
| @@ -65,14 +61,26 @@ export function scanOneLine( | ||
| 65 | 61 | lang: Language, |
| 66 | 62 | state: ScanState, |
| 67 | 63 | ): Scanned { |
| 68 | const scan = scannerFor(lang); | |
| 64 | return scanWith(scannerFor(lang), line, state); | |
| 65 | } | |
| 66 | ||
| 67 | /** `scanOneLine` for a scanner already in hand (`loadScanner`); null is plain. */ | |
| 68 | export function scanWith( | |
| 69 | scan: Scanner | null, | |
| 70 | line: string, | |
| 71 | state: ScanState, | |
| 72 | ): Scanned { | |
| 69 | 73 | if (!scan) return { tokens: [tok("plain", line)], state }; |
| 70 | 74 | return scan(line, state); |
| 71 | 75 | } |
| 72 | 76 | |
| 73 | 77 | export function highlightLines(code: string, lang: Language): Token[][] { |
| 78 | return highlightWith(scannerFor(lang), code); | |
| 79 | } | |
| 80 | ||
| 81 | /** `highlightLines` for a scanner already in hand (`loadScanner`); null is plain. */ | |
| 82 | export function highlightWith(scan: Scanner | null, code: string): Token[][] { | |
| 74 | 83 | const lines = code.split("\n"); |
| 75 | const scan = scannerFor(lang); | |
| 76 | 84 | if (!scan) return lines.map((line) => [tok("plain", line)]); |
| 77 | 85 | |
| 78 | 86 | const out: Token[][] = []; |
src/web/highlight/state.ts +3
src/web/highlight/toml.ts +7 −1
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | /** TOML and INI scanner. */ |
| 2 | 2 | import { consumeStringBody } from "./lexing.ts"; |
| 3 | import { type ScanState, type Scanned } from "./state.ts"; | |
| 3 | import { type ScanState, type Scanned, type Scanner } from "./state.ts"; | |
| 4 | 4 | import { Out } from "./tokens.ts"; |
| 5 | 5 | import { scanConfigValue } from "./value.ts"; |
| 6 | 6 | |
| @@ -67,3 +67,9 @@ export function scanTomlRest( | ||
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | // ---- HTML ---- |
| 70 | ||
| 71 | /** The TOML scanner, or the INI one: the grammars differ in one flag. */ | |
| 72 | export const tomlScanner = | |
| 73 | (toml: boolean): Scanner => | |
| 74 | (line, state) => | |
| 75 | scanToml(line, state, toml); |
src/web/pages/commits.ts +1 −1
| @@ -341,7 +341,7 @@ export async function commitPage(ctx: Ctx): Promise<Response> { | ||
| 341 | 341 | ? diffResult.value |
| 342 | 342 | : { files: [], additions: 0, deletions: 0 }; |
| 343 | 343 | const highlighting = performance.now(); |
| 344 | const syntax = hunkSyntax(diff); | |
| 344 | const syntax = await hunkSyntax(diff); | |
| 345 | 345 | notePhase(ctx.request, "highlight", performance.now() - highlighting); |
| 346 | 346 | |
| 347 | 347 | const checked = await checking; |
test/diff-syntax-hunks.test.ts +6 −6
| @@ -23,23 +23,23 @@ function patch(path: string, oldLines: string[], newLines: string[]): string { | ||
| 23 | 23 | ].join("\n"); |
| 24 | 24 | } |
| 25 | 25 | |
| 26 | test("a hunk's tokens equal the whole-file tokens at its line numbers", () => { | |
| 26 | test("a hunk's tokens equal the whole-file tokens at its line numbers", async () => { | |
| 27 | 27 | const diff = parseUnifiedDiff(patch("a.ts", OLD, NEW)); |
| 28 | const got = hunkSyntax(diff).get("a.ts")!; | |
| 28 | const got = (await hunkSyntax(diff)).get("a.ts")!; | |
| 29 | 29 | const lang = detectLanguage("a.ts"); |
| 30 | 30 | expect(got.old).toEqual(highlightLines(OLD.join("\n"), lang)); |
| 31 | 31 | expect(got.new).toEqual(highlightLines(NEW.join("\n"), lang)); |
| 32 | 32 | }); |
| 33 | 33 | |
| 34 | test("only the hunk's lines are tokenised", () => { | |
| 34 | test("only the hunk's lines are tokenised", async () => { | |
| 35 | 35 | const text = patch("a.ts", OLD, NEW).replace("@@ -1,4 +1,4 @@", "@@ -101,4 +101,4 @@"); |
| 36 | const got = hunkSyntax(parseUnifiedDiff(text)).get("a.ts")!; | |
| 36 | const got = (await hunkSyntax(parseUnifiedDiff(text))).get("a.ts")!; | |
| 37 | 37 | expect(got.new![0]).toBeUndefined(); |
| 38 | 38 | expect(got.new![100]).toBeDefined(); |
| 39 | 39 | expect(got.new![103]).toBeDefined(); |
| 40 | 40 | }); |
| 41 | 41 | |
| 42 | test("20 files of 800 lines: only the 7-line hunks are tokenised", () => { | |
| 42 | test("20 files of 800 lines: only the 7-line hunks are tokenised", async () => { | |
| 43 | 43 | const body = Array.from({ length: 800 }, (_, i) => `const v${i} = foo("s${i}", ${i}); // c`); |
| 44 | 44 | const parts: string[] = []; |
| 45 | 45 | for (let f = 0; f < 20; f++) { |
| @@ -62,7 +62,7 @@ test("20 files of 800 lines: only the 7-line hunks are tokenised", () => { | ||
| 62 | 62 | } |
| 63 | 63 | const diff = parseUnifiedDiff(parts.join("\n")); |
| 64 | 64 | expect(diff.files.length).toBe(20); |
| 65 | const map = hunkSyntax(diff); | |
| 65 | const map = await hunkSyntax(diff); | |
| 66 | 66 | expect(map.size).toBe(20); |
| 67 | 67 | // Work done, not wall time: one token row per drawn line per side. Each |
| 68 | 68 | // hunk draws 7 old and 7 new lines (6 shared context + 1 changed each), |
test/highlight-entry.test.ts +14
| @@ -27,3 +27,17 @@ test("every language scans losslessly through its scanner", () => { | ||
| 27 | 27 | expect(one.tokens.map((t) => t.text).join("")).toBe("x = 1"); |
| 28 | 28 | } |
| 29 | 29 | }); |
| 30 | ||
| 31 | test("a loaded scanner tokenizes exactly as the language-named entry points", async () => { | |
| 32 | const sample = "a = 1 # x\n\"s\" <b> {k: v}\n/* c\n"; | |
| 33 | for (const lang of highlight.LANGUAGES) { | |
| 34 | const scan = await highlight.loadScanner(lang); | |
| 35 | expect(highlight.highlightWith(scan, sample)).toEqual( | |
| 36 | highlight.highlightLines(sample, lang), | |
| 37 | ); | |
| 38 | expect(highlight.scanWith(scan, "x = 1", highlight.INITIAL_SCAN)).toEqual( | |
| 39 | highlight.scanOneLine("x = 1", lang, highlight.INITIAL_SCAN), | |
| 40 | ); | |
| 41 | } | |
| 42 | expect(await highlight.loadScanner("plain")).toBeNull(); | |
| 43 | }); |
new above
offline
genericScannersits under the stray// ---- Markdown ----marker, which is the wrong section. Put it next toscanGeneric. The same applies totomlScannerin toml.ts under// ---- HTML ----.