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

Method formatFilesTree

src/mcp/tools.ts:9021–9091  ·  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

9019 * Format files as a tree structure
9020 */
9021 private formatFilesTree(
9022 files: { path: string; language: string; nodeCount: number }[],
9023 includeMetadata: boolean,
9024 maxDepth?: number
9025 ): string {
9026 // Build tree structure
9027 interface TreeNode {
9028 name: string;
9029 children: Map<string, TreeNode>;
9030 file?: { language: string; nodeCount: number };
9031 }
9032
9033 const root: TreeNode = { name: '', children: new Map() };
9034
9035 for (const file of files) {
9036 const parts = file.path.split('/');
9037 let current = root;
9038
9039 for (let i = 0; i < parts.length; i++) {
9040 const part = parts[i];
9041 if (!part) continue;
9042
9043 if (!current.children.has(part)) {
9044 current.children.set(part, { name: part, children: new Map() });
9045 }
9046 current = current.children.get(part)!;
9047
9048 // If this is the last part, it's a file
9049 if (i === parts.length - 1) {
9050 current.file = { language: file.language, nodeCount: file.nodeCount };
9051 }
9052 }
9053 }
9054
9055 // Render tree
9056 const lines: string[] = [`**Project Structure (${files.length} files)**`, ''];
9057
9058 const renderNode = (node: TreeNode, prefix: string, isLast: boolean, depth: number): void => {
9059 if (maxDepth !== undefined && depth > maxDepth) return;
9060
9061 const connector = isLast ? '└── ' : '├── ';
9062 const childPrefix = isLast ? ' ' : '│ ';
9063
9064 if (node.name) {
9065 let line = prefix + connector + node.name;
9066 if (node.file && includeMetadata) {
9067 line += ` (${node.file.language}, ${node.file.nodeCount} symbols)`;
9068 }
9069 lines.push(line);
9070 }
9071
9072 const children = [...node.children.values()];
9073 // Sort: directories first, then files, both alphabetically
9074 children.sort((a, b) => {
9075 const aIsDir = a.children.size > 0 && !a.file;
9076 const bIsDir = b.children.size > 0 && !b.file;
9077 if (aIsDir !== bIsDir) return aIsDir ? -1 : 1;
9078 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