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

diffRendering.ts

171 lines4.6 KB

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;
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;
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}`;
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;
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 );
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;
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 };
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 }
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;
153​
154export function buildFileDiffRenderKey(fileDiff: FileDiffMetadata): string {
155 return fileDiff.cacheKey ?? `${fileDiff.prevName ?? "none"}:${fileDiff.name}`;
157​
158export function getDiffCollapseIconClassName(fileDiff: FileDiffMetadata): string {
159 switch (fileDiff.type) {
160 case "new":
161 return "text-[var(--diffs-addition-base)]";
162 case "deleted":
163 return "text-[var(--diffs-deletion-base)]";
164 case "change":
165 case "rename-pure":
166 case "rename-changed":
167 return "text-[var(--diffs-modified-base)]";
168 default:
169 return "text-muted-foreground/80";
170 }