MCPcopy Index your code
hub / github.com/forloopcodes/contextplus / buildTree

Function buildTree

src/tools/context-tree.ts:30–69  ·  view source on GitHub ↗
(entries: FileEntry[], _rootDir: string, includeSymbols: boolean)

Source from the content-addressed store, hash-verified

28}
29
30async function buildTree(entries: FileEntry[], _rootDir: string, includeSymbols: boolean): Promise<TreeNode> {
31 const root: TreeNode = { name: ".", relativePath: ".", isDirectory: true, children: [] };
32 const dirMap = new Map<string, TreeNode>();
33 dirMap.set(".", root);
34
35 const sortedEntries = entries.sort((a, b) => a.depth - b.depth || a.relativePath.localeCompare(b.relativePath));
36
37 for (const entry of sortedEntries) {
38 const parts = entry.relativePath.split("/");
39 const parentPath = parts.length > 1 ? parts.slice(0, -1).join("/") : ".";
40 let parent = dirMap.get(parentPath);
41 if (!parent) {
42 parent = root;
43 }
44
45 const node: TreeNode = {
46 name: parts[parts.length - 1],
47 relativePath: entry.relativePath,
48 isDirectory: entry.isDirectory,
49 children: [],
50 };
51
52 if (!entry.isDirectory && isSupportedFile(entry.path)) {
53 try {
54 const analysis = await analyzeFile(entry.path);
55 node.header = analysis.header || undefined;
56 if (includeSymbols && analysis.symbols.length > 0) {
57 node.symbols = analysis.symbols.map((s) => formatSymbol(s, 0)).join("\n");
58 }
59 } catch { }
60 }
61
62 parent.children.push(node);
63 if (entry.isDirectory) {
64 dirMap.set(entry.relativePath, node);
65 }
66 }
67
68 return root;
69}
70
71function renderTree(node: TreeNode, indent: number = 0): string {
72 let result = "";

Callers 1

getContextTreeFunction · 0.85

Calls 3

isSupportedFileFunction · 0.85
analyzeFileFunction · 0.85
formatSymbolFunction · 0.85

Tested by

no test coverage detected