Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

nativeReviewDiffHighlighter.ts

153 lines4.9 KB

1import { highlightCode, type HighlightScheme } from "../../lib/highlights";
2import type { NativeReviewDiffFile } from "./nativeReviewDiffTypes";
3import type { NativeReviewDiffRow, NativeReviewDiffToken } from "./nativeReviewDiffSurface";
4​
5interface NativeReviewDiffLineRow extends NativeReviewDiffRow {
6 readonly kind: "line";
7 readonly fileId: string;
8 readonly content: string;
9}
10​
11interface IndexedNativeReviewDiffLineRow {
12 readonly row: NativeReviewDiffLineRow;
13 readonly rowIndex: number;
15​
16export interface HighlightNativeReviewDiffVisibleRowsInput {
17 readonly rows: ReadonlyArray<NativeReviewDiffRow>;
18 readonly files: ReadonlyArray<NativeReviewDiffFile>;
19 readonly scheme: HighlightScheme;
20 readonly firstRowIndex: number;
21 readonly lastRowIndex: number;
22 readonly overscanRows?: number;
23 readonly maxRows?: number;
24 readonly alreadyHighlightedRowIds?: ReadonlySet<string>;
26​
27// Bounds each token patch the native view parses, not the highlighting itself.
28const NATIVE_REVIEW_DIFF_VISIBLE_OVERSCAN_ROWS = 160;
29const NATIVE_REVIEW_DIFF_VISIBLE_MAX_ROWS = 360;
30​
31function isHighlightableLineRow(row: NativeReviewDiffRow): row is NativeReviewDiffLineRow {
32 return row.kind === "line" && typeof row.fileId === "string" && typeof row.content === "string";
34​
35function hasConsecutiveLineNumbers(
36 previous: number | null | undefined,
37 next: number | null | undefined,
38): boolean {
39 return typeof previous === "number" && typeof next === "number" && next === previous + 1;
41​
42function hasOnlyCommentRowsBetween(
43 rows: ReadonlyArray<NativeReviewDiffRow>,
44 previousRowIndex: number,
45 nextRowIndex: number,
46): boolean {
47 for (let rowIndex = previousRowIndex + 1; rowIndex < nextRowIndex; rowIndex += 1) {
48 if (rows[rowIndex]?.kind !== "comment") {
49 return false;
50 }
51 }
52 return true;
54​
55function canShareGrammarContext(
56 previous: IndexedNativeReviewDiffLineRow,
57 next: IndexedNativeReviewDiffLineRow,
58 rows: ReadonlyArray<NativeReviewDiffRow>,
59): boolean {
60 if (
61 next.row.fileId !== previous.row.fileId ||
62 !hasOnlyCommentRowsBetween(rows, previous.rowIndex, next.rowIndex)
63 ) {
64 return false;
65 }
66​
67 if (previous.row.change === "delete" || next.row.change === "delete") {
68 return (
69 previous.row.change !== "add" &&
70 next.row.change !== "add" &&
71 hasConsecutiveLineNumbers(previous.row.oldLineNumber, next.row.oldLineNumber)
72 );
73 }
74​
75 if (previous.row.change === "add" || next.row.change === "add") {
76 return hasConsecutiveLineNumbers(previous.row.newLineNumber, next.row.newLineNumber);
77 }
78​
79 return (
80 previous.row.change === "context" &&
81 next.row.change === "context" &&
82 hasConsecutiveLineNumbers(previous.row.oldLineNumber, next.row.oldLineNumber) &&
83 hasConsecutiveLineNumbers(previous.row.newLineNumber, next.row.newLineNumber)
84 );
86​
87function clampRowIndex(index: number, rows: ReadonlyArray<NativeReviewDiffRow>) {
88 if (rows.length === 0) {
89 return 0;
90 }
91​
92 return Math.min(rows.length - 1, Math.max(0, Math.floor(index)));
94​
95export function highlightNativeReviewDiffVisibleRows(
96 input: HighlightNativeReviewDiffVisibleRowsInput,
97): {
98 readonly tokensByRowId: Record<string, ReadonlyArray<NativeReviewDiffToken>>;
99 readonly rowCount: number;
100} {
101 const fileMap = new Map(input.files.map((file) => [file.id, file]));
102 const overscanRows = input.overscanRows ?? NATIVE_REVIEW_DIFF_VISIBLE_OVERSCAN_ROWS;
103 const maxRows = input.maxRows ?? NATIVE_REVIEW_DIFF_VISIBLE_MAX_ROWS;
104 const startIndex = clampRowIndex(input.firstRowIndex - overscanRows, input.rows);
105 const endIndex = clampRowIndex(input.lastRowIndex + overscanRows, input.rows);
106 const selectedRows: IndexedNativeReviewDiffLineRow[] = [];
107​
108 for (
109 let rowIndex = startIndex;
110 rowIndex <= endIndex && selectedRows.length < maxRows;
111 rowIndex += 1
112 ) {
113 const row = input.rows[rowIndex];
114 if (
115 row &&
116 isHighlightableLineRow(row) &&
117 !input.alreadyHighlightedRowIds?.has(row.id) &&
118 fileMap.has(row.fileId)
119 ) {
120 selectedRows.push({ row, rowIndex });
121 }
122 }
123​
124 // Rows that continue each other's source are highlighted together so
125 // multi-line constructs keep their colors.
126 const tokensByRowId: Record<string, ReadonlyArray<NativeReviewDiffToken>> = {};
127 let segment: IndexedNativeReviewDiffLineRow[] = [];
128 const flushSegment = () => {
129 const file = segment[0] ? fileMap.get(segment[0].row.fileId) : undefined;
130 if (file) {
131 const tokenLines = highlightCode(
132 segment.map(({ row }) => row.content).join("\n"),
133 file.language,
134 input.scheme,
135 );
136 segment.forEach(({ row }, index) => {
137 tokensByRowId[row.id] = tokenLines[index] ?? [];
138 });
139 }
140 segment = [];
141 };
142​
143 for (const selectedRow of selectedRows) {
144 const previousRow = segment.at(-1);
145 if (previousRow && !canShareGrammarContext(previousRow, selectedRow, input.rows)) {
146 flushSegment();
147 }
148 segment.push(selectedRow);
149 }
150 flushSegment();
151​
152 return { tokensByRowId, rowCount: selectedRows.length };