{"draft":null,"from":153,"to":318,"files":[{"path":"src/engine/experimental/projection-reader.ts","added":102,"removed":10,"diff":"--- a/src/engine/experimental/projection-reader.ts\n+++ b/src/engine/experimental/projection-reader.ts\n@@ -1,6 +1,5 @@\n import { startNode } from \"../keys.ts\";\n import { StableSet } from \"../state-source.ts\";\n-import { joinLines } from \"../text.ts\";\n import type { FileAttrs, ReadView, SeqLine, VisibleLine } from \"../view.ts\";\n import type { ExperimentalMarkerProjection, MarkerState } from \"./projection-marker.ts\";\n \n@@ -23,18 +22,31 @@\n \tget set(): ReadonlySet<string> {\n \t\tthis.#assertLive();\n \t\tconst has = (id: string) =>\n-\t\t\tthis.graph.has(id) &&\n-\t\t\t(this.#adapter.members.get(this.#state.membership, this.graph.atom(id).seq)?.present ?? false);\n+\t\t\tthis.graph.has(id) && (this.#adapter.members.get(this.#state.membership, Number(id.slice(1)))?.present ?? false);\n \t\tconst assertLive = this.#assertLive;\n+\t\tconst members = this.#adapter.members;\n+\t\tconst root = this.#state.membership;\n+\t\tconst graph = this.graph;\n \t\treturn new (class extends StableSet<string> {\n \t\t\treadonly retainedBytes = 64;\n \t\t\thas(id: string): boolean {\n \t\t\t\tassertLive();\n \t\t\t\treturn has(id);\n \t\t\t}\n-\t\t\tvalues(): SetIterator<string> {\n+\t\t\t*values(): SetIterator<string> {\n \t\t\t\tassertLive();\n-\t\t\t\tthrow Error(\"unsupported projected membership enumeration\");\n+\t\t\t\tconst iterator = members.entries(root);\n+\t\t\t\ttry {\n+\t\t\t\t\tfor (;;) {\n+\t\t\t\t\t\tassertLive();\n+\t\t\t\t\t\tconst item = iterator.next();\n+\t\t\t\t\t\tif (item.done) return;\n+\t\t\t\t\t\tconst [seq, member] = item.value;\n+\t\t\t\t\t\tif (member.present) yield graph.idOfSeq(seq);\n+\t\t\t\t\t}\n+\t\t\t\t} finally {\n+\t\t\t\t\titerator.return?.();\n+\t\t\t\t}\n \t\t\t}\n \t\t})();\n \t}\n@@ -61,7 +73,14 @@\n \t}\n \tlineNumber(path: string, line: string): number {\n \t\tthis.#assertLive();\n-\t\treturn this.lines(path).findIndex((item) => item.id === line);\n+\t\tlet index = 0;\n+\t\tfor (const id of this.#adapter.lineIds(this.#state, path)) {\n+\t\t\tthis.#assertLive();\n+\t\t\tif (id === line) return index;\n+\t\t\tindex++;\n+\t\t}\n+\t\tthis.#assertLive();\n+\t\treturn -1;\n \t}\n \tsameText(path: string, other: ReadView): boolean {\n \t\tthis.#assertLive();\n@@ -91,6 +110,30 @@\n \t\tfor (const id of this.graph.movers()) if (this.set.has(id)) return true;\n \t\treturn false;\n \t}\n+\t*#textChunks(n: number): IterableIterator<string> {\n+\t\tthis.#assertLive();\n+\t\tconst iterator = this.graph.textChunks(n);\n+\t\twhile (true) {\n+\t\t\tthis.#assertLive();\n+\t\t\tconst next = iterator.next();\n+\t\t\tthis.#assertLive();\n+\t\t\tif (next.done) return;\n+\t\t\tyield next.value;\n+\t\t}\n+\t}\n+\t*lineChunks(path: string): IterableIterator<() => Iterable<string>> {\n+\t\tthis.#assertLive();\n+\t\tconst iterator = this.#adapter.lineIds(this.#state, path);\n+\t\twhile (true) {\n+\t\t\tthis.#assertLive();\n+\t\t\tconst next = iterator.next();\n+\t\t\tthis.#assertLive();\n+\t\t\tif (next.done) return;\n+\t\t\tconst n = this.graph.lineN(next.value);\n+\t\t\tyield () => this.#textChunks(n);\n+\t\t}\n+\t}\n+\n \t*visibleLines(path: string): IterableIterator<VisibleLine> {\n \t\tthis.#assertLive();\n \t\tconst graph = this.#adapter.graph;\n@@ -109,6 +152,34 @@\n \t\tthis.#assertLive();\n \t\treturn [...this.visibleLines(path)];\n \t}\n+\t*#buildingLines(path: string): IterableIterator<VisibleLine> {\n+\t\tthis.#assertLive();\n+\t\tconst graph = this.#adapter.graph;\n+\t\tconst iterator = this.#adapter.lineIds(this.#state, path);\n+\t\twhile (true) {\n+\t\t\tthis.#assertLive();\n+\t\t\tconst next = iterator.next();\n+\t\t\tthis.#assertLive();\n+\t\t\tif (next.done) return;\n+\t\t\tconst id = next.value;\n+\t\t\tconst n = graph.lineN(id);\n+\t\t\tconst assertLive = () => this.#assertLive();\n+\t\t\tyield {\n+\t\t\t\tid,\n+\t\t\t\ttextChunks: () => this.#textChunks(n),\n+\t\t\t\tget text() {\n+\t\t\t\t\tassertLive();\n+\t\t\t\t\treturn graph.textOf(n);\n+\t\t\t\t},\n+\t\t\t\tatom: graph.idOfSeq(graph.writerSeqOf(n)),\n+\t\t\t};\n+\t\t}\n+\t}\n+\tbuildLines(path: string): readonly VisibleLine[] {\n+\t\tthis.#assertLive();\n+\t\treturn [...this.#buildingLines(path)];\n+\t}\n+\n \tsequence(path: string): readonly SeqLine[] {\n \t\tthis.#assertLive();\n \t\tconst adapter = this.#adapter,\n@@ -162,13 +233,34 @@\n \tfileOf(line: string): string {\n \t\tthis.#assertLive();\n \t\tif (line.startsWith(\"^\")) return line.slice(1);\n-\t\tconst marker = this.#adapter.markers.get(this.#state.locator, this.graph.lineN(line) + 1);\n-\t\tif (marker?.kind !== \"line\") throw Error(\"missing projected line\");\n-\t\treturn this.#adapter.sequence.fileAt(this.#state.sequence, marker.enter);\n+\t\tlet current = line;\n+\t\twhile (!current.startsWith(\"^\")) {\n+\t\t\tconst n = this.graph.lineN(current);\n+\t\t\tif (n < 0) throw Error(\"unknown projected line\");\n+\t\t\tconst marker = this.#adapter.markers.get(this.#state.locator, n + 1);\n+\t\t\tif (marker?.kind === \"line\") return this.#adapter.sequence.fileAt(this.#state.sequence, marker.enter);\n+\t\t\t// A line written outside this view has no marker. Follow canonical ancestry\n+\t\t\t// until it reaches a line whose placement this view actually contains.\n+\t\t\tif (marker || this.#adapter.members.get(this.#state.membership, this.graph.writerSeqOf(n))?.present)\n+\t\t\t\tthrow Error(\"missing projected line\");\n+\t\t\tconst parent = this.graph.lineParent(current);\n+\t\t\tif (parent === undefined) throw Error(\"missing projected line parent\");\n+\t\t\tcurrent = parent;\n+\t\t}\n+\t\treturn current.slice(1);\n \t}\n \ttext(path: string): string {\n \t\tthis.#assertLive();\n-\t\treturn joinLines([...this.visibleLines(path)].map((line) => line.text));\n+\t\tlet text = \"\",\n+\t\t\tpart = \"\";\n+\t\tfor (const line of this.visibleLines(path)) {\n+\t\t\tpart += `${line.text}\\n`;\n+\t\t\tif (part.length >= 16384) {\n+\t\t\t\ttext += part;\n+\t\t\t\tpart = \"\";\n+\t\t\t}\n+\t\t}\n+\t\treturn text + part;\n \t}\n \thasFile(path: string): boolean {\n \t\tthis.#assertLive();"}],"unmatched":[]}