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

Function pickClosestFileNode

src/resolution/name-matcher.ts:113–133  ·  view source on GitHub ↗

* Among several file nodes that all match a bare include/import by basename, * pick the one closest to the referencing file: same directory first, then by * directory-tree proximity, with the same language family as a tiebreak. A * C/C++ `#include "X.h"` (and any bare-filename import) resolves re

(candidates: Node[], ref: UnresolvedRef)

Source from the content-addressed store, hash-verified

111 * including file — not to an arbitrary same-named header on another platform.
112 */
113function pickClosestFileNode(candidates: Node[], ref: UnresolvedRef): Node {
114 const dirOf = (p: string): string => {
115 const i = p.lastIndexOf('/');
116 return i >= 0 ? p.slice(0, i) : '';
117 };
118 const refDir = dirOf(ref.filePath);
119 const sameDir = candidates.filter((c) => dirOf(c.filePath) === refDir);
120 const pool = sameDir.length > 0 ? sameDir : candidates;
121 let best = pool[0]!;
122 let bestScore = -Infinity;
123 for (const c of pool) {
124 const score =
125 computePathProximity(ref.filePath, c.filePath) +
126 (sameLanguageFamily(c.language, ref.language) ? 5 : 0);
127 if (score > bestScore) {
128 bestScore = score;
129 best = c;
130 }
131 }
132 return best;
133}
134
135/**
136 * Language families that share a type system / runtime, so a same-language-only

Callers 1

matchByFilePathFunction · 0.85

Calls 3

computePathProximityFunction · 0.85
sameLanguageFamilyFunction · 0.85
dirOfFunction · 0.70

Tested by

no test coverage detected