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