Open History to see the last commit
1import { parsePatchFiles } from "@pierre/diffs/utils/parsePatchFiles";
2import type { FileDiffMetadata } from "@pierre/diffs/types";
3
4export const DIFF_THEME_NAMES = {
5 light: "pierre-light",
6 dark: "pierre-dark",
7} as const;
8
9export type DiffThemeName = (typeof DIFF_THEME_NAMES)[keyof typeof DIFF_THEME_NAMES];
10
11export function resolveDiffThemeName(theme: "light" | "dark"): DiffThemeName {
12 return theme === "dark" ? DIFF_THEME_NAMES.dark : DIFF_THEME_NAMES.light;
13}
14
15const FNV_OFFSET_BASIS_32 = 0x811c9dc5;
16const FNV_PRIME_32 = 0x01000193;
17const SECONDARY_HASH_SEED = 0x9e3779b9;
18const SECONDARY_HASH_MULTIPLIER = 0x85ebca6b;
19
20export function fnv1a32(
21 input: string,
22 seed = FNV_OFFSET_BASIS_32,
23 multiplier = FNV_PRIME_32,
24): number {
25 let hash = seed >>> 0;
26 for (let index = 0; index < input.length; index += 1) {
27 hash ^= input.charCodeAt(index);
28 hash = Math.imul(hash, multiplier) >>> 0;
29 }
30 return hash >>> 0;
31}
32
33export function buildPatchCacheKey(patch: string, scope = "diff-panel"): string {
34 const normalizedPatch = patch.trim();
35 const primary = fnv1a32(normalizedPatch, FNV_OFFSET_BASIS_32, FNV_PRIME_32).toString(36);
36 const secondary = fnv1a32(
37 normalizedPatch,
38 SECONDARY_HASH_SEED,
39 SECONDARY_HASH_MULTIPLIER,
40 ).toString(36);
41 return `${scope}:${normalizedPatch.length}:${primary}:${secondary}`;
42}
43
44export type RenderablePatch =
45 | {
46 kind: "files";
47 files: FileDiffMetadata[];
48 }
49 | {
50 kind: "raw";
51 text: string;
52 reason: string;
53 };
54
55export interface DiffLineStat {
56 additions: number;
57 deletions: number;
58}
59
60export function getDiffLineStat(files: ReadonlyArray<FileDiffMetadata>): DiffLineStat {
61 return files.reduce<DiffLineStat>(
62 (total, file) => {
63 for (const hunk of file.hunks) {
64 total.additions += hunk.additionLines;
65 total.deletions += hunk.deletionLines;
66 }
67
68 return total;
69 },
70 { additions: 0, deletions: 0 },
71 );
72}
73
74interface RenderablePatchOptions {
75 /**
76 * Pierre's partial-patch parser keeps hunk render starts in source-file
77 * coordinates. Its virtualizer iterates partial patches as compact rows, so
78 * review diffs need compact render starts while retaining collapsedBefore
79 * for the "N unmodified lines" separator.
80 */
81 compactPartialHunkOffsets?: boolean;
82}
83
84export function compactPartialHunkOffsets(file: FileDiffMetadata): FileDiffMetadata {
85 if (!file.isPartial) return file;
86
87 let splitLineStart = 0;
88 let unifiedLineStart = 0;
89 const hunks = file.hunks.map((hunk) => {
90 const compactHunk = {
91 ...hunk,
92 splitLineStart,
93 unifiedLineStart,
94 };
95 splitLineStart += hunk.splitLineCount;
96 unifiedLineStart += hunk.unifiedLineCount;
97 return compactHunk;
98 });
99
100 return {
101 ...file,
102 hunks,
103 splitLineCount: splitLineStart,
104 unifiedLineCount: unifiedLineStart,
105 ...(file.cacheKey ? { cacheKey: `${file.cacheKey}:compact-partial` } : {}),
106 };
107}
108
109export function getRenderablePatch(
110 patch: string | undefined,
111 cacheScope = "diff-panel",
112 options: RenderablePatchOptions = {},
113): RenderablePatch | null {
114 if (!patch) return null;
115 const normalizedPatch = patch.trim();
116 if (normalizedPatch.length === 0) return null;
117
118 try {
119 const parsedPatches = parsePatchFiles(
120 normalizedPatch,
121 buildPatchCacheKey(normalizedPatch, cacheScope),
122 );
123 const files = parsedPatches.flatMap((parsedPatch) =>
124 options.compactPartialHunkOffsets
125 ? parsedPatch.files.map(compactPartialHunkOffsets)
126 : parsedPatch.files,
127 );
128 if (files.length > 0) {
129 return { kind: "files", files };
130 }
131
132 return {
133 kind: "raw",
134 text: normalizedPatch,
135 reason: "Unsupported diff format. Showing raw patch.",
136 };
137 } catch {
138 return {
139 kind: "raw",
140 text: normalizedPatch,
141 reason: "Failed to parse patch. Showing raw patch.",
142 };
143 }
144}
145
146export function resolveFileDiffPath(fileDiff: FileDiffMetadata): string {
147 const raw = fileDiff.name ?? fileDiff.prevName ?? "";
148 if (raw.startsWith("a/") || raw.startsWith("b/")) {
149 return raw.slice(2);
150 }
151 return raw;
152}
153
154/**
155 * What the file was called before the change. Only a rename makes it differ from the current
156 * path, and the hosts that resolve a diff position against both sides need both names.
157 */
158export function resolveFileDiffPreviousPath(fileDiff: FileDiffMetadata): string {
159 const raw = fileDiff.prevName ?? fileDiff.name ?? "";
160 if (raw.startsWith("a/") || raw.startsWith("b/")) {
161 return raw.slice(2);
162 }
163 return raw;
164}
165
166export function buildFileDiffRenderKey(fileDiff: FileDiffMetadata): string {
167 const cacheKey = fileDiff.cacheKey;
168 if (!cacheKey) return `${fileDiff.prevName ?? "none"}:${fileDiff.name}`;
169
170 return cacheKey.endsWith(":hydrated") ? cacheKey.slice(0, -":hydrated".length) : cacheKey;
171}
172
173export function getDiffCollapseIconClassName(fileDiff: FileDiffMetadata): string {
174 switch (fileDiff.type) {
175 case "new":
176 return "text-[var(--diffs-addition-base)]";
177 case "deleted":
178 return "text-[var(--diffs-deletion-base)]";
179 case "change":
180 case "rename-pure":
181 case "rename-changed":
182 return "text-[var(--diffs-modified-base)]";
183 default:
184 return "text-muted-foreground/80";
185 }
186}
187
188/**
189 * Maps every diff/file surface the @pierre/diffs renderer paints onto the
190 * app's code tokens, so themed palettes reach the code body, gutter, and
191 * row tints instead of the renderer's bundled colors. Shared by the diff
192 * panel and the file preview.
193 */
194export const DIFF_SURFACE_THEME_UNSAFE_CSS = `
195[data-diffs-header],
196[data-diff],
197[data-file],
198[data-error-wrapper],
199[data-virtualizer-buffer] {
200 --diffs-header-font-family: var(--font-sans) !important;
201 --diffs-font-family: var(--font-mono) !important;
202 --diffs-bg: var(--code-background) !important;
203 --diffs-light-bg: var(--code-background) !important;
204 --diffs-dark-bg: var(--code-background) !important;
205 --diffs-token-light-bg: transparent;
206 --diffs-token-dark-bg: transparent;
207
208 /* Gutter, context, and row tints all derive from the code surface the diff
209 body sits on — mixing from the canvas leaves the gutter looking unthemed
210 when a palette separates the two. */
211 --diffs-bg-context-override: color-mix(in srgb, var(--code-background) 97%, var(--code-foreground));
212 --diffs-bg-hover-override: color-mix(in srgb, var(--code-background) 94%, var(--code-foreground));
213 --diffs-bg-separator-override: color-mix(
214 in srgb,
215 var(--code-background) 95%,
216 var(--code-foreground)
217 );
218 --diffs-bg-buffer-override: color-mix(in srgb, var(--code-background) 90%, var(--code-foreground));
219
220 --diffs-bg-addition-override: light-dark(
221 color-mix(in srgb, var(--code-background) 50%, var(--success)),
222 color-mix(in srgb, var(--code-background) 70%, var(--success))
223 );
224 --diffs-bg-addition-number-override: light-dark(
225 color-mix(in srgb, var(--code-background) 35%, var(--success)),
226 color-mix(in srgb, var(--code-background) 60%, var(--success))
227 );
228 --diffs-bg-addition-hover-override: color-mix(in srgb, var(--code-background) 85%, var(--success));
229 --diffs-bg-addition-emphasis-override: color-mix(
230 in srgb,
231 var(--code-background) 80%,
232 var(--success)
233 );
234
235 --diffs-bg-deletion-override: light-dark(
236 color-mix(in srgb, var(--code-background) 50%, var(--destructive)),
237 color-mix(in srgb, var(--code-background) 70%, var(--destructive))
238 );
239 --diffs-bg-deletion-number-override: light-dark(
240 color-mix(in srgb, var(--code-background) 35%, var(--destructive)),
241 color-mix(in srgb, var(--code-background) 60%, var(--destructive))
242 );
243 --diffs-bg-deletion-hover-override: color-mix(
244 in srgb,
245 var(--code-background) 85%,
246 var(--destructive)
247 );
248 --diffs-bg-deletion-emphasis-override: color-mix(
249 in srgb,
250 var(--code-background) 80%,
251 var(--destructive)
252 );
253
254 background-color: var(--diffs-bg) !important;
255 color: var(--code-foreground) !important;
256}
257`;