CI & Build / Python lint (push) Successful in 9s
CI & Build / Plugin hooks (push) Successful in 22s
CI & Build / TypeScript typecheck (push) Successful in 1m0s
CI & Build / integration (push) Successful in 1m55s
CI & Build / Python tests (push) Successful in 2m20s
CI & Build / Build & push image (push) Successful in 1m7s
Components, stores, api clients, router, types and shared CSS now state what each piece is and why, without the milestone that introduced it. diff.ts names its consumers instead of the copy history; stores/notes and tasks state their current scope instead of the cleanup that set it; theme.css states the token practice instead of the sweep that established it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
47 lines
1.8 KiB
TypeScript
47 lines
1.8 KiB
TypeScript
/**
|
|
* Line diff — one copy, for every surface that shows what changed.
|
|
*
|
|
* WHY THIS FILE EXISTS. The one LCS diff walk, shared by `useAssist.ts`,
|
|
* `HistoryPanel.vue`, `VersionHistorySection.vue` and the rule edit history.
|
|
* A copy per consumer means a fix has to be found in N places by someone who
|
|
* does not know N.
|
|
*/
|
|
|
|
export interface DiffLine {
|
|
type: "equal" | "delete" | "insert";
|
|
text: string;
|
|
}
|
|
|
|
/**
|
|
* Diff `a` against `b`, line by line.
|
|
*
|
|
* `delete` lines come from `a`, `insert` lines from `b` — so the caller
|
|
* decides which side reads as "before" by which argument it passes. Every
|
|
* caller here passes the CURRENT text as `a` and the older text as `b`, so a
|
|
* deletion is what the old version had and an insertion is what replaced it.
|
|
*
|
|
* O(m·n) in time and memory: fine for a note or a rule, and deliberately not
|
|
* generalised further, since nothing here diffs a file of thousands of lines.
|
|
*/
|
|
export function computeDiff(a: string, b: string): DiffLine[] {
|
|
const aLines = a.split("\n");
|
|
const bLines = b.split("\n");
|
|
const m = aLines.length, n = bLines.length;
|
|
const dp: number[][] = Array.from({ length: m + 1 }, () => new Array(n + 1).fill(0));
|
|
for (let i = m - 1; i >= 0; i--)
|
|
for (let j = n - 1; j >= 0; j--)
|
|
dp[i][j] = aLines[i] === bLines[j]
|
|
? dp[i + 1][j + 1] + 1
|
|
: Math.max(dp[i + 1][j], dp[i][j + 1]);
|
|
const result: DiffLine[] = [];
|
|
let i = 0, j = 0;
|
|
while (i < m && j < n) {
|
|
if (aLines[i] === bLines[j]) { result.push({ type: "equal", text: aLines[i++] }); j++; }
|
|
else if (dp[i + 1][j] >= dp[i][j + 1]) result.push({ type: "delete", text: aLines[i++] });
|
|
else result.push({ type: "insert", text: bLines[j++] });
|
|
}
|
|
while (i < m) result.push({ type: "delete", text: aLines[i++] });
|
|
while (j < n) result.push({ type: "insert", text: bLines[j++] });
|
|
return result;
|
|
}
|