MCPcopy
hub / github.com/colbymchenry/codegraph / formatFilesTree

Method formatFilesTree

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

4173 * Format files as a tree structure
4174 */
4175 private formatFilesTree(
4176 files: { path: string; language: string; nodeCount: number }[],
4177 includeMetadata: boolean,
4178 maxDepth?: number
4179 ): string {
4180 // Build tree structure
4181 interface TreeNode {
4182 name: string;
4183 children: Map<string, TreeNode>;
4184 file?: { language: string; nodeCount: number };
4185 }
4186
4187 const root: TreeNode = { name: '', children: new Map() };
4188
4189 for (const file of files) {
4190 const parts = file.path.split('/');
4191 let current = root;
4192
4193 for (let i = 0; i < parts.length; i++) {
4194 const part = parts[i];
4195 if (!part) continue;
4196
4197 if (!current.children.has(part)) {
4198 current.children.set(part, { name: part, children: new Map() });
4199 }
4200 current = current.children.get(part)!;
4201
4202 // If this is the last part, it's a file
4203 if (i === parts.length - 1) {
4204 current.file = { language: file.language, nodeCount: file.nodeCount };
4205 }
4206 }
4207 }
4208
4209 // Render tree
4210 const lines: string[] = [`**Project Structure (${files.length} files)**`, ''];
4211
4212 const renderNode = (node: TreeNode, prefix: string, isLast: boolean, depth: number): void => {
4213 if (maxDepth !== undefined && depth > maxDepth) return;
4214
4215 const connector = isLast ? '└── ' : '├── ';
4216 const childPrefix = isLast ? ' ' : '│ ';
4217
4218 if (node.name) {
4219 let line = prefix + connector + node.name;
4220 if (node.file && includeMetadata) {
4221 line += ` (${node.file.language}, ${node.file.nodeCount} symbols)`;
4222 }
4223 lines.push(line);
4224 }
4225
4226 const children = [...node.children.values()];
4227 // Sort: directories first, then files, both alphabetically
4228 children.sort((a, b) => {
4229 const aIsDir = a.children.size > 0 && !a.file;
4230 const bIsDir = b.children.size > 0 && !b.file;
4231 if (aIsDir !== bIsDir) return aIsDir ? -1 : 1;
4232 return a.name.localeCompare(b.name);

Callers 1

handleFilesMethod · 0.95

Calls 5

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

Tested by

no test coverage detected