diff-model.ts
This file
Path
apps/web/src/features/code/diff-model.ts
349 lines · 9.2 KiB · TypeScript
Last changed by
Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350/** * Parsed by the same parser the drawing uses, so the server's plain-text diff * and the browser's highlighted one hold the same hunks, line for line. No * React and no DOM: both renders read it. */import { parseDiffFromFile } from "@pierre/diffs";import type { FileDiffMetadata } from "@pierre/diffs";import type { ChangedFile } from "@repo/protocol/domain/code";import { Option } from "effect";
export const LONG_FILE = 80;
export const fileDiffOf = ( file: ChangedFile): Option.Option<FileDiffMetadata> => { if ( file.binary || (file.oldText === undefined && file.newText === undefined) ) { return Option.none(); }
const before = file.status === "added" || file.oldText === undefined ? null : { contents: file.oldText, name: file.path };
const after = file.status === "deleted" || file.newText === undefined ? null : { contents: file.newText, name: file.path };
const diff = parseDiffFromFile(before, after);
// Keyed by both blobs, so a file drawn once is never highlighted again. diff.cacheKey = `diff:${file.before ?? "none"}:${file.after ?? "none"}`;
return Option.some(diff);};
const CHANGE_TYPES = { added: "new", deleted: "deleted", modified: "change",} as const satisfies Record<ChangedFile["status"], FileDiffMetadata["type"]>;
export const emptyDiffOf = (file: ChangedFile): FileDiffMetadata => ({ additionLines: [], cacheKey: `diff:${file.before ?? "none"}:${file.after ?? "none"}`, deletionLines: [], hunks: [], isPartial: false, name: file.path, splitLineCount: 0, type: CHANGE_TYPES[file.status], unifiedLineCount: 0,});
export interface Counts { readonly additions: number; readonly deletions: number;}
const NONE: Counts = { additions: 0, deletions: 0 };
export const countsOf = (diff: Option.Option<FileDiffMetadata>): Counts => { if (Option.isNone(diff)) { return NONE; }
let additions = 0; let deletions = 0;
for (const hunk of diff.value.hunks) { additions += hunk.additionLines; deletions += hunk.deletionLines; }
return { additions, deletions };};
export interface DrawnFile { readonly file: ChangedFile; readonly diff: Option.Option<FileDiffMetadata>; readonly counts: Counts; readonly folded: boolean;}
export const treeOrder = (left: string, right: string) => { const a = left.split("/"); const b = right.split("/");
for (let index = 0; index < Math.min(a.length, b.length); index += 1) { const x = a[index] ?? ""; const y = b[index] ?? "";
if (x !== y) { const xFolder = index < a.length - 1; const yFolder = index < b.length - 1;
if (xFolder !== yFolder) { return xFolder ? -1 : 1; }
return x.localeCompare(y, "en"); } }
return a.length - b.length;};
export const drawnFiles = ( files: readonly ChangedFile[]): readonly DrawnFile[] => files .toSorted((a, b) => treeOrder(a.path, b.path)) .map((file) => { const diff = fileDiffOf(file); const counts = countsOf(diff);
return { counts, diff, file, folded: file.status !== "modified" && counts.additions + counts.deletions > LONG_FILE, }; });
export const isTestPath = (path: string) => /(?:^|\/)(?:tests?|__tests__|spec)\//u.test(path) || /\.(?:test|spec)\.[^/]+$/u.test(path);
export const globTest = (glob: string): ((path: string) => boolean) => { const pattern = glob .replaceAll(/[.+^${}()|[\]\\]/gu, "\\$&") .replaceAll("**/", "\u0000") .replaceAll("**", "\u0001") .replaceAll("*", "[^/]*") .replaceAll("?", "[^/]") .replaceAll("\u0000", "(?:.*/)?") .replaceAll("\u0001", ".*");
const whole = new RegExp(`^${pattern}(?:/.*)?$`, "u");
return (path) => whole.test(path);};
export interface StatsPart extends Counts { readonly label: "other" | "territory" | "tests"; readonly files: number;}
export interface DiffStats extends Counts { readonly files: number; readonly parts: readonly StatsPart[];}
export const statsOf = ( files: readonly DrawnFile[], territory: readonly string[] = []): DiffStats => { const inTerritory = territory.map(globTest);
const labelOf = (path: string): StatsPart["label"] => { if (isTestPath(path)) { return "tests"; }
return inTerritory.some((test) => test(path)) ? "territory" : "other"; };
const parts = (["territory", "tests", "other"] as const).flatMap( (label): readonly StatsPart[] => { const mine = files.filter(({ file }) => labelOf(file.path) === label);
return mine.length === 0 ? [] : [ { additions: mine.reduce( (sum, { counts }) => sum + counts.additions, 0 ), deletions: mine.reduce( (sum, { counts }) => sum + counts.deletions, 0 ), files: mine.length, label, }, ]; } );
return { additions: parts.reduce((sum, part) => sum + part.additions, 0), deletions: parts.reduce((sum, part) => sum + part.deletions, 0), files: files.length, parts, };};
export type PlainRow = | { readonly kind: "separator"; readonly lines: number } | { readonly kind: "context" | "addition" | "deletion"; readonly number: number; readonly text: string; };
const bare = (line: string | undefined) => (line ?? "").replace(/\r?\n$/u, "");
export const unifiedRows = (diff: FileDiffMetadata): readonly PlainRow[] => { const rows: PlainRow[] = []; let lastAddition = 0;
for (const hunk of diff.hunks) { if (hunk.collapsedBefore > 0) { rows.push({ kind: "separator", lines: hunk.collapsedBefore }); }
for (const content of hunk.hunkContent) { if (content.type === "context") { for (let line = 0; line < content.lines; line += 1) { const index = content.additionLineIndex + line;
rows.push({ kind: "context", number: index + 1, text: bare(diff.additionLines[index]), }); } } else { for (let line = 0; line < content.deletions; line += 1) { const index = content.deletionLineIndex + line;
rows.push({ kind: "deletion", number: index + 1, text: bare(diff.deletionLines[index]), }); }
for (let line = 0; line < content.additions; line += 1) { const index = content.additionLineIndex + line;
rows.push({ kind: "addition", number: index + 1, text: bare(diff.additionLines[index]), }); } } }
lastAddition = hunk.additionStart - 1 + hunk.additionCount; }
const after = diff.isPartial ? 0 : diff.additionLines.length - lastAddition;
if (diff.hunks.length > 0 && after > 0 && diff.type !== "deleted") { rows.push({ kind: "separator", lines: after }); }
return rows;};
type Line = Extract<PlainRow, { readonly number: number }>;
export type SplitRow = | { readonly kind: "separator"; readonly lines: number } | { readonly kind: "pair"; readonly left?: Line; readonly right?: Line; };
export const splitRows = (diff: FileDiffMetadata): readonly SplitRow[] => { const rows: SplitRow[] = []; let lastAddition = 0;
for (const hunk of diff.hunks) { if (hunk.collapsedBefore > 0) { rows.push({ kind: "separator", lines: hunk.collapsedBefore }); }
for (const content of hunk.hunkContent) { if (content.type === "context") { for (let line = 0; line < content.lines; line += 1) { const old = content.deletionLineIndex + line; const index = content.additionLineIndex + line;
rows.push({ kind: "pair", left: { kind: "context", number: old + 1, text: bare(diff.deletionLines[old]), }, right: { kind: "context", number: index + 1, text: bare(diff.additionLines[index]), }, }); } } else { const span = Math.max(content.deletions, content.additions);
for (let line = 0; line < span; line += 1) { const old = content.deletionLineIndex + line; const index = content.additionLineIndex + line;
const left: Line = { kind: "deletion", number: old + 1, text: bare(diff.deletionLines[old]), };
const right: Line = { kind: "addition", number: index + 1, text: bare(diff.additionLines[index]), };
if (line < content.deletions && line < content.additions) { rows.push({ kind: "pair", left, right }); } else if (line < content.deletions) { rows.push({ kind: "pair", left }); } else { rows.push({ kind: "pair", right }); } } } }
lastAddition = hunk.additionStart - 1 + hunk.additionCount; }
const after = diff.isPartial ? 0 : diff.additionLines.length - lastAddition;
if (diff.hunks.length > 0 && after > 0 && diff.type !== "deleted") { rows.push({ kind: "separator", lines: after }); }
return rows;};