{"draft":null,"from":142,"to":143,"files":[{"path":"src/engine/experimental/projection-reader.ts","added":183,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/experimental/projection-reader.ts\n@@ -0,0 +1,183 @@\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+/** Private reader for a host-verified immutable projection. Not a production View replacement. */\n+export class ProjectionReader implements ReadView {\n+\treadonly #adapter: ExperimentalMarkerProjection;\n+\treadonly #state: MarkerState;\n+\treadonly #assertLive: () => void;\n+\tconstructor(adapter: ExperimentalMarkerProjection, state: MarkerState, assertLive: () => void) {\n+\t\tassertLive();\n+\t\tthis.#assertLive = assertLive;\n+\t\tadapter.assertContext(state);\n+\t\tthis.#adapter = adapter;\n+\t\tthis.#state = structuredClone(state);\n+\t}\n+\tget graph() {\n+\t\tthis.#assertLive();\n+\t\treturn this.#adapter.graph;\n+\t}\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\tconst assertLive = this.#assertLive;\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\tassertLive();\n+\t\t\t\tthrow Error(\"unsupported projected membership enumeration\");\n+\t\t\t}\n+\t\t})();\n+\t}\n+\tpaths(): string[] {\n+\t\tthis.#assertLive();\n+\t\treturn this.knownPaths().filter((path) => this.hasFile(path));\n+\t}\n+\tfiles(): Record<string, string> {\n+\t\tthis.#assertLive();\n+\t\treturn Object.fromEntries(\n+\t\t\tthis.paths()\n+\t\t\t\t.filter((path) => this.attrs(path).blob === null)\n+\t\t\t\t.map((path) => [path, this.text(path)]),\n+\t\t);\n+\t}\n+\tblobs(): Record<string, string> {\n+\t\tthis.#assertLive();\n+\t\tconst result: Record<string, string> = {};\n+\t\tfor (const path of this.paths()) {\n+\t\t\tconst blob = this.attrs(path).blob;\n+\t\t\tif (blob !== null) result[path] = blob;\n+\t\t}\n+\t\treturn result;\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}\n+\tsameText(path: string, other: ReadView): boolean {\n+\t\tthis.#assertLive();\n+\t\tconst a = this.lines(path),\n+\t\t\tb = other.lines(path);\n+\t\treturn a.length === b.length && a.every((line, index) => line.text === b[index]!.text);\n+\t}\n+\tvisibleNeighbours(_path: string, _line: string): string[] {\n+\t\tthis.#assertLive();\n+\t\tthrow Error(\"unsupported projected visibleNeighbours\");\n+\t}\n+\tweight(): number {\n+\t\tthis.#assertLive();\n+\t\tthrow Error(\"transaction projections cannot enter historical View cache\");\n+\t}\n+\n+\tattrs(path: string): FileAttrs {\n+\t\tthis.#assertLive();\n+\t\treturn this.#adapter.attrs(this.#state, path);\n+\t}\n+\tknownPaths(): string[] {\n+\t\tthis.#assertLive();\n+\t\treturn this.#adapter.knownPaths(this.#state);\n+\t}\n+\thasMoves(): boolean {\n+\t\tthis.#assertLive();\n+\t\tfor (const id of this.graph.movers()) if (this.set.has(id)) return true;\n+\t\treturn false;\n+\t}\n+\t*visibleLines(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\tyield { id, text: graph.textOf(n), atom: graph.idOfSeq(graph.writerSeqOf(n)) };\n+\t\t}\n+\t}\n+\tlines(path: string): readonly VisibleLine[] {\n+\t\tthis.#assertLive();\n+\t\treturn [...this.visibleLines(path)];\n+\t}\n+\tsequence(path: string): readonly SeqLine[] {\n+\t\tthis.#assertLive();\n+\t\tconst adapter = this.#adapter,\n+\t\t\tstate = this.#state,\n+\t\t\tgraph = adapter.graph;\n+\t\tconst key = adapter.fileKey(path);\n+\t\tconst file = key === undefined ? undefined : adapter.markers.get(state.locator, key);\n+\t\tif (!file) return [];\n+\t\tconst result: SeqLine[] = [];\n+\t\tfor (const node of adapter.sequence.range(\n+\t\t\tstate.sequence,\n+\t\t\tadapter.sequence.rank(state.sequence, file.enter) + 1,\n+\t\t\tadapter.sequence.rank(state.sequence, file.exit),\n+\t\t\tfalse,\n+\t\t)) {\n+\t\t\tif (node.text.endsWith(\":end\")) continue;\n+\t\t\tconst n = graph.lineN(node.text),\n+\t\t\t\twriter = graph.writerSeqOf(n);\n+\t\t\tif (!adapter.members.get(state.membership, writer)?.present) continue;\n+\t\t\tconst deletedBy = [...graph.eachDeleterSeqOf(n)]\n+\t\t\t\t.filter((seq) => adapter.members.get(state.membership, seq)?.present)\n+\t\t\t\t.sort((a, b) => graph.lamportOfSeq(a) - graph.lamportOfSeq(b) || a - b)\n+\t\t\t\t.map((seq) => graph.idOfSeq(seq));\n+\t\t\tresult.push({\n+\t\t\t\tid: node.text,\n+\t\t\t\ttext: graph.textOf(n),\n+\t\t\t\tatom: graph.idOfSeq(writer),\n+\t\t\t\tdeletedBy,\n+\t\t\t\tvisible: deletedBy.length === 0,\n+\t\t\t});\n+\t\t}\n+\t\treturn result;\n+\t}\n+\tsequenceIndex(path: string, line: string): number {\n+\t\tthis.#assertLive();\n+\t\treturn this.sequence(path).findIndex((item) => item.id === line);\n+\t}\n+\tparentOf(line: string): string {\n+\t\tthis.#assertLive();\n+\t\tconst adapter = this.#adapter,\n+\t\t\tstate = this.#state;\n+\t\tconst marker = adapter.markers.get(state.locator, adapter.graph.lineN(line) + 1);\n+\t\tif (marker?.kind !== \"line\") throw Error(\"missing projected line\");\n+\t\tif (marker.parentKey > 0 && marker.parentKey < 0x80000000) return adapter.graph.lineIdOf(marker.parentKey - 1);\n+\t\tconst parent = adapter.markers.get(state.locator, marker.parentKey);\n+\t\tif (parent?.kind !== \"file\") throw Error(\"missing projected file parent\");\n+\t\tconst text = adapter.sequence.get(state.sequence.map, parent.enter).text;\n+\t\tif (!text.startsWith(\"root:\")) throw Error(\"invalid projected file parent\");\n+\t\treturn startNode(text.slice(5));\n+\t}\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}\n+\ttext(path: string): string {\n+\t\tthis.#assertLive();\n+\t\treturn joinLines([...this.visibleLines(path)].map((line) => line.text));\n+\t}\n+\thasFile(path: string): boolean {\n+\t\tthis.#assertLive();\n+\t\tconst attrs = this.attrs(path);\n+\t\treturn (\n+\t\t\tattrs.exists ||\n+\t\t\tattrs.link !== null ||\n+\t\t\tattrs.blob !== null ||\n+\t\t\t!this.#adapter.lineIds(this.#state, path).next().done\n+\t\t);\n+\t}\n+}"}],"unmatched":[]}