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

Function formatNodeTree

src/context/formatter.ts:174–240  ·  view source on GitHub ↗

* Format a single node and its relationships

(
  node: Node,
  subgraph: Subgraph,
  outgoing: Map<string, Edge[]>,
  printed: Set<string>,
  lines: string[],
  depth: number,
  prefix: string
)

Source from the content-addressed store, hash-verified

172 * Format a single node and its relationships
173 */
174function formatNodeTree(
175 node: Node,
176 subgraph: Subgraph,
177 outgoing: Map<string, Edge[]>,
178 printed: Set<string>,
179 lines: string[],
180 depth: number,
181 prefix: string
182): void {
183 if (printed.has(node.id)) {
184 return;
185 }
186 printed.add(node.id);
187
188 // Node header
189 const location = node.startLine ? `:${node.startLine}` : '';
190 const signature = node.signature ? ` - ${truncate(node.signature, 50)}` : '';
191 lines.push(`${prefix}${node.kind}: ${node.name} (${node.filePath}${location})${signature}`);
192
193 // Outgoing edges
194 const edges = outgoing.get(node.id) ?? [];
195 const significantEdges = edges.filter((e) =>
196 ['calls', 'extends', 'implements', 'imports', 'references'].includes(e.kind)
197 );
198
199 // Group by kind
200 const edgesByKind = new Map<string, Edge[]>();
201 for (const edge of significantEdges) {
202 const existing = edgesByKind.get(edge.kind) ?? [];
203 existing.push(edge);
204 edgesByKind.set(edge.kind, existing);
205 }
206
207 // Print edges grouped by kind
208 const newPrefix = prefix + ' ';
209 for (const [kind, kindEdges] of edgesByKind) {
210 if (kindEdges.length > 3) {
211 // Summarize if too many
212 const names = kindEdges
213 .slice(0, 3)
214 .map((e) => {
215 const target = subgraph.nodes.get(e.target);
216 return target?.name ?? 'unknown';
217 })
218 .join(', ');
219 lines.push(`${newPrefix}├── ${kind}: ${names} and ${kindEdges.length - 3} more`);
220 } else {
221 for (let i = 0; i < kindEdges.length; i++) {
222 const edge = kindEdges[i]!;
223 const target = subgraph.nodes.get(edge.target);
224 const targetName = target?.name ?? 'unknown';
225 const connector = i === kindEdges.length - 1 ? '└──' : '├──';
226 lines.push(`${newPrefix}${connector} ${kind} → ${targetName}`);
227 }
228 }
229 }
230
231 // Recurse for directly connected nodes (limited depth)

Callers 1

formatSubgraphTreeFunction · 0.85

Calls 8

hasMethod · 0.80
pushMethod · 0.80
mapMethod · 0.80
truncateFunction · 0.70
getMethod · 0.65
addMethod · 0.45
setMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected