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

diffRendering.ts

152 lines4.2 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​
55interface RenderablePatchOptions {
56 /**
57 * Pierre's partial-patch parser keeps hunk render starts in source-file
58 * coordinates. Its virtualizer iterates partial patches as compact rows, so
59 * review diffs need compact render starts while retaining collapsedBefore
60 * for the "N unmodified lines" separator.
61 */
62 compactPartialHunkOffsets?: boolean;
64​
65export function compactPartialHunkOffsets(file: FileDiffMetadata): FileDiffMetadata {
66 if (!file.isPartial) return file;
67​
68 let splitLineStart = 0;
69 let unifiedLineStart = 0;
70 const hunks = file.hunks.map((hunk) => {
71 const compactHunk = {
72 ...hunk,
73 splitLineStart,
74 unifiedLineStart,
75 };
76 splitLineStart += hunk.splitLineCount;
77 unifiedLineStart += hunk.unifiedLineCount;
78 return compactHunk;
79 });
80​
81 return {
82 ...file,
83 hunks,
84 splitLineCount: splitLineStart,
85 unifiedLineCount: unifiedLineStart,
86 ...(file.cacheKey ? { cacheKey: `${file.cacheKey}:compact-partial` } : {}),
87 };
89​
90export function getRenderablePatch(
91 patch: string | undefined,
92 cacheScope = "diff-panel",
93 options: RenderablePatchOptions = {},
94): RenderablePatch | null {
95 if (!patch) return null;
96 const normalizedPatch = patch.trim();
97 if (normalizedPatch.length === 0) return null;
98​
99 try {
100 const parsedPatches = parsePatchFiles(
101 normalizedPatch,
102 buildPatchCacheKey(normalizedPatch, cacheScope),
103 );
104 const files = parsedPatches.flatMap((parsedPatch) =>
105 options.compactPartialHunkOffsets
106 ? parsedPatch.files.map(compactPartialHunkOffsets)
107 : parsedPatch.files,
108 );
109 if (files.length > 0) {
110 return { kind: "files", files };
111 }
112​
113 return {
114 kind: "raw",
115 text: normalizedPatch,
116 reason: "Unsupported diff format. Showing raw patch.",
117 };
118 } catch {
119 return {
120 kind: "raw",
121 text: normalizedPatch,
122 reason: "Failed to parse patch. Showing raw patch.",
123 };
124 }
126​
127export function resolveFileDiffPath(fileDiff: FileDiffMetadata): string {
128 const raw = fileDiff.name ?? fileDiff.prevName ?? "";
129 if (raw.startsWith("a/") || raw.startsWith("b/")) {
130 return raw.slice(2);
131 }
132 return raw;
134​
135export function buildFileDiffRenderKey(fileDiff: FileDiffMetadata): string {
136 return fileDiff.cacheKey ?? `${fileDiff.prevName ?? "none"}:${fileDiff.name}`;
138​
139export function getDiffCollapseIconClassName(fileDiff: FileDiffMetadata): string {
140 switch (fileDiff.type) {
141 case "new":
142 return "text-[var(--diffs-addition-base)]";
143 case "deleted":
144 return "text-[var(--diffs-deletion-base)]";
145 case "change":
146 case "rename-pure":
147 case "rename-changed":
148 return "text-[var(--diffs-modified-base)]";
149 default:
150 return "text-muted-foreground/80";
151 }