Skip to content

Commit dc3a09e

Browse files
Isolate native diff highlight grammar state
- Split highlight tokenization at hunk, cache, and add/delete boundaries - Add regression coverage for native review diff highlighting
1 parent ecb35f7 commit dc3a09e

2 files changed

Lines changed: 206 additions & 8 deletions

File tree

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import type { NativeReviewDiffRow } from "./nativeReviewDiffSurface";
4+
import type { NativeReviewDiffFile } from "./nativeReviewDiffTypes";
5+
import { highlightNativeReviewDiffVisibleRows } from "./nativeReviewDiffHighlighter";
6+
7+
const TYPESCRIPT_FILE: NativeReviewDiffFile = {
8+
id: "file-1",
9+
path: "example.ts",
10+
language: "typescript",
11+
additions: 0,
12+
deletions: 0,
13+
};
14+
15+
function makeLine(
16+
input: Pick<NativeReviewDiffRow, "id" | "content" | "change" | "oldLineNumber" | "newLineNumber">,
17+
): NativeReviewDiffRow {
18+
return {
19+
kind: "line",
20+
fileId: TYPESCRIPT_FILE.id,
21+
...input,
22+
};
23+
}
24+
25+
function makeHunk(id: string): NativeReviewDiffRow {
26+
return {
27+
kind: "hunk",
28+
id,
29+
fileId: TYPESCRIPT_FILE.id,
30+
text: "@@",
31+
};
32+
}
33+
34+
function highlight(
35+
rows: ReadonlyArray<NativeReviewDiffRow>,
36+
alreadyHighlightedRowIds?: ReadonlySet<string>,
37+
) {
38+
return highlightNativeReviewDiffVisibleRows({
39+
rows,
40+
files: [TYPESCRIPT_FILE],
41+
scheme: "dark",
42+
engine: "javascript",
43+
firstRowIndex: 0,
44+
lastRowIndex: rows.length - 1,
45+
overscanRows: 0,
46+
maxRows: 100,
47+
alreadyHighlightedRowIds,
48+
});
49+
}
50+
51+
describe("highlightNativeReviewDiffVisibleRows", () => {
52+
it("does not carry grammar state across hunk boundaries", async () => {
53+
const exportRow = makeLine({
54+
id: "export-row",
55+
content: "export async function run() {}",
56+
change: "add",
57+
oldLineNumber: null,
58+
newLineNumber: 100,
59+
});
60+
const rows = [
61+
makeHunk("hunk-1"),
62+
makeLine({
63+
id: "import-open",
64+
content: "import {",
65+
change: "context",
66+
oldLineNumber: 1,
67+
newLineNumber: 1,
68+
}),
69+
makeLine({
70+
id: "import-entry",
71+
content: " Model,",
72+
change: "context",
73+
oldLineNumber: 2,
74+
newLineNumber: 2,
75+
}),
76+
makeHunk("hunk-2"),
77+
exportRow,
78+
];
79+
80+
const [highlighted, standalone] = await Promise.all([
81+
highlight(rows),
82+
highlight([makeHunk("standalone-hunk"), exportRow]),
83+
]);
84+
85+
expect(highlighted.tokensByRowId[exportRow.id]).toEqual(standalone.tokensByRowId[exportRow.id]);
86+
});
87+
88+
it("does not join unhighlighted rows across cached gaps", async () => {
89+
const trailingRow = makeLine({
90+
id: "trailing-row",
91+
content: "export const answer = 42;",
92+
change: "add",
93+
oldLineNumber: null,
94+
newLineNumber: 3,
95+
});
96+
const rows = [
97+
makeLine({
98+
id: "template-open",
99+
content: "const message = `open",
100+
change: "add",
101+
oldLineNumber: null,
102+
newLineNumber: 1,
103+
}),
104+
makeLine({
105+
id: "template-close",
106+
content: "closed`;",
107+
change: "add",
108+
oldLineNumber: null,
109+
newLineNumber: 2,
110+
}),
111+
trailingRow,
112+
];
113+
114+
const [highlighted, standalone] = await Promise.all([
115+
highlight(rows, new Set(["template-close"])),
116+
highlight([trailingRow]),
117+
]);
118+
119+
expect(highlighted.tokensByRowId[trailingRow.id]).toEqual(
120+
standalone.tokensByRowId[trailingRow.id],
121+
);
122+
});
123+
124+
it("keeps deletion grammar state out of addition rows", async () => {
125+
const additionRow = makeLine({
126+
id: "addition-row",
127+
content: "export const answer = 42;",
128+
change: "add",
129+
oldLineNumber: null,
130+
newLineNumber: 1,
131+
});
132+
const rows = [
133+
makeLine({
134+
id: "deletion-row",
135+
content: "const removed = `open",
136+
change: "delete",
137+
oldLineNumber: 1,
138+
newLineNumber: null,
139+
}),
140+
additionRow,
141+
];
142+
143+
const [highlighted, standalone] = await Promise.all([
144+
highlight(rows),
145+
highlight([additionRow]),
146+
]);
147+
148+
expect(highlighted.tokensByRowId[additionRow.id]).toEqual(
149+
standalone.tokensByRowId[additionRow.id],
150+
);
151+
});
152+
});

