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

Method formatFilesTree

src/mcp/tools.ts:9333–9403  ·  view source on GitHub ↗

* Format files as a tree structure

(
    files: { path: string; language: string; nodeCount: number }[],
    includeMetadata: boolean,
    maxDepth?: number
  )

Source from the content-addressed store, hash-verified

9331 * Format files as a tree structure
9332 */
9333 private formatFilesTree(
9334 files: { path: string; language: string; nodeCount: number }[],
9335 includeMetadata: boolean,
9336 maxDepth?: number
9337 ): string {
9338 // Build tree structure
9339 interface TreeNode {
9340 name: string;
9341 children: Map<string, TreeNode>;
9342 file?: { language: string; nodeCount: number };
9343 }
9344
9345 const root: TreeNode = { name: '', children: new Map() };
9346
9347 for (const file of files) {
9348 const parts = file.path.split('/');
9349 let current = root;
9350
9351 for (let i = 0; i < parts.length; i++) {
9352 const part = parts[i];
9353 if (!part) continue;
9354
9355 if (!current.children.has(part)) {
9356 current.children.set(part, { name: part, children: new Map() });
9357 }
9358 current = current.children.get(part)!;
9359
9360 // If this is the last part, it's a file
9361 if (i === parts.length - 1) {
9362 current.file = { language: file.language, nodeCount: file.nodeCount };
9363 }
9364 }
9365 }
9366
9367 // Render tree
9368 const lines: string[] = [`**Project Structure (${files.length} files)**`, ''];
9369
9370 const renderNode = (node: TreeNode, prefix: string, isLast: boolean, depth: number): void => {
9371 if (maxDepth !== undefined && depth > maxDepth) return;
9372
9373 const connector = isLast ? '└── ' : '├── ';
9374 const childPrefix = isLast ? ' ' : '│ ';
9375
9376 if (node.name) {
9377 let line = prefix + connector + node.name;
9378 if (node.file && includeMetadata) {
9379 line += ` (${node.file.language}, ${node.file.nodeCount} symbols)`;
9380 }
9381 lines.push(line);
9382 }
9383
9384 const children = [...node.children.values()];
9385 // Sort: directories first, then files, both alphabetically
9386 children.sort((a, b) => {
9387 const aIsDir = a.children.size > 0 && !a.file;
9388 const bIsDir = b.children.size > 0 && !b.file;
9389 if (aIsDir !== bIsDir) return aIsDir ? -1 : 1;
9390 return a.name.localeCompare(b.name);

Callers 1

handleFilesMethod · 0.95

Calls 5

renderNodeFunction · 0.85
hasMethod · 0.80
getMethod · 0.65
setMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected