{"draft":null,"from":166,"to":318,"files":[{"path":"src/engine/build.ts","added":46,"removed":14,"diff":"--- a/src/engine/build.ts\n+++ b/src/engine/build.ts\n@@ -4,7 +4,7 @@\n \n import { Graph } from \"./graph.ts\";\n import { attrKey, gapKey, isStartNode, lineKey, parseKey, posKey, startNode, writerOf } from \"./keys.ts\";\n-import { diffLines, joinLines, normalizeText, splitLines } from \"./text.ts\";\n+import { diffChunkedLines, joinLines, normalizeText, splitLines } from \"./text.ts\";\n import {\n \tATTR_NAMES,\n \ttype Atom,\n@@ -26,8 +26,33 @@\n /** Exact read surface used to build atoms; membership requires point lookup only. */\n export type AtomBuildView = Pick<View, \"graph\" | \"lines\" | \"sequence\" | \"sequenceIndex\" | \"parentOf\"> & {\n \treadonly set: Pick<ReadonlySet<AtomId>, \"has\">;\n+\tbuildLines?: View[\"buildLines\"];\n };\n \n+function buildingLines(view: AtomBuildView, path: string) {\n+\treturn view.buildLines ? view.buildLines(path) : view.lines(path);\n+}\n+\n+/** Cache only within one immutable build; never retain handles across writes. */\n+function cachedBuildView(view: AtomBuildView): AtomBuildView {\n+\tconst files = new Map<string, ReturnType<View[\"lines\"]>>();\n+\treturn {\n+\t\tgraph: view.graph,\n+\t\tset: view.set,\n+\t\tlines(path) {\n+\t\t\tlet lines = files.get(path);\n+\t\t\tif (!lines) {\n+\t\t\t\tlines = buildingLines(view, path);\n+\t\t\t\tfiles.set(path, lines);\n+\t\t\t}\n+\t\t\treturn lines;\n+\t\t},\n+\t\tsequence: (path) => view.sequence(path),\n+\t\tsequenceIndex: (path, line) => view.sequenceIndex(path, line),\n+\t\tparentOf: (line) => view.parentOf(line),\n+\t};\n+}\n+\n /** Replace the one exact occurrence of `old` in `file` with `new`. */\n export interface TextEdit {\n \treadonly file: string;\n@@ -132,6 +157,7 @@\n }\n \n function build(view: AtomBuildView, input: AtomInput, allowEmpty: boolean): Atom {\n+\tview = cachedBuildView(view);\n \tconst graph = view.graph;\n \tconst id = graph.nextId();\n \tif (typeof input.author !== \"string\" || typeof input.message !== \"string\") {\n@@ -152,9 +178,14 @@\n \tconst emptiedBy = new Set<AtomId>();\n \n \tfor (const region of regions) {\n-\t\tconst visible = view.lines(region.file);\n-\t\tconst oldLines = visible.slice(region.start, region.end).map((l) => l.text);\n-\t\tfor (const hunk of diffLines(oldLines, region.lines)) {\n+\t\tconst visible = buildingLines(view, region.file);\n+\t\tfunction* oldLines() {\n+\t\t\tfor (let i = region.start; i < region.end; i++) {\n+\t\t\t\tconst line = visible[i]!;\n+\t\t\t\tyield () => (line.textChunks ? line.textChunks() : [line.text]);\n+\t\t\t}\n+\t\t}\n+\t\tfor (const hunk of diffChunkedLines(oldLines(), region.lines)) {\n \t\t\tconst start = region.start + hunk.start;\n \t\t\tconst end = region.start + hunk.end;\n \t\t\tlet parent: NodeId;\n@@ -285,7 +316,7 @@\n \t\tif (typeof path !== \"string\" || path === \"\" || /[\\n\\r]/.test(path))\n \t\t\tthrow new EngineError(\"bad-edit\", `bad file path ${JSON.stringify(path)}`);\n \t}\n-\tconst visible = view.lines(move.file);\n+\tconst visible = buildingLines(view, move.file);\n \tconst { start, end, at } = move;\n \tif (!Number.isInteger(start) || !Number.isInteger(end) || start < 0 || end > visible.length || start >= end) {\n \t\tthrow new EngineError(\n@@ -293,7 +324,7 @@\n \t\t\t`${move.file} lines [${start}, ${end}) are not a non-empty range of its ${visible.length} visible lines`,\n \t\t);\n \t}\n-\tconst dest = view.lines(move.to);\n+\tconst dest = buildingLines(view, move.to);\n \tif (!Number.isInteger(at) || at < 0 || at > dest.length) {\n \t\tthrow new EngineError(\"bad-range\", `${move.to} has no place ${at} (it has ${dest.length} visible lines)`);\n \t}\n@@ -396,7 +427,7 @@\n }\n \n function rangeLines(view: AtomBuildView, range: LineRange): LineId[] {\n-\tconst visible = view.lines(range.file);\n+\tconst visible = buildingLines(view, range.file);\n \tif (\n \t\t!Number.isInteger(range.start) ||\n \t\t!Number.isInteger(range.end) ||\n@@ -465,7 +496,7 @@\n \t\tthrow new EngineError(\"bad-edit\", \"a text edit needs old and new strings\");\n \t}\n \tconst file = edit.file;\n-\tconst visible = view.lines(file);\n+\tconst visible = buildingLines(view, file);\n \tconst oldText = normalizeText(edit.old);\n \tconst newText = normalizeText(edit.new);\n \tif (oldText === \"\") {\n@@ -477,7 +508,8 @@\n \t\t}\n \t\treturn { file, start: 0, end: 0, lines: splitLines(newText), mode, order };\n \t}\n-\tconst text = joinLines(visible.map((l) => l.text));\n+\tconst texts = visible.map((line) => line.text);\n+\tconst text = joinLines(texts);\n \tconst at = text.indexOf(oldText);\n \tif (at < 0) throw new EngineError(\"no-match\", `old text not found in ${file}`);\n \tif (text.indexOf(oldText, at + 1) >= 0) {\n@@ -488,9 +520,9 @@\n \t}\n \tconst starts: number[] = [];\n \tlet pos = 0;\n-\tfor (const line of visible) {\n+\tfor (const text of texts) {\n \t\tstarts.push(pos);\n-\t\tpos += line.text.length + 1;\n+\t\tpos += text.length + 1;\n \t}\n \tconst lineAt = (offset: number): number => {\n \t\tlet lo = 0;\n@@ -506,12 +538,12 @@\n \tconst startLine = lineAt(at);\n \tlet endLine = lineAt(end - 1);\n \tconst lineStart = starts[startLine]!;\n-\tconst lineEnd = starts[endLine]! + visible[endLine]!.text.length + 1;\n+\tconst lineEnd = starts[endLine]! + texts[endLine]!.length + 1;\n \tlet replacement = text.slice(lineStart, at) + newText + text.slice(end, lineEnd);\n \tif (replacement !== \"\" && !replacement.endsWith(\"\\n\")) {\n \t\tif (endLine + 1 < visible.length) {\n \t\t\tendLine++;\n-\t\t\treplacement += `${visible[endLine]!.text}\\n`;\n+\t\t\treplacement += `${texts[endLine]}\\n`;\n \t\t} else {\n \t\t\treplacement += \"\\n\";\n \t\t}\n@@ -520,7 +552,7 @@\n }\n \n function matchSplice(view: AtomBuildView, edit: SpliceEdit, mode: Mode, order: number): Region {\n-\tconst count = view.lines(edit.file).length;\n+\tconst count = buildingLines(view, edit.file).length;\n \tif (\n \t\t!Number.isInteger(edit.start) ||\n \t\t!Number.isInteger(edit.end) ||"}],"unmatched":[]}