‎apps/mobile/src/features/diffs/nativeReviewDiffHighlighter.ts‎

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ interface NativeReviewDiffLineRow extends NativeReviewDiffRow {
5656
readonly content: string;
5757
}
5858

59+
interface IndexedNativeReviewDiffLineRow {
60+
readonly row: NativeReviewDiffLineRow;
61+
readonly rowIndex: number;
62+
}
63+
5964
export interface NativeReviewDiffTokenChunk {
6065
readonly chunkIndex: number;
6166
readonly fileId: string;
@@ -308,6 +313,41 @@ function isHighlightableLineRow(row: NativeReviewDiffRow): row is NativeReviewDi
308313
return row.kind === "line" && typeof row.fileId === "string" && typeof row.content === "string";
309314
}
310315

316+
function hasConsecutiveLineNumbers(
317+
previous: number | null | undefined,
318+
next: number | null | undefined,
319+
): boolean {
320+
return typeof previous === "number" && typeof next === "number" && next === previous + 1;
321+
}
322+
323+
function canShareGrammarContext(
324+
previous: IndexedNativeReviewDiffLineRow,
325+
next: IndexedNativeReviewDiffLineRow,
326+
): boolean {
327+
if (next.rowIndex !== previous.rowIndex + 1 || next.row.fileId !== previous.row.fileId) {
328+
return false;
329+
}
330+
331+
if (previous.row.change === "delete" || next.row.change === "delete") {
332+
return (
333+
previous.row.change !== "add" &&
334+
next.row.change !== "add" &&
335+
hasConsecutiveLineNumbers(previous.row.oldLineNumber, next.row.oldLineNumber)
336+
);
337+
}
338+
339+
if (previous.row.change === "add" || next.row.change === "add") {
340+
return hasConsecutiveLineNumbers(previous.row.newLineNumber, next.row.newLineNumber);
341+
}
342+
343+
return (
344+
previous.row.change === "context" &&
345+
next.row.change === "context" &&
346+
hasConsecutiveLineNumbers(previous.row.oldLineNumber, next.row.oldLineNumber) &&
347+
hasConsecutiveLineNumbers(previous.row.newLineNumber, next.row.newLineNumber)
348+
);
349+
}
350+
311351
function groupLineRowsByFileId(rows: ReadonlyArray<NativeReviewDiffRow>) {
312352
const rowsByFileId = new Map<string, NativeReviewDiffLineRow[]>();
313353
for (const row of rows) {
@@ -360,7 +400,7 @@ export async function highlightNativeReviewDiffVisibleRows(
360400
const maxRows = input.maxRows ?? NATIVE_REVIEW_DIFF_VISIBLE_MAX_ROWS;
361401
const startIndex = clampRowIndex(input.firstRowIndex - overscanRows, input.rows);
362402
const endIndex = clampRowIndex(input.lastRowIndex + overscanRows, input.rows);
363-
const selectedRows: NativeReviewDiffLineRow[] = [];
403+
const selectedRows: IndexedNativeReviewDiffLineRow[] = [];
364404

365405
for (
366406
let rowIndex = startIndex;
@@ -374,12 +414,12 @@ export async function highlightNativeReviewDiffVisibleRows(
374414
!input.alreadyHighlightedRowIds?.has(row.id) &&
375415
fileMap.has(row.fileId)
376416
) {
377-
selectedRows.push(row);
417+
selectedRows.push({ row, rowIndex });
378418
}
379419
}
380420

381421
const tokensByRowId: Record<string, ReadonlyArray<NativeReviewDiffToken>> = {};
382-
let segmentRows: NativeReviewDiffLineRow[] = [];
422+
let segmentRows: IndexedNativeReviewDiffLineRow[] = [];
383423
let segmentFile: NativeReviewDiffFile | undefined;
384424

385425
const flushSegment = () => {
@@ -389,27 +429,33 @@ export async function highlightNativeReviewDiffVisibleRows(
389429
return;
390430
}
391431

392-
const code = segmentRows.map((row) => row.content).join("\n");
432+
const code = segmentRows.map(({ row }) => row.content).join("\n");
393433
const tokenLines = highlighter.tokenize(code, { lang: segmentFile.language, theme });
394-
segmentRows.forEach((row, rowIndex) => {
434+
segmentRows.forEach(({ row }, rowIndex) => {
395435
tokensByRowId[row.id] = tokenLines[rowIndex] ?? makePlainTokenFallback(row);
396436
});
397437
segmentRows = [];
398438
segmentFile = undefined;
399439
};
400440

401-
for (const row of selectedRows) {
441+
for (const selectedRow of selectedRows) {
442+
const { row } = selectedRow;
402443
const file = fileMap.get(row.fileId);
403444
if (!file) {
404445
continue;
405446
}
406447

407-
if (segmentFile && segmentFile.id !== file.id) {
448+
const previousRow = segmentRows.at(-1);
449+
if (
450+
segmentFile &&
451+
(segmentFile.id !== file.id ||
452+
(previousRow !== undefined && !canShareGrammarContext(previousRow, selectedRow)))
453+
) {
408454
flushSegment();
409455
}
410456

411457
segmentFile = file;
412-
segmentRows.push(row);
458+
segmentRows.push(selectedRow);
413459
}
414460
flushSegment();
415461

0 commit comments

Comments
 (0)