The best way to control Claude Code, Codex and more from any computer or phone

main
ts 111 lines 3.89 KB
Raw
1 import { resolveHighlightLanguage } from "../../lib/highlightLanguage";
2 import { highlightCode, type HighlightScheme } from "../../lib/highlights";
3 import type { ReviewHighlightedToken } from "./reviewHighlightedToken.types";
4 import type { ReviewRenderableLineRow } from "./reviewModel";
5 import { applyDiffRangesToTokens, computeWordAltDiffRanges } from "./reviewWordDiffs";
6
7 function applyWordAltDiffHighlightsToSelectedLines(input: {
8 readonly lines: ReadonlyArray<ReviewRenderableLineRow>;
9 readonly tokenMap: Record<string, ReadonlyArray<ReviewHighlightedToken>>;
10 }): Record<string, ReadonlyArray<ReviewHighlightedToken>> {
11 const additionLineByTokenIndex = new Map<number, ReviewRenderableLineRow>();
12 const deletionLineByTokenIndex = new Map<number, ReviewRenderableLineRow>();
13
14 input.lines.forEach((line) => {
15 if (line.change === "add" && line.additionTokenIndex !== null) {
16 additionLineByTokenIndex.set(line.additionTokenIndex, line);
17 }
18 if (line.change === "delete" && line.deletionTokenIndex !== null) {
19 deletionLineByTokenIndex.set(line.deletionTokenIndex, line);
20 }
21 });
22
23 const nextTokenMap = { ...input.tokenMap };
24 const processedPairs = new Set<string>();
25
26 input.lines.forEach((line) => {
27 if (line.change === "context" || !line.comparison) {
28 return;
29 }
30
31 const pairedDeletionLine =
32 line.change === "delete"
33 ? line
34 : line.comparison.change === "delete"
35 ? deletionLineByTokenIndex.get(line.comparison.tokenIndex)
36 : undefined;
37 const pairedAdditionLine =
38 line.change === "add"
39 ? line
40 : line.comparison.change === "add"
41 ? additionLineByTokenIndex.get(line.comparison.tokenIndex)
42 : undefined;
43
44 if (
45 !pairedDeletionLine ||
46 !pairedAdditionLine ||
47 pairedDeletionLine.deletionTokenIndex === null ||
48 pairedAdditionLine.additionTokenIndex === null
49 ) {
50 return;
51 }
52
53 const pairKey = `${pairedDeletionLine.deletionTokenIndex}:${pairedAdditionLine.additionTokenIndex}`;
54 if (processedPairs.has(pairKey)) {
55 return;
56 }
57 processedPairs.add(pairKey);
58
59 const ranges = computeWordAltDiffRanges({
60 deletionLine: pairedDeletionLine.content,
61 additionLine: pairedAdditionLine.content,
62 });
63
64 if (ranges.deletion.length > 0) {
65 nextTokenMap[pairedDeletionLine.id] = applyDiffRangesToTokens(
66 nextTokenMap[pairedDeletionLine.id] ?? [],
67 ranges.deletion,
68 );
69 }
70 if (ranges.addition.length > 0) {
71 nextTokenMap[pairedAdditionLine.id] = applyDiffRangesToTokens(
72 nextTokenMap[pairedAdditionLine.id] ?? [],
73 ranges.addition,
74 );
75 }
76 });
77
78 return nextTokenMap;
79 }
80
81 export function highlightReviewSelectedLines(input: {
82 readonly filePath: string;
83 readonly lines: ReadonlyArray<ReviewRenderableLineRow>;
84 readonly theme: HighlightScheme;
85 }): Record<string, ReadonlyArray<ReviewHighlightedToken>> {
86 if (input.lines.length === 0) {
87 return {};
88 }
89
90 // Deleted and added lines each read as their own continuous source.
91 const language = resolveHighlightLanguage({ path: input.filePath });
92 const deletionLines = input.lines.filter((line) => line.change === "delete");
93 const additionLikeLines = input.lines.filter((line) => line.change !== "delete");
94 const highlightSide = (lines: ReadonlyArray<ReviewRenderableLineRow>) =>
95 highlightCode(lines.map((line) => line.content).join("\n"), language, input.theme);
96 const deletionTokens = highlightSide(deletionLines);
97 const additionTokens = highlightSide(additionLikeLines);
98
99 const tokenMap: Record<string, ReadonlyArray<ReviewHighlightedToken>> = {};
100 deletionLines.forEach((line, index) => {
101 tokenMap[line.id] = deletionTokens[index] ?? [];
102 });
103 additionLikeLines.forEach((line, index) => {
104 tokenMap[line.id] = additionTokens[index] ?? [];
105 });
106
107 return applyWordAltDiffHighlightsToSelectedLines({
108 lines: input.lines,
109 tokenMap,
110 });
111 }