Lines 19–27 at 318 (were 19–26 at 2)
1919// A use means a definition only through an import of it, or in the file 2020// that defines it (project.ts, #definitionCore). 22−import type { LineId, View } from "../../src/engine/index.ts";
23−import { codeOfLines, isCommonName, isProse } from "./names.ts";
22+import type { LineId, ReadView } from "../../src/engine/index.ts"; 23+import { withDefinitionCode } from "./definition-code.ts"; 24+import { holdsNoCode, isCommonName } from "./names.ts"; 2526export interface Definition { 2627 readonly name: string; Lines 91–103 at 318 (were 90–102 at 2)
9091const TOP_ASSIGN = new RegExp(`^(${NAME})\\s*(?::[^=]*)?=(?![=>])`); 9192const MAX_HEADER = 30; 93−const cache = new WeakMap<View, Map<string, readonly Definition[]>>();
94+const cache = new WeakMap<ReadView, Map<string, readonly Definition[]>>(); 9697 * Every definition in the view's code files (or only in `files`), by name. 9798 * Each file's definitions are cached per view. 99−export function definitionsIn(view: View, files?: Iterable<string>): Map<string, Definition[]> {
100+export function definitionsIn(view: ReadView, files?: Iterable<string>): Map<string, Definition[]> { 100101 let perFile = cache.get(view); 102103 perFile = new Map(); Lines 119–149 at 318 (were 118–147 at 2)
121−function definitionsInFile(view: View, file: string, keepCommon = false): Definition[] {
122− if (isProse(file) || !view.hasFile(file)) return [];
122+function definitionsInFile(view: ReadView, file: string, keepCommon = false): Definition[] { 123+ if (holdsNoCode(file) || !view.hasFile(file)) return []; 123124 const out: Definition[] = []; 124− const visible = view.lines(file);
125− const code = codeOfLines(visible.map((l) => l.text));
126− for (let i = 0; i < code.length; i++) {
127− // Only top-level declarations: what another file can import. Locals,
128− // parameters and methods are indented.
129− if (/^\s/.test(code[i]!)) continue;
130− const found = defineAt(code, i);
131− if (!found || (!keepCommon && (found.name.length < 3 || isCommonName(found.name)))) continue;
132− out.push({
133− name: found.name,
134− file,
135− start: i,
136− end: found.end,
137− lines: visible.slice(i, found.end).map((l) => l.id),
138− text: visible
139− .slice(i, found.end)
140− .map((l) => l.text)
141− .join("\n"),
142− });
143− }
144− return out;
125+ const visible = view.buildLines(file); 126+ return withDefinitionCode(view, visible, (code) => { 127+ for (let i = 0; i < code.length; i++) { 128+ // Only top-level declarations: what another file can import. Locals, 129+ // parameters and methods are indented. 130+ if (/^\s/.test(code[i]!)) continue; 131+ const found = defineAt(code, i); 132+ if (!found || (!keepCommon && (found.name.length < 3 || isCommonName(found.name)))) continue; 138+ lines: visible.slice(i, found.end).map((l) => l.id), 140+ .slice(i, found.end) Lines 151–182 at 318 (were 149–179 at 2)
149151 * included (two `parse` functions in a file are a mistake whatever the name). 150152 * Each spans its whole block, body and all, for showing to the agent. 152−export function definedTwice(view: View, file: string): Map<string, Definition[]> {
154+export function definedTwice(view: ReadView, file: string): Map<string, Definition[]> { 153155 const out = new Map<string, Definition[]>(); 154156 const all = definitionsInFile(view, file, true); 155157 const count = new Map<string, number>(); 156158 for (const d of all) count.set(d.name, (count.get(d.name) ?? 0) + 1); 157159 if (![...count.values()].some((n) => n > 1)) return out; 158− const visible = view.lines(file);
159− const code = codeOfLines(visible.map((l) => l.text));
160− for (const d of all) {
161− if (count.get(d.name)! < 2) continue;
162− const end = Math.max(d.end, wholeBlockEnd(code, d.start));
163− const whole: Definition = {
164− ...d,
165− end,
166− lines: visible.slice(d.start, end).map((l) => l.id),
167− text: visible
168− .slice(d.start, end)
169− .map((l) => l.text)
170− .join("\n"),
171− };
172− const list = out.get(d.name);
173− if (list) list.push(whole);
174− else out.set(d.name, [whole]);
175− }
176− return out;
160+ const visible = view.buildLines(file); 161+ return withDefinitionCode(view, visible, (code) => { 162+ for (const d of all) { 163+ if (count.get(d.name)! < 2) continue; 164+ const end = Math.max(d.end, wholeBlockEnd(code, d.start)); 165+ const whole: Definition = { 168+ lines: visible.slice(d.start, end).map((l) => l.id), 170+ .slice(d.start, end) 174+ const list = out.get(d.name); 175+ if (list) list.push(whole); 176+ else out.set(d.name, [whole]); Lines 212–218 at 318 (were 209–215 at 2)
209212 * Whether a declaration found at `at` starts its line, after modifiers 210213 * only: `export const x`, `async function f`, `pub fn g`. A `let` inside 211214 * `for (let i = 0; ...)` or a `function` passed as an argument is not a 212− * definition of the file (#37, Builder 4).
215+ * definition of the file (#37). 215218 /^\s*(?:(?:export|default|declare|async|public|private|protected|static|abstract|override|readonly|pub(?:\([a-z]+\))?|unsafe|extern|const|final|sealed|open|data|inline|private\[this\])\s+)*$/; Lines 309–315 at 318 (were 306–312 at 2)
309−const importCache = new WeakMap<View, Map<string, ReadonlyMap<string, string>>>();
312+const importCache = new WeakMap<ReadView, Map<string, ReadonlyMap<string, string>>>(); 312315 * Names a file imports by name, and the project file each comes from, when Lines 319–325 at 318 (were 316–322 at 2)
316319 * x.y import a, b`. Lets a use of a name be matched to the definition it 317320 * means instead of every definition with that name. 319−export function importsOf(view: View, file: string): ReadonlyMap<string, string> {
322+export function importsOf(view: ReadView, file: string): ReadonlyMap<string, string> { 320323 let perFile = importCache.get(view); 322325 perFile = new Map(); Lines 370–376 at 318 (were 367–373 at 2)
367370 return parts.join("/"); 370−function resolveModule(view: View, from: string, spec: string): string | undefined {
373+function resolveModule(view: ReadView, from: string, spec: string): string | undefined { 371374 if (!spec.startsWith(".")) return undefined; 372375 const base = joinPath(dirOf(from), spec); 373376 const stem = base.replace(/\.(?:js|mjs|cjs|jsx)$/, ""); Lines 388–394 at 318 (were 385–391 at 2)
388−function resolvePython(view: View, from: string, spec: string): string | undefined {
391+function resolvePython(view: ReadView, from: string, spec: string): string | undefined { 389392 const dots = /^\.*/.exec(spec)![0].length; 390393 const rest = spec.slice(dots).replace(/\./g, "/"); 391394 let dir = dots > 0 ? dirOf(from) : "";