Skip to content

Commit c3138b7

Browse files
Preserve diff highlight state across comments
- Keep grammar context through inline review comment rows - Cover multiline template highlighting with a regression test
1 parent dc3a09e commit c3138b7

2 files changed

Lines changed: 57 additions & 2 deletions

File tree

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,43 @@ describe("highlightNativeReviewDiffVisibleRows", () => {
8585
expect(highlighted.tokensByRowId[exportRow.id]).toEqual(standalone.tokensByRowId[exportRow.id]);
8686
});
8787

88+
it("keeps grammar state across inline comment rows", async () => {
89+
const openingRow = makeLine({
90+
id: "template-open",
91+
content: "const message = `open",
92+
change: "add",
93+
oldLineNumber: null,
94+
newLineNumber: 1,
95+
});
96+
const closingRow = makeLine({
97+
id: "template-close",
98+
content: "closed`;",
99+
change: "add",
100+
oldLineNumber: null,
101+
newLineNumber: 2,
102+
});
103+
const trailingRow = makeLine({
104+
id: "trailing-row",
105+
content: "export const answer = 42;",
106+
change: "add",
107+
oldLineNumber: null,
108+
newLineNumber: 3,
109+
});
110+
const commentRow: NativeReviewDiffRow = {
111+
kind: "comment",
112+
id: "comment-1",
113+
fileId: TYPESCRIPT_FILE.id,
114+
commentText: "Review note",
115+
};
116+
117+
const [withComment, contiguous] = await Promise.all([
118+
highlight([openingRow, commentRow, closingRow, trailingRow]),
119+
highlight([openingRow, closingRow, trailingRow]),
120+
]);
121+
122+
expect(withComment.tokensByRowId).toEqual(contiguous.tokensByRowId);
123+
});
124+
88125
it("does not join unhighlighted rows across cached gaps", async () => {
89126
const trailingRow = makeLine({
90127
id: "trailing-row",

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,28 @@ function hasConsecutiveLineNumbers(
320320
return typeof previous === "number" && typeof next === "number" && next === previous + 1;
321321
}
322322

323+
function hasOnlyCommentRowsBetween(
324+
rows: ReadonlyArray<NativeReviewDiffRow>,
325+
previousRowIndex: number,
326+
nextRowIndex: number,
327+
): boolean {
328+
for (let rowIndex = previousRowIndex + 1; rowIndex < nextRowIndex; rowIndex += 1) {
329+
if (rows[rowIndex]?.kind !== "comment") {
330+
return false;
331+
}
332+
}
333+
return true;
334+
}
335+
323336
function canShareGrammarContext(
324337
previous: IndexedNativeReviewDiffLineRow,
325338
next: IndexedNativeReviewDiffLineRow,
339+
rows: ReadonlyArray<NativeReviewDiffRow>,
326340
): boolean {
327-
if (next.rowIndex !== previous.rowIndex + 1 || next.row.fileId !== previous.row.fileId) {
341+
if (
342+
next.row.fileId !== previous.row.fileId ||
343+
!hasOnlyCommentRowsBetween(rows, previous.rowIndex, next.rowIndex)
344+
) {
328345
return false;
329346
}
330347

@@ -449,7 +466,8 @@ export async function highlightNativeReviewDiffVisibleRows(
449466
if (
450467
segmentFile &&
451468
(segmentFile.id !== file.id ||
452-
(previousRow !== undefined && !canShareGrammarContext(previousRow, selectedRow)))
469+
(previousRow !== undefined &&
470+
!canShareGrammarContext(previousRow, selectedRow, input.rows)))
453471
) {
454472
flushSegment();
455473
}

0 commit comments

Comments
 (0)