tfrere's picture
tfrere HF Staff
Cursor
feat(embed-studio): drafts with Apply to article, compare, data drawer
f7733a1
Raw History Blame Contribute Delete
3.2 kB
/**
* Line diff for the studio's before/after code view (LCS on lines).
* Charts are a few hundred lines, so the quadratic table is fine; above
* MAX_DIFF_LINES on either side we refuse rather than freeze the tab.
*/
export type DiffLine =
| { type: "same" | "add" | "del"; text: string }
| { type: "skip"; count: number };
export const MAX_DIFF_LINES = 3000;
function splitLines(s: string): string[] {
return s === "" ? [] : s.split("\n");
}
export function lineDiff(before: string, after: string): DiffLine[] | null {
const a = splitLines(before);
const b = splitLines(after);
if (a.length > MAX_DIFF_LINES || b.length > MAX_DIFF_LINES) return null;
// Trim the common head and tail first: most iterations touch a few lines.
let head = 0;
while (head < a.length && head < b.length && a[head] === b[head]) head++;
let tail = 0;
while (
tail < a.length - head &&
tail < b.length - head &&
a[a.length - 1 - tail] === b[b.length - 1 - tail]
) tail++;
const ma = a.slice(head, a.length - tail);
const mb = b.slice(head, b.length - tail);
const n = ma.length;
const m = mb.length;
const lcs: Uint32Array[] = Array.from({ length: n + 1 }, () => new Uint32Array(m + 1));
for (let i = n - 1; i >= 0; i--) {
for (let j = m - 1; j >= 0; j--) {
lcs[i][j] = ma[i] === mb[j] ? lcs[i + 1][j + 1] + 1 : Math.max(lcs[i + 1][j], lcs[i][j + 1]);
}
}
const out: DiffLine[] = a.slice(0, head).map((text) => ({ type: "same", text }));
let i = 0;
let j = 0;
while (i < n && j < m) {
if (ma[i] === mb[j]) {
out.push({ type: "same", text: ma[i] });
i++;
j++;
} else if (lcs[i + 1][j] >= lcs[i][j + 1]) {
out.push({ type: "del", text: ma[i++] });
} else {
out.push({ type: "add", text: mb[j++] });
}
}
while (i < n) out.push({ type: "del", text: ma[i++] });
while (j < m) out.push({ type: "add", text: mb[j++] });
for (const text of a.slice(a.length - tail)) out.push({ type: "same", text });
return out;
}
/** Collapse unchanged runs longer than `2 * context` lines into a skip marker. */
export function collapseUnchanged(lines: DiffLine[], context = 3): DiffLine[] {
const out: DiffLine[] = [];
let run: DiffLine[] = [];
const flush = (atStart: boolean, atEnd: boolean) => {
const keepBefore = atStart ? 0 : context;
const keepAfter = atEnd ? 0 : context;
if (run.length > keepBefore + keepAfter + 1) {
out.push(...run.slice(0, keepBefore));
out.push({ type: "skip", count: run.length - keepBefore - keepAfter });
out.push(...run.slice(run.length - keepAfter));
} else {
out.push(...run);
}
run = [];
};
let seenChange = false;
for (const line of lines) {
if (line.type === "same") {
run.push(line);
} else {
flush(!seenChange, false);
seenChange = true;
out.push(line);
}
}
flush(!seenChange, true);
return out;
}
export function diffStats(lines: DiffLine[]): { added: number; removed: number } {
let added = 0;
let removed = 0;
for (const l of lines) {
if (l.type === "add") added++;
else if (l.type === "del") removed++;
}
return { added, removed };
}