Repository navigation
Expand file tree
/
Copy pathdiffRendering.ts
More file actions
352 lines (318 loc) · 11.8 KB
/
Copy pathdiffRendering.ts
File metadata and controls
352 lines (318 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import { parsePatchFiles } from "@pierre/diffs/utils/parsePatchFiles";
import { parseDiffFromFile } from "@pierre/diffs";
import type { FileDiffMetadata } from "@pierre/diffs/types";
import { READABLE_LIGHT_THEME_NAME } from "./readableSyntaxTheme";
// Light mode uses a darker pierre-light so syntax stays readable on every code surface.
export const DIFF_THEME_NAMES = {
light: READABLE_LIGHT_THEME_NAME,
dark: "pierre-dark",
} as const;
export type DiffThemeName = (typeof DIFF_THEME_NAMES)[keyof typeof DIFF_THEME_NAMES];
export function resolveDiffThemeName(theme: "light" | "dark"): DiffThemeName {
return DIFF_THEME_NAMES[theme];
}
const FNV_OFFSET_BASIS_32 = 0x811c9dc5;
const FNV_PRIME_32 = 0x01000193;
const SECONDARY_HASH_SEED = 0x9e3779b9;
const SECONDARY_HASH_MULTIPLIER = 0x85ebca6b;
export function fnv1a32(
input: string,
seed = FNV_OFFSET_BASIS_32,
multiplier = FNV_PRIME_32,
): number {
let hash = seed >>> 0;
for (let index = 0; index < input.length; index += 1) {
hash ^= input.charCodeAt(index);
hash = Math.imul(hash, multiplier) >>> 0;
}
return hash >>> 0;
}
function buildPatchCacheKey(patch: string, scope = "diff-panel"): string {
const primary = fnv1a32(patch, FNV_OFFSET_BASIS_32, FNV_PRIME_32).toString(36);
const secondary = fnv1a32(patch, SECONDARY_HASH_SEED, SECONDARY_HASH_MULTIPLIER).toString(36);
return `${scope}:${patch.length}:${primary}:${secondary}`;
}
export type RenderablePatch =
| {
kind: "files";
files: FileDiffMetadata[];
sourceFiles: FileDiffMetadata[];
}
| {
kind: "raw";
text: string;
reason: string;
};
export interface DiffLineStat {
additions: number;
deletions: number;
}
export function getDiffLineStat(files: ReadonlyArray<FileDiffMetadata>): DiffLineStat {
return files.reduce<DiffLineStat>(
(total, file) => {
for (const hunk of file.hunks) {
total.additions += hunk.additionLines;
total.deletions += hunk.deletionLines;
}
return total;
},
{ additions: 0, deletions: 0 },
);
}
interface RenderablePatchOptions {
ignoreWhitespace?: boolean;
/** Compact Pierre source offsets for partial-patch rows; retain collapsedBefore for separators. */
compactPartialHunkOffsets?: boolean;
}
function hideWhitespaceChanges(file: FileDiffMetadata): FileDiffMetadata {
let splitDelta = 0;
let unifiedDelta = 0;
const hunks = file.hunks.map((hunk) => {
const oldContents = file.deletionLines
.slice(hunk.deletionLineIndex, hunk.deletionLineIndex + hunk.deletionCount)
.map((line) => `${line.replace(/\s/g, "")}\n`)
.join("");
const newContents = file.additionLines
.slice(hunk.additionLineIndex, hunk.additionLineIndex + hunk.additionCount)
.map((line) => `${line.replace(/\s/g, "")}\n`)
.join("");
const filtered = parseDiffFromFile(
{ name: file.name, contents: oldContents },
{ name: file.name, contents: newContents },
{ context: Infinity },
).hunks[0];
const next = {
...hunk,
additionLines: filtered?.additionLines ?? 0,
deletionLines: filtered?.deletionLines ?? 0,
hunkContent: filtered
? filtered.hunkContent.map((content) => ({
...content,
additionLineIndex: content.additionLineIndex + hunk.additionLineIndex,
deletionLineIndex: content.deletionLineIndex + hunk.deletionLineIndex,
}))
: [
{
type: "context" as const,
lines: hunk.additionCount,
additionLineIndex: hunk.additionLineIndex,
deletionLineIndex: hunk.deletionLineIndex,
},
],
splitLineStart: hunk.splitLineStart + splitDelta,
unifiedLineStart: hunk.unifiedLineStart + unifiedDelta,
splitLineCount: filtered?.splitLineCount ?? hunk.additionCount,
unifiedLineCount: filtered?.unifiedLineCount ?? hunk.additionCount,
};
splitDelta += next.splitLineCount - hunk.splitLineCount;
unifiedDelta += next.unifiedLineCount - hunk.unifiedLineCount;
return next;
});
return {
...file,
hunks,
splitLineCount: file.splitLineCount + splitDelta,
unifiedLineCount: file.unifiedLineCount + unifiedDelta,
...(file.cacheKey ? { cacheKey: `${file.cacheKey}:ignore-whitespace` } : {}),
};
}
function compactPartialHunkOffsets(file: FileDiffMetadata): FileDiffMetadata {
if (!file.isPartial) return file;
let splitLineStart = 0;
let unifiedLineStart = 0;
const hunks = file.hunks.map((hunk) => {
const compactHunk = {
...hunk,
splitLineStart,
unifiedLineStart,
};
splitLineStart += hunk.splitLineCount;
unifiedLineStart += hunk.unifiedLineCount;
return compactHunk;
});
return {
...file,
hunks,
splitLineCount: splitLineStart,
unifiedLineCount: unifiedLineStart,
...(file.cacheKey ? { cacheKey: `${file.cacheKey}:compact-partial` } : {}),
};
}
export function getRenderablePatch(
patch: string | undefined,
cacheScope = "diff-panel",
options: RenderablePatchOptions = {},
): RenderablePatch | null {
if (!patch?.trim()) return null;
try {
const parsedPatches = parsePatchFiles(patch, {
cacheKeyPrefix: buildPatchCacheKey(patch, cacheScope),
});
const sourceFiles = parsedPatches.flatMap((parsedPatch) => parsedPatch.files);
const files = sourceFiles.map((file) => {
const filtered = options.ignoreWhitespace ? hideWhitespaceChanges(file) : file;
return options.compactPartialHunkOffsets ? compactPartialHunkOffsets(filtered) : filtered;
});
if (files.length > 0) {
return { kind: "files", files, sourceFiles };
}
return {
kind: "raw",
text: patch,
reason: "Unsupported diff format. Showing raw patch.",
};
} catch {
return {
kind: "raw",
text: patch,
reason: "Failed to parse patch. Showing raw patch.",
};
}
}
/** Uses the parser-decoded filename, including Git's quoted and escaped names. */
export function resolveFileDiffPath(fileDiff: FileDiffMetadata): string {
return fileDiff.name ?? fileDiff.prevName ?? "";
}
export function resolveFileDiffPreviousPath(fileDiff: FileDiffMetadata): string {
return fileDiff.prevName ?? fileDiff.name ?? "";
}
/** Includes change type to distinguish deletion/addition pairs for file-to-symlink changes. */
export function buildFileDiffIdentityKey(fileDiff: FileDiffMetadata): string {
return `${resolveFileDiffPreviousPath(fileDiff)}\u0000${resolveFileDiffPath(fileDiff)}\u0000${fileDiff.type}`;
}
export function buildFileDiffRenderKey(fileDiff: FileDiffMetadata): string {
const cacheKey = fileDiff.cacheKey;
if (!cacheKey) return `${fileDiff.prevName ?? "none"}:${fileDiff.name}`;
return cacheKey.endsWith(":hydrated") ? cacheKey.slice(0, -":hydrated".length) : cacheKey;
}
function hashFileDiffPart(hash: number, value: string | number | boolean | undefined): number {
const serialized = value === undefined ? "undefined" : String(value);
const withLength = fnv1a32(`${typeof value}:${serialized.length}:`, hash);
return fnv1a32(serialized, withLength);
}
// Pierre's whole-patch cache key would repaint every file when one changes.
export function buildFileDiffContentVersion(fileDiff: FileDiffMetadata): number {
let hash = FNV_OFFSET_BASIS_32;
const append = (value: string | number | boolean | undefined) => {
hash = hashFileDiffPart(hash, value);
};
append(fileDiff.name);
append(fileDiff.prevName);
append(fileDiff.lang);
append(fileDiff.newObjectId);
append(fileDiff.prevObjectId);
append(fileDiff.mode);
append(fileDiff.prevMode);
append(fileDiff.type);
append(fileDiff.isPartial);
append(fileDiff.splitLineCount);
append(fileDiff.unifiedLineCount);
for (const line of fileDiff.additionLines) append(line);
for (const line of fileDiff.deletionLines) append(line);
for (const hunk of fileDiff.hunks) {
append(hunk.collapsedBefore);
append(hunk.additionStart);
append(hunk.additionCount);
append(hunk.additionLines);
append(hunk.additionLineIndex);
append(hunk.deletionStart);
append(hunk.deletionCount);
append(hunk.deletionLines);
append(hunk.deletionLineIndex);
append(hunk.hunkContext);
append(hunk.hunkSpecs);
append(hunk.splitLineStart);
append(hunk.splitLineCount);
append(hunk.unifiedLineStart);
append(hunk.unifiedLineCount);
append(hunk.noEOFCRAdditions);
append(hunk.noEOFCRDeletions);
for (const content of hunk.hunkContent) {
append(content.type);
append(content.additionLineIndex);
append(content.deletionLineIndex);
append(content.type === "change" ? content.additions : content.lines);
append(content.type === "change" ? content.deletions : undefined);
}
}
return hash;
}
export const DIFF_SURFACE_THEME_UNSAFE_CSS = `
[data-diffs-header],
[data-diff],
[data-file],
[data-error-wrapper],
[data-virtualizer-buffer] {
--diffs-header-font-family: var(--font-sans) !important;
--diffs-font-family: var(--font-mono) !important;
--diffs-bg: var(--code-background) !important;
--diffs-light-bg: var(--code-background) !important;
--diffs-dark-bg: var(--code-background) !important;
--diffs-token-light-bg: transparent;
--diffs-token-dark-bg: transparent;
/* Gutter, context, and row tints all derive from the code surface the diff
body sits on. Mixing from the canvas leaves the gutter looking unthemed
when a palette separates the two. */
--diffs-bg-context-override: color-mix(in srgb, var(--code-background) 97%, var(--code-foreground));
--diffs-bg-hover-override: color-mix(in srgb, var(--code-background) 94%, var(--code-foreground));
--diffs-bg-separator-override: color-mix(
in srgb,
var(--code-background) 95%,
var(--code-foreground)
);
--diffs-bg-buffer-override: color-mix(in srgb, var(--code-background) 90%, var(--code-foreground));
--diffs-bg-addition-override: light-dark(
color-mix(in srgb, var(--code-background) 50%, var(--diff-addition)),
color-mix(in srgb, var(--code-background) 70%, var(--diff-addition))
);
--diffs-bg-addition-number-override: light-dark(
color-mix(in srgb, var(--code-background) 35%, var(--diff-addition)),
color-mix(in srgb, var(--code-background) 60%, var(--diff-addition))
);
--diffs-bg-addition-hover-override: color-mix(in srgb, var(--code-background) 85%, var(--diff-addition));
--diffs-bg-addition-emphasis-override: color-mix(
in srgb,
var(--code-background) 80%,
var(--diff-addition)
);
--diffs-bg-deletion-override: light-dark(
color-mix(in srgb, var(--code-background) 50%, var(--diff-deletion)),
color-mix(in srgb, var(--code-background) 70%, var(--diff-deletion))
);
--diffs-bg-deletion-number-override: light-dark(
color-mix(in srgb, var(--code-background) 35%, var(--diff-deletion)),
color-mix(in srgb, var(--code-background) 60%, var(--diff-deletion))
);
--diffs-bg-deletion-hover-override: color-mix(
in srgb,
var(--code-background) 85%,
var(--diff-deletion)
);
--diffs-bg-deletion-emphasis-override: color-mix(
in srgb,
var(--code-background) 80%,
var(--diff-deletion)
);
background-color: var(--diffs-bg) !important;
color: var(--code-foreground) !important;
}
/* The library's change-icon colors are tuned for dark canvases; light mode takes the app's darker tokens. */
[data-change-icon="new"] {
color: var(
--diff-added-icon,
light-dark(var(--diff-addition-foreground), var(--diffs-addition-base))
) !important;
}
[data-change-icon="deleted"] {
color: light-dark(var(--diff-deletion-foreground), var(--diffs-deletion-base)) !important;
}
/* The page sets --diff-modified-icon to a muted T3 blue, so the common case stays quiet. */
[data-change-icon="change"],
[data-change-icon="rename-pure"],
[data-change-icon="rename-changed"] {
color: var(
--diff-modified-icon,
light-dark(var(--info-foreground), var(--diffs-modified-base))
) !important;
}
`;