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

Function nixOptionPathEdges

src/resolution/callback-synthesizer.ts:4876–4989  ·  view source on GitHub ↗
(queries: QueryBuilder, onYield: MaybeYield)

Source from the content-addressed store, hash-verified

4874}
4875
4876async function nixOptionPathEdges(queries: QueryBuilder, onYield: MaybeYield): Promise<Edge[]> {
4877 let scanned255 = 0;
4878 type Rec = { id: string; filePath: string; startLine: number; endLine: number; segs: string[] };
4879
4880 // One streaming pass over nix bindings (variables + the odd function-valued
4881 // option); memory stays O(bindings-kept), not O(all nodes) (#610).
4882 const byFile = new Map<string, Rec[]>();
4883 let scanned = 0;
4884 for (const kind of ['variable', 'function'] as NodeKind[]) {
4885 for (const node of queries.iterateNodesByKindIn(kind, ['nix'])) {
4886 if ((++scanned255 & 63) === 0) await onYield();
4887 if ((++scanned & 0x3fff) === 0 && onYield) await onYield();
4888 const segs = nixLeadingPlainSegments(node.name);
4889 if (segs.length === 0) continue;
4890 const rec: Rec = {
4891 id: node.id,
4892 filePath: node.filePath,
4893 startLine: node.startLine,
4894 endLine: node.endLine,
4895 segs,
4896 };
4897 const arr = byFile.get(node.filePath);
4898 if (arr) arr.push(rec);
4899 else byFile.set(node.filePath, [rec]);
4900 }
4901 }
4902
4903 // Per file: walk bindings outermost-first with a stack of active option
4904 // spans, composing nested declaration paths (`options = { services.foo = {
4905 // enable = mkOption ...; }; }` registers services.foo AND services.foo.enable).
4906 // An `options` binding nested inside another option span is a SUBMODULE's
4907 // own namespace (`attrsOf (submodule { options = ...; })`) — its internals
4908 // are not globally addressable, so the sentinel blocks registration below it
4909 // while still excluding the region from write candidates.
4910 const SUBMODULE = '\u0000submodule';
4911 const decls = new Map<string, Rec[]>();
4912 const writes: Rec[] = [];
4913 const register = (path: string[], rec: Rec) => {
4914 if (path.length < 2 || path.includes(SUBMODULE)) return;
4915 const key = path.join('.');
4916 const arr = decls.get(key);
4917 if (arr) arr.push(rec);
4918 else decls.set(key, [rec]);
4919 };
4920 for (const recs of byFile.values()) {
4921 recs.sort((a, b) => a.startLine - b.startLine || b.endLine - a.endLine);
4922 const stack: Array<{ start: number; end: number; prefix: string[] }> = [];
4923 for (const rec of recs) {
4924 while (stack.length > 0 && stack[stack.length - 1]!.end < rec.startLine) stack.pop();
4925 // Strict containment at line granularity: a one-line nested binding is
4926 // indistinguishable from its container, so it stays unclassified (rare
4927 // in module code, where option blocks are multi-line).
4928 const enclosing =
4929 stack.length > 0 &&
4930 rec.startLine >= stack[stack.length - 1]!.start &&
4931 rec.endLine <= stack[stack.length - 1]!.end &&
4932 !(rec.startLine === stack[stack.length - 1]!.start && rec.endLine === stack[stack.length - 1]!.end)
4933 ? stack[stack.length - 1]!

Callers 1

Calls 8

nixLeadingPlainSegmentsFunction · 0.85
iterateNodesByKindInMethod · 0.80
pushMethod · 0.80
mapMethod · 0.80
registerFunction · 0.70
getMethod · 0.65
setMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected