Lines 4–15 at 318 (were 4–15 at 3)
7− diffLines,
12− type View,
1313} from "../../src/engine/index.ts"; 1414import type { AgentState, Entry, Meta, Mode } from "./project-types.ts"; Lines 17–23 at 318 (were 17–23 at 3)
1717export interface RecordFileHost { 20− view: View,
2121 input: { edits: readonly EditInput[]; attrs?: readonly AttrSet[] }, Lines 34–40 at 318 (were 34–40 at 3)
3434export function recordFile( 3535 host: RecordFileHost, 37− view: View,
3939 text: string | null | undefined, Lines 44–122 at 318 (were 44–82 at 3)
4545 hunks?: readonly Hunk[], 47− const was = view.attrs(path);
48− const exists = view.hasFile(path);
4947 const lines = text === undefined ? undefined : text === null ? "" : text; 5048 const edits: SpliceEdit[] = 51− lines === undefined || view.text(path) === lines
49+ lines === undefined || (hunks !== undefined && sameProjectedText(view, path, lines)) 53− : (
54− hunks ??
55− diffLines(
56− view.lines(path).map((l) => l.text),
57− splitLines(lines),
58− )
59− ).map((h) => ({ file: path, start: h.start, end: h.end, lines: h.lines }));
51+ : (hunks ?? diffChunkedLines(view.lineChunks(path), splitLines(lines))).map((h) => ({ 57+ return recordFileChanges( 62+ text === undefined ? "unchanged" : text === null ? "missing" : text === "" ? "empty" : "present", 72+/** Apply a precomputed exact text diff and the normal file attribute rules. */ 73+export function recordFileChanges( 74+ host: RecordFileHost, 78+ state: "unchanged" | "missing" | "empty" | "present", 79+ edits: readonly SpliceEdit[], 86+ const was = view.attrs(path); 87+ const exists = view.hasFile(path); 6089 const attrs: AttrSet[] = []; 6190 const set = (name: AttrName, now: string | null, value: string | null): void => { 6291 if (now !== value) attrs.push({ file: path, name, value }); 64− const link = mode.link !== undefined ? mode.link : text === null ? null : was.link;
65− if (text === null) {
93+ const link = mode.link !== undefined ? mode.link : state === "missing" ? null : was.link; 94+ if (state === "missing") { 6695 // Gone: no lines, and nothing keeps it. 6796 set("exists", was.exists ? "1" : null, null); 6897 set("exec", was.exec ? "1" : null, null); 69− } else if (text === "" && link === null && blob === undefined) {
98+ } else if (state === "empty" && link === null && blob === undefined) { 7099 // An empty file is kept by its exists attribute. 71100 set("exists", was.exists ? "1" : null, "1"); 73102 // A binary file is its blob and no lines; text, or no file, clears the blob. 74103 if (blob !== undefined) set("blob", was.blob, blob); 75− else if (text !== undefined) set("blob", was.blob, null);
76− if (mode.exec !== undefined && text !== null) set("exec", was.exec ? "1" : null, mode.exec ? "1" : null);
104+ else if (state !== "unchanged") set("blob", was.blob, null); 105+ if (mode.exec !== undefined && state !== "missing") set("exec", was.exec ? "1" : null, mode.exec ? "1" : null); 77106 set("link", was.link, link); 78107 if (edits.length === 0 && attrs.length === 0) return false; 79− if (!exists && text === null && attrs.length === 0) return false;
108+ if (!exists && state === "missing" && attrs.length === 0) return false; 80109 host.addDraft(agent, view, { edits, attrs }, time, meta, entries); 113+/** Compare canonical projected text without joining or caching old lines. */ 114+function sameProjectedText(view: ReadView, path: string, text: string): boolean { 116+ for (const line of view.visibleLines(path)) { 117+ if (!text.startsWith(line.text, offset)) return false; 118+ offset += line.text.length; 119+ if (text[offset++] !== "\n") return false; 121+ return offset === text.length;