- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/src/engine/experimental/projection-reader.ts?version=143&format=json
183 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 143, 1 version changed it
You're reading version 143. The newest is 318, and 4 versions since then changed this file.
1import { startNode } from "../keys.ts";
2import { StableSet } from "../state-source.ts";
3import { joinLines } from "../text.ts";
4import type { FileAttrs, ReadView, SeqLine, VisibleLine } from "../view.ts";
5import type { ExperimentalMarkerProjection, MarkerState } from "./projection-marker.ts";
7/** Private reader for a host-verified immutable projection. Not a production View replacement. */
8export class ProjectionReader implements ReadView {
9 readonly #adapter: ExperimentalMarkerProjection;
10 readonly #state: MarkerState;
11 readonly #assertLive: () => void;
12 constructor(adapter: ExperimentalMarkerProjection, state: MarkerState, assertLive: () => void) {
13 assertLive();
14 this.#assertLive = assertLive;
15 adapter.assertContext(state);
16 this.#adapter = adapter;
17 this.#state = structuredClone(state);
18 }
19 get graph() {
20 this.#assertLive();
21 return this.#adapter.graph;
22 }
23 get set(): ReadonlySet<string> {
24 this.#assertLive();
25 const has = (id: string) =>
26 this.graph.has(id) &&
27 (this.#adapter.members.get(this.#state.membership, this.graph.atom(id).seq)?.present ?? false);
28 const assertLive = this.#assertLive;
29 return new (class extends StableSet<string> {
30 readonly retainedBytes = 64;
31 has(id: string): boolean {
32 assertLive();
33 return has(id);
34 }
35 values(): SetIterator<string> {
36 assertLive();
37 throw Error("unsupported projected membership enumeration");
38 }
39 })();
40 }
41 paths(): string[] {
42 this.#assertLive();
43 return this.knownPaths().filter((path) => this.hasFile(path));
44 }
45 files(): Record<string, string> {
46 this.#assertLive();
47 return Object.fromEntries(
48 this.paths()
49 .filter((path) => this.attrs(path).blob === null)
50 .map((path) => [path, this.text(path)]),
51 );
52 }
53 blobs(): Record<string, string> {
54 this.#assertLive();
55 const result: Record<string, string> = {};
56 for (const path of this.paths()) {
57 const blob = this.attrs(path).blob;
58 if (blob !== null) result[path] = blob;
59 }
60 return result;
61 }
62 lineNumber(path: string, line: string): number {
63 this.#assertLive();
64 return this.lines(path).findIndex((item) => item.id === line);
65 }
66 sameText(path: string, other: ReadView): boolean {
67 this.#assertLive();
68 const a = this.lines(path),
69 b = other.lines(path);
70 return a.length === b.length && a.every((line, index) => line.text === b[index]!.text);
71 }
72 visibleNeighbours(_path: string, _line: string): string[] {
73 this.#assertLive();
74 throw Error("unsupported projected visibleNeighbours");
75 }
76 weight(): number {
77 this.#assertLive();
78 throw Error("transaction projections cannot enter historical View cache");
79 }
81 attrs(path: string): FileAttrs {
82 this.#assertLive();
83 return this.#adapter.attrs(this.#state, path);
84 }
85 knownPaths(): string[] {
86 this.#assertLive();
87 return this.#adapter.knownPaths(this.#state);
88 }
89 hasMoves(): boolean {
90 this.#assertLive();
91 for (const id of this.graph.movers()) if (this.set.has(id)) return true;
92 return false;
93 }
94 *visibleLines(path: string): IterableIterator<VisibleLine> {
95 this.#assertLive();
96 const graph = this.#adapter.graph;
97 const iterator = this.#adapter.lineIds(this.#state, path);
98 while (true) {
99 this.#assertLive();
100 const next = iterator.next();
101 this.#assertLive();
102 if (next.done) return;
103 const id = next.value;
104 const n = graph.lineN(id);
105 yield { id, text: graph.textOf(n), atom: graph.idOfSeq(graph.writerSeqOf(n)) };
106 }
107 }
108 lines(path: string): readonly VisibleLine[] {
109 this.#assertLive();
110 return [...this.visibleLines(path)];
111 }
112 sequence(path: string): readonly SeqLine[] {
113 this.#assertLive();
114 const adapter = this.#adapter,
115 state = this.#state,
116 graph = adapter.graph;
117 const key = adapter.fileKey(path);
118 const file = key === undefined ? undefined : adapter.markers.get(state.locator, key);
119 if (!file) return [];
120 const result: SeqLine[] = [];
121 for (const node of adapter.sequence.range(
122 state.sequence,
123 adapter.sequence.rank(state.sequence, file.enter) + 1,
124 adapter.sequence.rank(state.sequence, file.exit),
125 false,
126 )) {
127 if (node.text.endsWith(":end")) continue;
128 const n = graph.lineN(node.text),
129 writer = graph.writerSeqOf(n);
130 if (!adapter.members.get(state.membership, writer)?.present) continue;
131 const deletedBy = [...graph.eachDeleterSeqOf(n)]
132 .filter((seq) => adapter.members.get(state.membership, seq)?.present)
133 .sort((a, b) => graph.lamportOfSeq(a) - graph.lamportOfSeq(b) || a - b)
134 .map((seq) => graph.idOfSeq(seq));
135 result.push({
136 id: node.text,
137 text: graph.textOf(n),
138 atom: graph.idOfSeq(writer),
139 deletedBy,
140 visible: deletedBy.length === 0,
141 });
142 }
143 return result;
144 }
145 sequenceIndex(path: string, line: string): number {
146 this.#assertLive();
147 return this.sequence(path).findIndex((item) => item.id === line);
148 }
149 parentOf(line: string): string {
150 this.#assertLive();
151 const adapter = this.#adapter,
152 state = this.#state;
153 const marker = adapter.markers.get(state.locator, adapter.graph.lineN(line) + 1);
154 if (marker?.kind !== "line") throw Error("missing projected line");
155 if (marker.parentKey > 0 && marker.parentKey < 0x80000000) return adapter.graph.lineIdOf(marker.parentKey - 1);
156 const parent = adapter.markers.get(state.locator, marker.parentKey);
157 if (parent?.kind !== "file") throw Error("missing projected file parent");
158 const text = adapter.sequence.get(state.sequence.map, parent.enter).text;
159 if (!text.startsWith("root:")) throw Error("invalid projected file parent");
160 return startNode(text.slice(5));
161 }
162 fileOf(line: string): string {
163 this.#assertLive();
164 if (line.startsWith("^")) return line.slice(1);
165 const marker = this.#adapter.markers.get(this.#state.locator, this.graph.lineN(line) + 1);
166 if (marker?.kind !== "line") throw Error("missing projected line");
167 return this.#adapter.sequence.fileAt(this.#state.sequence, marker.enter);
168 }
169 text(path: string): string {
170 this.#assertLive();
171 return joinLines([...this.visibleLines(path)].map((line) => line.text));
172 }
173 hasFile(path: string): boolean {
174 this.#assertLive();
175 const attrs = this.attrs(path);
176 return (
177 attrs.exists ||
178 attrs.link !== null ||
179 attrs.blob !== null ||
180 !this.#adapter.lineIds(this.#state, path).next().done
181 );
182 }
183}