Lines 4–10 at 318 (were 4–10 at 166)
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 26–58 at 318 (were 26–33 at 166)
2626/** Exact read surface used to build atoms; membership requires point lookup only. */ 2727export type AtomBuildView = Pick<View, "graph" | "lines" | "sequence" | "sequenceIndex" | "parentOf"> & { 2828 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), 3156/** Replace the one exact occurrence of `old` in `file` with `new`. */ 3257export interface TextEdit { 3358 readonly file: string; Lines 157–163 at 318 (were 132–137 at 166)
134159function build(view: AtomBuildView, input: AtomInput, allowEmpty: boolean): Atom { 160+ view = cachedBuildView(view); 135161 const graph = view.graph; 136162 const id = graph.nextId(); 137163 if (typeof input.author !== "string" || typeof input.message !== "string") { Lines 178–191 at 318 (were 152–160 at 166)
152178 const emptiedBy = new Set<AtomId>(); 154180 for (const region of regions) { 155− const visible = view.lines(region.file);
156− const oldLines = visible.slice(region.start, region.end).map((l) => l.text);
157− 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)) { 158189 const start = region.start + hunk.start; 159190 const end = region.start + hunk.end; 160191 let parent: NodeId; Lines 316–322 at 318 (were 285–291 at 166)
285316 if (typeof path !== "string" || path === "" || /[\n\r]/.test(path)) 286317 throw new EngineError("bad-edit", `bad file path ${JSON.stringify(path)}`); 288− const visible = view.lines(move.file);
319+ const visible = buildingLines(view, move.file); 289320 const { start, end, at } = move; 290321 if (!Number.isInteger(start) || !Number.isInteger(end) || start < 0 || end > visible.length || start >= end) { 291322 throw new EngineError( Lines 324–330 at 318 (were 293–299 at 166)
293324 `${move.file} lines [${start}, ${end}) are not a non-empty range of its ${visible.length} visible lines`, 296− const dest = view.lines(move.to);
327+ const dest = buildingLines(view, move.to); 297328 if (!Number.isInteger(at) || at < 0 || at > dest.length) { 298329 throw new EngineError("bad-range", `${move.to} has no place ${at} (it has ${dest.length} visible lines)`); Lines 427–433 at 318 (were 396–402 at 166)
398429function rangeLines(view: AtomBuildView, range: LineRange): LineId[] { 399− const visible = view.lines(range.file);
430+ const visible = buildingLines(view, range.file); 401432 !Number.isInteger(range.start) || 402433 !Number.isInteger(range.end) || Lines 496–502 at 318 (were 465–471 at 166)
465496 throw new EngineError("bad-edit", "a text edit needs old and new strings"); 467498 const file = edit.file; 468− const visible = view.lines(file);
499+ const visible = buildingLines(view, file); 469500 const oldText = normalizeText(edit.old); 470501 const newText = normalizeText(edit.new); 471502 if (oldText === "") { Lines 508–515 at 318 (were 477–483 at 166)
478509 return { file, start: 0, end: 0, lines: splitLines(newText), mode, order }; 480− const text = joinLines(visible.map((l) => l.text));
511+ const texts = visible.map((line) => line.text); 512+ const text = joinLines(texts); 481513 const at = text.indexOf(oldText); 482514 if (at < 0) throw new EngineError("no-match", `old text not found in ${file}`); 483515 if (text.indexOf(oldText, at + 1) >= 0) { Lines 520–528 at 318 (were 488–496 at 166)
489521 const starts: number[] = []; 491− for (const line of visible) {
523+ for (const text of texts) { 493− pos += line.text.length + 1;
525+ pos += text.length + 1; 495527 const lineAt = (offset: number): number => { Lines 538–549 at 318 (were 506–517 at 166)
506538 const startLine = lineAt(at); 507539 let endLine = lineAt(end - 1); 508540 const lineStart = starts[startLine]!; 509− const lineEnd = starts[endLine]! + visible[endLine]!.text.length + 1;
541+ const lineEnd = starts[endLine]! + texts[endLine]!.length + 1; 510542 let replacement = text.slice(lineStart, at) + newText + text.slice(end, lineEnd); 511543 if (replacement !== "" && !replacement.endsWith("\n")) { 512544 if (endLine + 1 < visible.length) { 514− replacement += `${visible[endLine]!.text}\n`;
546+ replacement += `${texts[endLine]}\n`; 516548 replacement += "\n"; Lines 552–558 at 318 (were 520–526 at 166)
522554function matchSplice(view: AtomBuildView, edit: SpliceEdit, mode: Mode, order: number): Region { 523− const count = view.lines(edit.file).length;
555+ const count = buildingLines(view, edit.file).length; 525557 !Number.isInteger(edit.start) || 526558 !Number.isInteger(edit.end) ||