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

Method getFilteredSubgraph

src/graph/queries.ts:304–354  ·  view source on GitHub ↗

* Get subgraph containing nodes matching a filter * * @param filter - Filter function to select nodes * @param includeEdges - Whether to include edges between matching nodes * @returns Subgraph containing matching nodes

(
    filter: (node: Node) => boolean,
    includeEdges: boolean = true
  )

Source from the content-addressed store, hash-verified

302 * @returns Subgraph containing matching nodes
303 */
304 getFilteredSubgraph(
305 filter: (node: Node) => boolean,
306 includeEdges: boolean = true
307 ): Subgraph {
308 const nodes = new Map<string, Node>();
309 const edges: Edge[] = [];
310
311 // Get all nodes of common kinds
312 const kinds: Node['kind'][] = [
313 'file',
314 'module',
315 'class',
316 'struct',
317 'union',
318 'interface',
319 'trait',
320 'function',
321 'method',
322 'variable',
323 'constant',
324 'enum',
325 'type_alias',
326 ];
327
328 for (const kind of kinds) {
329 const kindNodes = this.queries.getNodesByKind(kind);
330 for (const node of kindNodes) {
331 if (filter(node)) {
332 nodes.set(node.id, node);
333 }
334 }
335 }
336
337 // Include edges between matching nodes
338 if (includeEdges) {
339 for (const nodeId of nodes.keys()) {
340 const outgoing = this.queries.getOutgoingEdges(nodeId);
341 for (const edge of outgoing) {
342 if (nodes.has(edge.target)) {
343 edges.push(edge);
344 }
345 }
346 }
347 }
348
349 return {
350 nodes,
351 edges,
352 roots: [],
353 };
354 }
355
356 /**
357 * Access the underlying traverser for direct traversal operations

Callers

nothing calls this directly

Calls 5

hasMethod · 0.80
pushMethod · 0.80
getNodesByKindMethod · 0.65
setMethod · 0.45
getOutgoingEdgesMethod · 0.45

Tested by

no test coverage detected