Lines 4–10 at 318 (were 4–10 at 33)
55import { Graph } from "./graph.ts"; 66import { attrKey, gapKey, isStartNode, lineKey, parseKey, posKey, startNode, writerOf } from "./keys.ts"; 7−import { diffLines, joinLines, normalizeText, splitLines } from "./text.ts";
7+import { diffChunkedLines, joinLines, normalizeText, splitLines } from "./text.ts"; Lines 23–58 at 318 (were 23–28 at 33)
2424import { View } from "./view.ts"; 26+/** Exact read surface used to build atoms; membership requires point lookup only. */ 27+export type AtomBuildView = Pick<View, "graph" | "lines" | "sequence" | "sequenceIndex" | "parentOf"> & { 28+ readonly set: Pick<ReadonlySet<AtomId>, "has">; 29+ buildLines?: View["buildLines"]; 32+function buildingLines(view: AtomBuildView, path: string) { 33+ return view.buildLines ? view.buildLines(path) : view.lines(path); 36+/** Cache only within one immutable build; never retain handles across writes. */ 37+function cachedBuildView(view: AtomBuildView): AtomBuildView { 38+ const files = new Map<string, ReturnType<View["lines"]>>(); 43+ let lines = files.get(path); 45+ lines = buildingLines(view, path); 46+ files.set(path, lines); 50+ sequence: (path) => view.sequence(path), 51+ sequenceIndex: (path, line) => view.sequenceIndex(path, line), 52+ parentOf: (line) => view.parentOf(line), 2656/** Replace the one exact occurrence of `old` in `file` with `new`. */ 2757export interface TextEdit { 2858 readonly file: string; Lines 138–144 at 318 (were 108–114 at 33)
108138 * the graph's next id; accept it with `graph.add(atom)` and put it in a set. 109139 * Its dependencies are derived from what it touched in the view (see deriveDeps). 111−export function buildAtom(view: View, input: AtomInput): Atom {
141+export function buildAtom(view: AtomBuildView, input: AtomInput): Atom { 112142 return build(view, input, false); Lines 156–163 at 318 (were 126–132 at 33)
126156 return build(view, { ...meta, edits, attrs }, true); 129−function build(view: View, input: AtomInput, allowEmpty: boolean): Atom {
159+function build(view: AtomBuildView, input: AtomInput, allowEmpty: boolean): Atom { 160+ view = cachedBuildView(view); 130161 const graph = view.graph; 131162 const id = graph.nextId(); 132163 if (typeof input.author !== "string" || typeof input.message !== "string") { Lines 178–191 at 318 (were 147–155 at 33)
147178 const emptiedBy = new Set<AtomId>(); 149180 for (const region of regions) { 150− const visible = view.lines(region.file);
151− const oldLines = visible.slice(region.start, region.end).map((l) => l.text);
152− for (const hunk of diffLines(oldLines, region.lines)) {
181+ const visible = buildingLines(view, region.file); 182+ function* oldLines() { 183+ for (let i = region.start; i < region.end; i++) { 184+ const line = visible[i]!; 185+ yield () => (line.textChunks ? line.textChunks() : [line.text]); 188+ for (const hunk of diffChunkedLines(oldLines(), region.lines)) { 153189 const start = region.start + hunk.start; 154190 const end = region.start + hunk.end; 155191 let parent: NodeId; Lines 311–322 at 318 (were 275–286 at 33)
275311 * lines into each other collide in all but the rarest cases (the view breaks 276312 * any loop that is left). 278−function planMove(view: View, move: MoveInput, writes: Set<Key>, reads: Set<Key>): Relocation[] {
314+function planMove(view: AtomBuildView, move: MoveInput, writes: Set<Key>, reads: Set<Key>): Relocation[] { 279315 for (const path of [move.file, move.to]) { 280316 if (typeof path !== "string" || path === "" || /[\n\r]/.test(path)) 281317 throw new EngineError("bad-edit", `bad file path ${JSON.stringify(path)}`); 283− const visible = view.lines(move.file);
319+ const visible = buildingLines(view, move.file); 284320 const { start, end, at } = move; 285321 if (!Number.isInteger(start) || !Number.isInteger(end) || start < 0 || end > visible.length || start >= end) { 286322 throw new EngineError( Lines 324–330 at 318 (were 288–294 at 33)
288324 `${move.file} lines [${start}, ${end}) are not a non-empty range of its ${visible.length} visible lines`, 291− const dest = view.lines(move.to);
327+ const dest = buildingLines(view, move.to); 292328 if (!Number.isInteger(at) || at < 0 || at > dest.length) { 293329 throw new EngineError("bad-range", `${move.to} has no place ${at} (it has ${dest.length} visible lines)`); Lines 369–383 at 318 (were 333–342 at 33)
333369 * - creating a file over a deleted one: the atoms that deleted its lines; 334370 * - needs and resolves. 335371 * Nothing else from the view: not the line below an insertion, not the 336− * tombstones around it.
372+ * tombstones around it. New atoms encode implicit dependencies as maximal 373+ * tips: ancestors reachable through another tip are omitted from direct deps. 374+ * Explicit needs/resolves always remain direct. Existing atoms are not rewritten. 375+ * Direct-edge diagnostics (including why/Deps trailers and missingDeps) therefore 376+ * show fewer redundant edges; transitive closure and dependency ordering remain 377+ * the semantic contract. A missing tip still implies its required ancestors. 338379function deriveDeps( 339− view: View,
380+ view: AtomBuildView, 341382 writes: readonly Key[]; 342383 reads: readonly Key[]; Lines 388–417 at 318 (were 347–362 at 33)
349390 const graph = view.graph; 350− const deps = new Set<AtomId>(parts.emptiedBy);
391+ // New atoms retain maximal implicit dependencies. Their transitive closure is 392+ // unchanged; explicit needs/resolves remain direct edges below. 393+ const deps = new Set<AtomId>(); 394+ const addImplicit = (candidate: AtomId): void => { 395+ if (deps.has(candidate)) return; 396+ const candidateClock = graph.lamport(candidate); 397+ for (const previous of deps) { 398+ const previousClock = graph.lamport(previous); 399+ // Equal Lamport clocks cannot reach each other; check only the possible direction. 400+ if (previousClock > candidateClock && graph.dependsOn(previous, candidate)) return; 401+ if (candidateClock > previousClock && graph.dependsOn(candidate, previous)) deps.delete(previous); 403+ deps.add(candidate); 405+ for (const id of parts.emptiedBy) addImplicit(id); 351406 for (const key of [...parts.writes, ...parts.reads, ...parts.holds]) { 352407 const { kind, node } = parseKey(key); 353408 // An attribute builds on whoever set it last in the view, the way an insertion builds on a gap's writers; 354409 // a move builds on the earlier moves of its lines. 355− if (kind !== "attr" && !isStartNode(node)) deps.add(writerOf(node));
356− if (kind !== "line") for (const writer of graph.writersOf(key)) if (view.set.has(writer)) deps.add(writer);
410+ if (kind !== "attr" && !isStartNode(node)) addImplicit(writerOf(node)); 411+ if (kind !== "line") for (const writer of graph.eachWriterOf(key)) if (view.set.has(writer)) addImplicit(writer); 358413 for (const key of parts.writes) { 359− for (const reader of graph.readersOf(key)) if (view.set.has(reader)) deps.add(reader);
414+ for (const reader of graph.eachReaderOf(key)) if (view.set.has(reader)) addImplicit(reader); 361416 for (const id of parts.needs) deps.add(id); 362417 for (const id of parts.resolves) deps.add(id); Lines 426–433 at 318 (were 371–378 at 33)
374−function rangeLines(view: View, range: LineRange): LineId[] {
375− const visible = view.lines(range.file);
429+function rangeLines(view: AtomBuildView, range: LineRange): LineId[] { 430+ const visible = buildingLines(view, range.file); 377432 !Number.isInteger(range.start) || 378433 !Number.isInteger(range.end) || Lines 443–449 at 318 (were 388–394 at 33)
388443 return visible.slice(range.start, range.end).map((l) => l.id); 391−function uniqueAtoms(view: View, ids: readonly AtomId[], what: string): AtomId[] {
446+function uniqueAtoms(view: AtomBuildView, ids: readonly AtomId[], what: string): AtomId[] { 392447 const out: AtomId[] = []; 393448 for (const id of ids) { 394449 if (!view.graph.has(id)) throw new EngineError("unknown-atom", `${what} names unknown atom ${id}`); Lines 455–461 at 318 (were 400–406 at 33)
401456// Matching edits --------------------------------------------------------------- 403−function resolveRegions(view: View, edits: readonly EditInput[]): Region[] {
458+function resolveRegions(view: AtomBuildView, edits: readonly EditInput[]): Region[] { 404459 const regions = edits.map((edit, order) => { 405460 const mode = edit.mode ?? "both"; 406461 if (!MODES.includes(mode)) throw new EngineError("bad-edit", `mode must be one of ${MODES.join(", ")}`); Lines 491–502 at 318 (were 436–447 at 33)
436491 * whole lines. If the replacement would leave a line without its newline, the 437492 * next line joins the region (that is what the plain-text edit does). 439−function matchText(view: View, edit: TextEdit, mode: Mode, order: number): Region {
494+function matchText(view: AtomBuildView, edit: TextEdit, mode: Mode, order: number): Region { 440495 if (typeof edit.old !== "string" || typeof edit.new !== "string") { 441496 throw new EngineError("bad-edit", "a text edit needs old and new strings"); 443498 const file = edit.file; 444− const visible = view.lines(file);
499+ const visible = buildingLines(view, file); 445500 const oldText = normalizeText(edit.old); 446501 const newText = normalizeText(edit.new); 447502 if (oldText === "") { Lines 508–515 at 318 (were 453–459 at 33)
454509 return { file, start: 0, end: 0, lines: splitLines(newText), mode, order }; 456− const text = joinLines(visible.map((l) => l.text));
511+ const texts = visible.map((line) => line.text); 512+ const text = joinLines(texts); 457513 const at = text.indexOf(oldText); 458514 if (at < 0) throw new EngineError("no-match", `old text not found in ${file}`); 459515 if (text.indexOf(oldText, at + 1) >= 0) { Lines 520–528 at 318 (were 464–472 at 33)
465521 const starts: number[] = []; 467− for (const line of visible) {
523+ for (const text of texts) { 469− pos += line.text.length + 1;
525+ pos += text.length + 1; 471527 const lineAt = (offset: number): number => { Lines 538–549 at 318 (were 482–493 at 33)
482538 const startLine = lineAt(at); 483539 let endLine = lineAt(end - 1); 484540 const lineStart = starts[startLine]!; 485− const lineEnd = starts[endLine]! + visible[endLine]!.text.length + 1;
541+ const lineEnd = starts[endLine]! + texts[endLine]!.length + 1; 486542 let replacement = text.slice(lineStart, at) + newText + text.slice(end, lineEnd); 487543 if (replacement !== "" && !replacement.endsWith("\n")) { 488544 if (endLine + 1 < visible.length) { 490− replacement += visible[endLine]!.text + "\n";
546+ replacement += `${texts[endLine]}\n`; 492548 replacement += "\n"; Lines 551–558 at 318 (were 495–502 at 33)
495551 return { file, start: startLine, end: endLine + 1, lines: splitLines(replacement), mode, order }; 498−function matchSplice(view: View, edit: SpliceEdit, mode: Mode, order: number): Region {
499− const count = view.lines(edit.file).length;
554+function matchSplice(view: AtomBuildView, edit: SpliceEdit, mode: Mode, order: number): Region { 555+ const count = buildingLines(view, edit.file).length; 501557 !Number.isInteger(edit.start) || 502558 !Number.isInteger(edit.end) ||