MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / createSiteReader

Function createSiteReader

src/ui-server/api/when.ts:150–281  ·  view source on GitHub ↗
(cg: CodeGraph, projectRoot: string, maxSites = 600)

Source from the content-addressed store, hash-verified

148 * the whole pass.
149 */
150export function createSiteReader(cg: CodeGraph, projectRoot: string, maxSites = 600): SiteReader {
151 const files = new Map<string, { abs: string; language: Language } | null>();
152 const texts = new Map<string, string | null>();
153 let sites = 0;
154 const resolve = (caller: { filePath: string; language: Language }): { abs: string; language: Language } | null => {
155 const posix = caller.filePath.replace(/\\/g, '/');
156 let file = files.get(posix);
157 if (file === undefined) {
158 file = null;
159 const found = findIndexedFile(cg, posix);
160 if (found && !hasDriftedOnDisk(projectRoot, found.storedPath, found.record)) {
161 try {
162 file = { abs: resolveProjectFile(projectRoot, found.storedPath), language: found.record.language as Language };
163 } catch {
164 file = null;
165 }
166 }
167 files.set(posix, file);
168 }
169 return file;
170 };
171 /** A file's text, once, for the directive and declaration reads; null past the size cap or unreadable. */
172 const readText = (abs: string): string | null => {
173 let text = texts.get(abs);
174 if (text === undefined) {
175 try {
176 text = fs.statSync(abs).size <= MAX_DIRECTIVE_FILE ? fs.readFileSync(abs, 'utf8') : null;
177 } catch {
178 text = null;
179 }
180 texts.set(abs, text);
181 }
182 return text;
183 };
184 // Named rather than a method, because `createWhenReader` hands `when` out
185 // detached: it must not depend on `this`.
186 const guards = async (
187 caller: { filePath: string; language: Language },
188 site: { line?: number; column?: number }
189 ): Promise<BranchGuard[]> => {
190 if (!site.line || sites >= maxSites || !supportsBranchGuards(caller.language)) return [];
191 const file = resolve(caller);
192 if (!file) return [];
193 sites++;
194 const key = { line: site.line, column: typeof site.column === 'number' ? site.column : null };
195 return (await guardsForFile(file.abs, file.language, [key])).get(siteKey(key)) ?? [];
196 };
197 return {
198 guards,
199 async when(caller, site) {
200 return guardLabel(await guards(caller, site));
201 },
202 async loops(caller, site) {
203 // Not counted against the budget: the tree is parsed for the site's
204 // guards anyway, and this is a second climb up the same nodes.
205 if (!site.line || !supportsBranchGuards(caller.language)) return [];
206 const file = resolve(caller);
207 if (!file) return [];

Callers 2

buildStepsFunction · 0.90
createWhenReaderFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected