TypeScript
fix(mobile): keep highlighting review diffs after a long line (#9911)
Julius Marminge50027bb3ad29
diff --git a/apps/mobile/src/features/diffs/nativeReviewDiffHighlighter.test.ts b/apps/mobile/src/features/diffs/nativeReviewDiffHighlighter.test.tsindex 00679afa4a6..e7e75a4faa1 100644--- a/apps/mobile/src/features/diffs/nativeReviewDiffHighlighter.test.ts+++ b/apps/mobile/src/features/diffs/nativeReviewDiffHighlighter.test.ts@@ -257,13 +257,13 @@ describe.each(["native", "javascript"] as const)("%s highlighting budgets", (engexpect(result.tokensByRowId["line-1"]?.some((token) => token.color !== null)).toBe(true);});- it("keeps long lines and unknown following syntax plain until the next hunk", async () => {+ it("keeps only the long line plain and resumes highlighting after it", async () => {const longLine = `${"x".repeat(1_001)} /*`;const rows = [line(1, "export const before = 1;"),line(2, longLine),{ kind: "comment", id: "note", commentText: "Check this", fileId: TYPESCRIPT_FILE.id },- line(3, "inside the comment */"),+ line(3, "export const inside = 'x';"),makeHunk("next-hunk"),line(100, "export const after = 2;"),] satisfies ReadonlyArray<NativeReviewDiffRow>;@@ -274,14 +274,36 @@ describe.each(["native", "javascript"] as const)("%s highlighting budgets", (engexpect(result.tokensByRowId["line-2"]).toEqual([{ content: longLine, color: null, fontStyle: null },]);- expect(result.tokensByRowId["line-3"]).toEqual([- { content: "inside the comment */", color: null, fontStyle: null },- ]);- expect(result.tokensByRowId["line-1"]?.some((token) => token.color !== null)).toBe(true);- expect(result.tokensByRowId["line-100"]?.some((token) => token.color !== null)).toBe(true);+ for (const id of ["line-1", "line-3", "line-100"]) {+ expect(result.tokensByRowId[id]?.some((token) => token.color !== null)).toBe(true);+ }expect(tokenization.calls.some((code) => code.includes(longLine))).toBe(false);});+ it("highlights the rows after a long line the same regardless of the first window", async () => {+ const rows = [+ line(1, "export const before = 1;"),+ line(2, `const data = "${"x".repeat(1_050)}";`),+ line(3, "export const inside = 'x';"),+ line(4, "export const after = 2;"),+ ];+ const spanning = await highlightRows(rows);+ const afterLongLine = await highlightNativeReviewDiffVisibleRows({+ rows,+ files: [TYPESCRIPT_FILE],+ scheme: "dark",+ engine,+ firstRowIndex: 2,+ lastRowIndex: 3,+ overscanRows: 0,+ });++ for (const id of ["line-3", "line-4"]) {+ expect(spanning.tokensByRowId[id]?.some((token) => token.color !== null)).toBe(true);+ expect(afterLongLine.tokensByRowId[id]).toEqual(spanning.tokensByRowId[id]);+ }+ });+it("preserves multiline grammar and row mapping across character-limited batches", async () => {const opening = line(1, "const message = `open");const body = Array.from({ length: 40 }, (_, index) => line(index + 2, "inside ".repeat(45)));diff --git a/apps/mobile/src/features/diffs/nativeReviewDiffHighlighter.ts b/apps/mobile/src/features/diffs/nativeReviewDiffHighlighter.tsindex 383e1e73a85..0ea8c100dc1 100644--- a/apps/mobile/src/features/diffs/nativeReviewDiffHighlighter.ts+++ b/apps/mobile/src/features/diffs/nativeReviewDiffHighlighter.ts@@ -247,15 +247,16 @@ function createHighlighterHandle(while (start < lines.length) {if (signal?.aborted) return [];- // Skipping this line leaves its ending grammar state unknown. Keep the- // rest of this contiguous segment plain instead of guessing its syntax.+ // Skipping this line leaves its ending grammar state unknown. Resume+ // from a fresh state rather than leaving the rest of the segment plain:+ // highlighted rows are cached for the sheet's lifetime, so a plain tail+ // would stick, and which rows it covered would depend on where the+ // first visible window happened to start.if (lines[start]!.length > NATIVE_REVIEW_DIFF_TOKENIZE_MAX_LINE_LENGTH) {- highlighted.push(- ...lines- .slice(start)- .map((content) => [{ content: content || " ", color: null, fontStyle: null }]),- );- break;+ highlighted.push([{ content: lines[start] || " ", color: null, fontStyle: null }]);+ grammarState = undefined;+ start += 1;+ continue;}let end = start;