{"draft":null,"from":89,"to":318,"files":[{"path":"src/engine/text.ts","added":123,"removed":6,"diff":"--- a/src/engine/text.ts\n+++ b/src/engine/text.ts\n@@ -43,12 +43,129 @@\n  * file's length times MAX_COST instead of its length squared.\n  */\n export function diffLines(a: readonly string[], b: readonly string[]): Hunk[] {\n-\tconst matches = commonLines(a, b);\n-\tconst hunks: Hunk[] = [];\n+\treturn hunksFromMatches(a.length, b, commonLines(a, b));\n+}\n+\n+/** Diff a single-pass old-line source without retaining its strings. */\n+export function diffLineStream(a: Iterable<string>, b: readonly string[]): Hunk[] {\n+\tconst ids = new Map<string, number>();\n+\tconst next = b.map((line) => {\n+\t\tlet id = ids.get(line);\n+\t\tif (id === undefined) {\n+\t\t\tid = ids.size;\n+\t\t\tids.set(line, id);\n+\t\t}\n+\t\treturn id;\n+\t});\n+\tconst previous: number[] = [];\n+\tfor (const line of a) previous.push(ids.get(line) ?? -1);\n+\treturn hunksFromMatches(previous.length, b, commonLines(previous, next));\n+}\n+\n+/** Compare replayable old-line chunks without assembling historical strings. */\n+export function diffChunkedLines(a: Iterable<() => Iterable<string>>, b: readonly string[]): Hunk[] {\n+\tconst keys = new Map<string, number>();\n+\tconst buckets = new Map<string, number[]>();\n+\tconst next = b.map((line, index) => {\n+\t\tlet id = keys.get(line);\n+\t\tif (id === undefined) {\n+\t\t\tid = index;\n+\t\t\tkeys.set(line, id);\n+\t\t\tconst key = chunkKey([line]);\n+\t\t\tconst bucket = buckets.get(key) ?? [];\n+\t\t\tbucket.push(id);\n+\t\t\tbuckets.set(key, bucket);\n+\t\t}\n+\t\treturn id;\n+\t});\n+\tconst previous: number[] = [];\n+\tfor (const chunks of a) {\n+\t\tconst iterator = chunks()[Symbol.iterator]();\n+\t\tlet key: string;\n+\t\tconst aligned = b[previous.length];\n+\t\tlet alignedOffset = 0;\n+\t\tlet alignedEqual = aligned !== undefined;\n+\t\ttry {\n+\t\t\tconst first = iterator.next();\n+\t\t\tif (first.done) {\n+\t\t\t\tprevious.push(keys.get(\"\") ?? -1);\n+\t\t\t\tcontinue;\n+\t\t\t}\n+\t\t\tconst second = iterator.next();\n+\t\t\tif (second.done) {\n+\t\t\t\tprevious.push(keys.get(first.value) ?? -1);\n+\t\t\t\tcontinue;\n+\t\t\t}\n+\t\t\tkey = chunkKey(\n+\t\t\t\t(function* () {\n+\t\t\t\t\tfunction* remaining() {\n+\t\t\t\t\t\tyield first.value;\n+\t\t\t\t\t\tyield second.value;\n+\t\t\t\t\t\tfor (let next = iterator.next(); !next.done; next = iterator.next()) yield next.value;\n+\t\t\t\t\t}\n+\t\t\t\t\tfor (const chunk of remaining()) {\n+\t\t\t\t\t\tif (alignedEqual && !aligned!.startsWith(chunk, alignedOffset)) alignedEqual = false;\n+\t\t\t\t\t\talignedOffset += chunk.length;\n+\t\t\t\t\t\tyield chunk;\n+\t\t\t\t\t}\n+\t\t\t\t})(),\n+\t\t\t);\n+\t\t} finally {\n+\t\t\titerator.return?.();\n+\t\t}\n+\t\t// Same-position unchanged lines are common. Confirm them during hashing,\n+\t\t// retaining exact comparison and the collision-safe fallback for moved lines.\n+\t\tif (alignedEqual && alignedOffset === aligned!.length) {\n+\t\t\tprevious.push(next[previous.length]!);\n+\t\t\tcontinue;\n+\t\t}\n+\t\tconst candidates = buckets.get(key);\n+\t\tlet match = -1;\n+\t\tfor (const candidate of candidates ?? []) {\n+\t\t\tlet offset = 0;\n+\t\t\tlet equal = true;\n+\t\t\tfor (const chunk of chunks()) {\n+\t\t\t\tif (!b[candidate]!.startsWith(chunk, offset)) equal = false;\n+\t\t\t\toffset += chunk.length;\n+\t\t\t}\n+\t\t\tif (equal && offset === b[candidate]!.length) {\n+\t\t\t\tmatch = candidate;\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t}\n+\t\tprevious.push(match);\n+\t}\n+\treturn hunksFromMatches(previous.length, b, commonLines(previous, next));\n+}\n+\n+function chunkKey(chunks: Iterable<string>): string {\n+\tlet hash = 2166136261;\n+\tlet length = 0;\n+\tfor (const chunk of chunks) {\n+\t\tlength += chunk.length;\n+\t\tfor (let i = 0; i < chunk.length; i++) hash = Math.imul(hash ^ chunk.charCodeAt(i), 16777619) >>> 0;\n+\t}\n+\treturn `${length}:${hash}`;\n+}\n+\n+/** The existing diff algorithm over exact line symbols, without retaining text. */\n+export function diffLineSymbols(\n+\ta: readonly number[],\n+\tb: readonly number[],\n+): { start: number; end: number; lines: number[] }[] {\n+\treturn hunksFromMatches(a.length, b, commonLines(a, b));\n+}\n+\n+function hunksFromMatches<T>(\n+\tlength: number,\n+\tb: readonly T[],\n+\tmatches: readonly number[],\n+): { start: number; end: number; lines: T[] }[] {\n+\tconst hunks: { start: number; end: number; lines: T[] }[] = [];\n \tlet i = 0;\n \tlet j = 0;\n \tfor (let m = 0; m <= matches.length; m += 2) {\n-\t\tconst mi = m < matches.length ? matches[m]! : a.length;\n+\t\tconst mi = m < matches.length ? matches[m]! : length;\n \t\tconst mj = m < matches.length ? matches[m + 1]! : b.length;\n \t\tif (mi > i || mj > j) hunks.push({ start: i, end: mi, lines: b.slice(j, mj) });\n \t\ti = mi + 1;\n@@ -61,7 +178,7 @@\n  * The matched pairs of a longest common subsequence of `a` and `b`, flattened\n  * as [i0, j0, i1, j1, ...] in increasing order.\n  */\n-function commonLines(a: readonly string[], b: readonly string[]): number[] {\n+function commonLines<T>(a: readonly T[], b: readonly T[]): number[] {\n \t// Common prefix and suffix match directly.\n \tlet pre = 0;\n \twhile (pre < a.length && pre < b.length && a[pre] === b[pre]) pre++;\n@@ -72,8 +189,8 @@\n \n \t// A line that occurs on only one side can never be matched; leave it out of\n \t// the search (this also makes wholly different texts instant).\n-\tconst ids = new Map<string, number>();\n-\tconst intern = (s: string): number => {\n+\tconst ids = new Map<T, number>();\n+\tconst intern = (s: T): number => {\n \t\tlet id = ids.get(s);\n \t\tif (id === undefined) {\n \t\t\tid = ids.size;"}],"unmatched":[]}