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

Function blockFor

src/ui-server/api/program.ts:161–202  ·  view source on GitHub ↗

One function's body, in the code's order.

(input: ProgramInput, fn: string, path: readonly string[], state: Reading)

Source from the content-addressed store, hash-verified

159
160/** One function's body, in the code's order. */
161function blockFor(input: ProgramInput, fn: string, path: readonly string[], state: Reading): WireBlock {
162 const sites = [...(input.sites.get(fn) ?? [])].sort(compareSites);
163 if (sites.length === 0) return [];
164
165 const root: WireBlock = [];
166 /** The constructs open at the site being placed, outermost first. */
167 const stack: Open[] = [];
168 const bodyAt = (depth: number): WireBlock => (depth === 0 ? root : stack[depth - 1]!.body);
169
170 for (const site of sites) {
171 const scopes = scopesOf(site);
172
173 // The longest run of open constructs the site still sits inside — same
174 // construct AND, for a fork, the same arm of it.
175 let keep = 0;
176 while (keep < stack.length && keep < scopes.length && sameScope(stack[keep]!, scopes[keep]!)) keep++;
177 // The construct after it may still be the SAME decision taken the other
178 // way — an `else`, another `case`, the code after an early exit. That keeps
179 // the fork and opens its other arm; anything deeper is closed either way.
180 const open = stack[keep];
181 const scope = scopes[keep];
182 if (open && scope && open.branch === scope.branch && open.fork && scope.kind === 'guard') {
183 stack.length = keep + 1;
184 open.armKey = armKey(scope.guard);
185 open.body = armFor(open.fork, scope.guard).body;
186 keep++;
187 } else {
188 stack.length = keep;
189 }
190 for (let i = keep; i < scopes.length; i++) stack.push(openScope(bodyAt(i), scopes[i]!));
191
192 const item = itemFor(input, site, path, state);
193 if (item !== null) {
194 state.items++;
195 place(bodyAt(scopes.length), item, site);
196 }
197 }
198
199 // An arm that answers the request, or whose code leaves, stops there.
200 seal(input, root);
201 return root;
202}
203
204/** A construct the reading is inside: a loop, or one arm of a fork. */
205interface Open {

Callers 2

buildProgramFunction · 0.85
itemForFunction · 0.85

Calls 11

scopesOfFunction · 0.85
armKeyFunction · 0.85
armForFunction · 0.85
openScopeFunction · 0.85
bodyAtFunction · 0.85
itemForFunction · 0.85
sealFunction · 0.85
pushMethod · 0.80
sameScopeFunction · 0.70
placeFunction · 0.70
getMethod · 0.65

Tested by

no test coverage detected