| ... | ... | @@ -1,4 +1,4 @@ |
| 1 | | import { createHighlighterCore, type HighlighterCore } from "@shikijs/core"; |
| 1 | import { createHighlighterCore, type GrammarState, type HighlighterCore } from "@shikijs/core"; |
| 2 | 2 | import { createJavaScriptRegexEngine } from "@shikijs/engine-javascript"; |
| 3 | 3 | import bashLanguage from "@shikijs/langs/bash"; |
| 4 | 4 | import javascriptLanguage from "@shikijs/langs/javascript"; |
| ... | ... | @@ -50,7 +50,12 @@ const REVIEW_HIGHLIGHTER_ENGINE_PREFERENCE = resolveReviewHighlighterEnginePrefe |
| 50 | 50 | REVIEW_HIGHLIGHTER_ENGINE_ENV_VALUE, |
| 51 | 51 | ); |
| 52 | 52 | const REVIEW_HIGHLIGHT_CHUNK_LINE_THRESHOLD = 8; |
| 53 | | const REVIEW_HIGHLIGHT_CHUNK_SIZE = 200; |
| 53 | // Bounds each tokenizing call by characters, not lines, so long-line files |
| 54 | // still yield to touches and renders between short batches. |
| 55 | const REVIEW_HIGHLIGHT_CHUNK_CHARACTERS = 2_000; |
| 56 | // A yield waits for the next frame, so batches run back to back until about one |
| 57 | // frame of work has passed instead of yielding after every batch. |
| 58 | const REVIEW_HIGHLIGHT_YIELD_AFTER_MS = 16; |
| 54 | 59 | const REVIEW_TOKENIZE_MAX_LINE_LENGTH = 1_000; |
| 55 | 60 | const REVIEW_INITIAL_LANGUAGE_MODULES = [ |
| 56 | 61 | bashLanguage, |
| ... | ... | @@ -513,46 +518,53 @@ async function highlightLines( |
| 513 | 518 | const highlighter = await getHighlighter(); |
| 514 | 519 | const sourceLines = code.split("\n"); |
| 515 | 520 | const highlightedLines: Array<ReadonlyArray<ReviewHighlightedToken>> = []; |
| 516 | | const shortLineBatch: string[] = []; |
| 517 | | |
| 518 | | const flushShortLineBatch = async (): Promise<void> => { |
| 519 | | if (shortLineBatch.length === 0) { |
| 520 | | return; |
| 521 | | } |
| 522 | | |
| 523 | | const tokenLines = highlighter.codeToTokensBase(shortLineBatch.join("\n"), { |
| 524 | | lang: language, |
| 525 | | theme, |
| 526 | | }); |
| 527 | | highlightedLines.push(...normalizeHighlightedLines(tokenLines)); |
| 528 | | shortLineBatch.length = 0; |
| 529 | | }; |
| 530 | | |
| 531 | | for (let lineIndex = 0; lineIndex < sourceLines.length; lineIndex += 1) { |
| 532 | | const line = sourceLines[lineIndex] ?? ""; |
| 533 | | |
| 534 | | if (line.length > REVIEW_TOKENIZE_MAX_LINE_LENGTH) { |
| 535 | | await flushShortLineBatch(); |
| 536 | | highlightedLines.push([{ content: line, color: null, fontStyle: null }]); |
| 521 | // Batches resume from the previous batch's grammar state, so a comment or |
| 522 | // template string that spans a batch boundary keeps its colors. |
| 523 | let grammarState: GrammarState | undefined; |
| 524 | let start = 0; |
| 525 | let sliceStartedAt = performance.now(); |
| 526 | |
| 527 | while (start < sourceLines.length) { |
| 528 | // A skipped line leaves its ending state unknown; resume from a fresh state. |
| 529 | if (sourceLines[start]!.length > REVIEW_TOKENIZE_MAX_LINE_LENGTH) { |
| 530 | highlightedLines.push([{ content: sourceLines[start]!, color: null, fontStyle: null }]); |
| 531 | grammarState = undefined; |
| 532 | start += 1; |
| 537 | 533 | } else { |
| 538 | | shortLineBatch.push(line); |
| 539 | | } |
| 534 | let end = start; |
| 535 | let characters = 0; |
| 536 | while (end < sourceLines.length) { |
| 537 | const length = sourceLines[end]!.length; |
| 538 | if ( |
| 539 | length > REVIEW_TOKENIZE_MAX_LINE_LENGTH || |
| 540 | (end > start && characters + length + 1 > REVIEW_HIGHLIGHT_CHUNK_CHARACTERS) |
| 541 | ) { |
| 542 | break; |
| 543 | } |
| 544 | characters += length + (end > start ? 1 : 0); |
| 545 | end += 1; |
| 546 | } |
| 540 | 547 | |
| 541 | | if (shortLineBatch.length >= REVIEW_HIGHLIGHT_CHUNK_SIZE) { |
| 542 | | await flushShortLineBatch(); |
| 548 | const tokenLines = highlighter.codeToTokensBase(sourceLines.slice(start, end).join("\n"), { |
| 549 | lang: language, |
| 550 | theme, |
| 551 | grammarState, |
| 552 | }); |
| 553 | grammarState = highlighter.getLastGrammarState(tokenLines); |
| 554 | highlightedLines.push(...normalizeHighlightedLines(tokenLines)); |
| 555 | start = end; |
| 543 | 556 | } |
| 544 | 557 | |
| 545 | 558 | if ( |
| 546 | 559 | sourceLines.length > REVIEW_HIGHLIGHT_CHUNK_LINE_THRESHOLD && |
| 547 | | lineIndex + 1 < sourceLines.length && |
| 548 | | (shortLineBatch.length === 0 || line.length > REVIEW_TOKENIZE_MAX_LINE_LENGTH) |
| 560 | start < sourceLines.length && |
| 561 | performance.now() - sliceStartedAt >= REVIEW_HIGHLIGHT_YIELD_AFTER_MS |
| 549 | 562 | ) { |
| 550 | 563 | await waitForNextFrame(); |
| 564 | sliceStartedAt = performance.now(); |
| 551 | 565 | } |
| 552 | 566 | } |
| 553 | 567 | |
| 554 | | await flushShortLineBatch(); |
| 555 | | |
| 556 | 568 | return highlightedLines; |
| 557 | 569 | } |
| 558 | 570 | |