Lines 4–10 at 318 (were 4–10 at 136)
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 136)
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 136)
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 136)
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 136)
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 136)
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 136)
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 377–383 at 318 (were 341–347 at 136)
341377 * the semantic contract. A missing tip still implies its required ancestors. 343379function deriveDeps( 344− view: View,
380+ view: AtomBuildView, 346382 writes: readonly Key[]; 347383 reads: readonly Key[]; Lines 426–433 at 318 (were 390–397 at 136)
393−function rangeLines(view: View, range: LineRange): LineId[] {
394− const visible = view.lines(range.file);
429+function rangeLines(view: AtomBuildView, range: LineRange): LineId[] { 430+ const visible = buildingLines(view, range.file); 396432 !Number.isInteger(range.start) || 397433 !Number.isInteger(range.end) || Lines 443–449 at 318 (were 407–413 at 136)
407443 return visible.slice(range.start, range.end).map((l) => l.id); 410−function uniqueAtoms(view: View, ids: readonly AtomId[], what: string): AtomId[] {
446+function uniqueAtoms(view: AtomBuildView, ids: readonly AtomId[], what: string): AtomId[] { 411447 const out: AtomId[] = []; 412448 for (const id of ids) { 413449 if (!view.graph.has(id)) throw new EngineError("unknown-atom", `${what} names unknown atom ${id}`); Lines 455–461 at 318 (were 419–425 at 136)
420456// Matching edits --------------------------------------------------------------- 422−function resolveRegions(view: View, edits: readonly EditInput[]): Region[] {
458+function resolveRegions(view: AtomBuildView, edits: readonly EditInput[]): Region[] { 423459 const regions = edits.map((edit, order) => { 424460 const mode = edit.mode ?? "both"; 425461 if (!MODES.includes(mode)) throw new EngineError("bad-edit", `mode must be one of ${MODES.join(", ")}`); Lines 491–502 at 318 (were 455–466 at 136)
455491 * whole lines. If the replacement would leave a line without its newline, the 456492 * next line joins the region (that is what the plain-text edit does). 458−function matchText(view: View, edit: TextEdit, mode: Mode, order: number): Region {
494+function matchText(view: AtomBuildView, edit: TextEdit, mode: Mode, order: number): Region { 459495 if (typeof edit.old !== "string" || typeof edit.new !== "string") { 460496 throw new EngineError("bad-edit", "a text edit needs old and new strings"); 462498 const file = edit.file; 463− const visible = view.lines(file);
499+ const visible = buildingLines(view, file); 464500 const oldText = normalizeText(edit.old); 465501 const newText = normalizeText(edit.new); 466502 if (oldText === "") { Lines 508–515 at 318 (were 472–478 at 136)
473509 return { file, start: 0, end: 0, lines: splitLines(newText), mode, order }; 475− const text = joinLines(visible.map((l) => l.text));
511+ const texts = visible.map((line) => line.text); 512+ const text = joinLines(texts); 476513 const at = text.indexOf(oldText); 477514 if (at < 0) throw new EngineError("no-match", `old text not found in ${file}`); 478515 if (text.indexOf(oldText, at + 1) >= 0) { Lines 520–528 at 318 (were 483–491 at 136)
484521 const starts: number[] = []; 486− for (const line of visible) {
523+ for (const text of texts) { 488− pos += line.text.length + 1;
525+ pos += text.length + 1; 490527 const lineAt = (offset: number): number => { Lines 538–549 at 318 (were 501–512 at 136)
501538 const startLine = lineAt(at); 502539 let endLine = lineAt(end - 1); 503540 const lineStart = starts[startLine]!; 504− const lineEnd = starts[endLine]! + visible[endLine]!.text.length + 1;
541+ const lineEnd = starts[endLine]! + texts[endLine]!.length + 1; 505542 let replacement = text.slice(lineStart, at) + newText + text.slice(end, lineEnd); 506543 if (replacement !== "" && !replacement.endsWith("\n")) { 507544 if (endLine + 1 < visible.length) { 509− replacement += `${visible[endLine]!.text}\n`;
546+ replacement += `${texts[endLine]}\n`; 511548 replacement += "\n"; Lines 551–558 at 318 (were 514–521 at 136)
514551 return { file, start: startLine, end: endLine + 1, lines: splitLines(replacement), mode, order }; 517−function matchSplice(view: View, edit: SpliceEdit, mode: Mode, order: number): Region {
518− 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; 520557 !Number.isInteger(edit.start) || 521558 !Number.isInteger(edit.end) ||