| 130 | * Format a subgraph as an ASCII tree structure |
| 131 | */ |
| 132 | export function formatSubgraphTree(subgraph: Subgraph, entryPoints: Node[]): string { |
| 133 | const lines: string[] = []; |
| 134 | const printed = new Set<string>(); |
| 135 | |
| 136 | // Build adjacency list for outgoing edges |
| 137 | const outgoing = new Map<string, Edge[]>(); |
| 138 | for (const edge of subgraph.edges) { |
| 139 | const existing = outgoing.get(edge.source) ?? []; |
| 140 | existing.push(edge); |
| 141 | outgoing.set(edge.source, existing); |
| 142 | } |
| 143 | |
| 144 | // Print each entry point as a tree root |
| 145 | for (const entry of entryPoints) { |
| 146 | formatNodeTree(entry, subgraph, outgoing, printed, lines, 0, ''); |
| 147 | lines.push(''); // Blank line between trees |
| 148 | } |
| 149 | |
| 150 | // Print any remaining nodes not reached from entry points |
| 151 | const remaining: Node[] = []; |
| 152 | for (const node of subgraph.nodes.values()) { |
| 153 | if (!printed.has(node.id)) { |
| 154 | remaining.push(node); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | if (remaining.length > 0 && remaining.length <= 10) { |
| 159 | lines.push('Other relevant symbols:'); |
| 160 | for (const node of remaining) { |
| 161 | const location = node.startLine ? `:${node.startLine}` : ''; |
| 162 | lines.push(` ${node.kind}: ${node.name} (${node.filePath}${location})`); |
| 163 | } |
| 164 | } else if (remaining.length > 10) { |
| 165 | lines.push(`... and ${remaining.length} more related symbols`); |
| 166 | } |
| 167 | |
| 168 | return lines.join('\n').trim(); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Format a single node and its relationships |