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

Method formatFilesTree

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

8872 * Format files as a tree structure
8873 */
8874 private formatFilesTree(
8875 files: { path: string; language: string; nodeCount: number }[],
8876 includeMetadata: boolean,
8877 maxDepth?: number
8878 ): string {
8879 // Build tree structure
8880 interface TreeNode {
8881 name: string;
8882 children: Map<string, TreeNode>;
8883 file?: { language: string; nodeCount: number };
8884 }
8885
8886 const root: TreeNode = { name: '', children: new Map() };
8887
8888 for (const file of files) {
8889 const parts = file.path.split('/');
8890 let current = root;
8891
8892 for (let i = 0; i < parts.length; i++) {
8893 const part = parts[i];
8894 if (!part) continue;
8895
8896 if (!current.children.has(part)) {
8897 current.children.set(part, { name: part, children: new Map() });
8898 }
8899 current = current.children.get(part)!;
8900
8901 // If this is the last part, it's a file
8902 if (i === parts.length - 1) {
8903 current.file = { language: file.language, nodeCount: file.nodeCount };
8904 }
8905 }
8906 }
8907
8908 // Render tree
8909 const lines: string[] = [`**Project Structure (${files.length} files)**`, ''];
8910
8911 const renderNode = (node: TreeNode, prefix: string, isLast: boolean, depth: number): void => {
8912 if (maxDepth !== undefined && depth > maxDepth) return;
8913
8914 const connector = isLast ? '└── ' : '├── ';
8915 const childPrefix = isLast ? ' ' : '│ ';
8916
8917 if (node.name) {
8918 let line = prefix + connector + node.name;
8919 if (node.file && includeMetadata) {
8920 line += ` (${node.file.language}, ${node.file.nodeCount} symbols)`;
8921 }
8922 lines.push(line);
8923 }
8924
8925 const children = [...node.children.values()];
8926 // Sort: directories first, then files, both alphabetically
8927 children.sort((a, b) => {
8928 const aIsDir = a.children.size > 0 && !a.file;
8929 const bIsDir = b.children.size > 0 && !b.file;
8930 if (aIsDir !== bIsDir) return aIsDir ? -1 : 1;
8931 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