- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/definition-code.ts?version=261&format=json
70 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 175, 1 version changed it
You're reading version 261. The newest is 318, and 2 versions since then changed this file.
1import type { ReadView, VisibleLine } from "../../src/engine/index.ts";
2import { StoredText } from "../../src/engine/stored-text.ts";
3import { eachCodeLine } from "./names.ts";
5/** Parsed code is temporary derived data, discarded with the scratch transaction. */
6export function withDefinitionCode<T>(
7 view: ReadView,
8 visible: readonly VisibleLine[],
9 operation: (code: readonly string[]) => T,
10): T {
11 function* source() {
12 for (const line of visible) yield line.text;
13 }
14 if (!view.graph.hasOrderedChildren) return operation([...eachCodeLine(source())]);
15 const iterator = eachCodeLine(source());
16 let prefix: string[] = [];
17 let size = 0;
18 let pending = iterator.next();
19 while (!pending.done && size + pending.value.length + 1 <= 65536) {
20 prefix.push(pending.value);
21 size += pending.value.length + 1;
22 pending = iterator.next();
23 }
24 if (pending.done) return operation(prefix);
25 return view.graph.withProjectionScratch((storage) => {
26 const text = new StoredText(storage, "definition-code");
27 let count = 0;
28 for (const line of prefix) text.write(String(count++), line);
29 prefix = [];
30 text.write(String(count++), pending.value!);
31 for (const line of iterator) text.write(String(count++), line);
32 const cache = new Map<number, string>();
33 let characters = 0;
34 const read = (index: number) => {
35 if (index < 0 || index >= count) return undefined;
36 const cached = cache.get(index);
37 if (cached !== undefined) return cached;
38 const value = text.read(String(index));
39 if (value.length <= 65536) {
40 while (cache.size >= 64 || characters + value.length > 65536) {
41 const first = cache.keys().next().value!;
42 characters -= cache.get(first)!.length;
43 cache.delete(first);
44 }
45 cache.set(index, value);
46 characters += value.length;
47 }
48 return value;
49 };
50 let active = true;
51 const code = new Proxy([] as string[], {
52 has(target, key) {
53 if (!active) throw Error("definition code scope has ended");
54 if (typeof key === "string" && /^(0|[1-9][0-9]*)$/.test(key)) return Number(key) < count;
55 return Reflect.has(target, key);
56 },
57 get(target, key, receiver) {
58 if (!active) throw Error("definition code scope has ended");
59 if (key === "length") return count;
60 if (typeof key === "string" && /^(0|[1-9][0-9]*)$/.test(key)) return read(Number(key));
61 return Reflect.get(target, key, receiver);
62 },
63 });
64 try {
65 return operation(code);
66 } finally {
67 active = false;
68 }
69 });
70}