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

Function nestedRoutineInScope

src/resolution/pascal-scope.ts:863–894  ·  view source on GitHub ↗

* The routine nested in the calling routine, or in one around it, that a * bare name there names (`procedure Inner;` between `procedure TFoo.Bar;` * and its `begin`, `TFoo::Bar::Inner`): the innermost one declared above the * call, the calling routine itself among them, or declared `forward` abov

(lower: string, ref: UnresolvedRef, context: ResolutionContext)

Source from the content-addressed store, hash-verified

861 * method, which is no node — is the routine's whose header is above it.
862 */
863function nestedRoutineInScope(lower: string, ref: UnresolvedRef, context: ResolutionContext): Found | null {
864 // Most names no nested routine has: settled before any other lookup.
865 const named = pascalNamed(lower, context);
866 if (!named.some((n) => n.kind === 'function' && n.qualifiedName.includes('::'))) return null;
867 const caller = routineNode(ref.fromNodeId, context);
868 const fromHeader = caller ? null : headerRoutineAt(ref.filePath!, ref.line, context);
869 const memo = memos(context).nestedInScope;
870 const key = `${ref.fromNodeId}|${fromHeader ?? ''}|${lower}`;
871 if (memo.has(key)) return memo.get(key)!;
872 let found: Found | null = null;
873 if ((caller && caller.language === 'pascal') || fromHeader) {
874 const chain = (caller?.qualifiedName ?? fromHeader!).toLowerCase();
875 let best: Node | null = null;
876 let depth = -1;
877 for (const n of named) {
878 // A routine whose header is above the code is no node: what is nested in it is one by its name alone.
879 const nested = caller ? isNestedRoutine(n, context) : n.kind === 'function' && n.language === 'pascal' && n.qualifiedName.includes('::');
880 if (n.filePath !== ref.filePath || !nested) continue;
881 if (n.startLine > ref.line && !declaredForward(n, ref.line, context)) continue;
882 const outer = ownerOf(n);
883 // Nested in the caller, or in a routine the caller is nested in.
884 if (chain !== outer && !chain.startsWith(`${outer}::`)) continue;
885 if (outer.length > depth || (outer.length === depth && n.startLine < best!.startLine)) {
886 best = n;
887 depth = outer.length;
888 }
889 }
890 if (best) found = { node: best, resolvedBy: 'exact-match', confidence: 0.95 };
891 }
892 memo.set(key, found);
893 return found;
894}
895
896/**
897 * Whether a routine nested in another is declared `forward` above a line

Callers 3

matchPascalNestedCallFunction · 0.85
declarationInScopeFunction · 0.85
typeOfLinksFunction · 0.85

Calls 10

pascalNamedFunction · 0.85
routineNodeFunction · 0.85
headerRoutineAtFunction · 0.85
memosFunction · 0.85
isNestedRoutineFunction · 0.85
declaredForwardFunction · 0.85
hasMethod · 0.80
ownerOfFunction · 0.70
getMethod · 0.65
setMethod · 0.45

Tested by

no test coverage detected