Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/util/common/fileTree.ts
13397 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import type * as vscode from 'vscode';
7
8
export function fileTreePartToMarkdown(fileTree: vscode.ChatResponseFileTreePart) {
9
const printTree = (node: vscode.ChatResponseFileTree, depth: number, isLastChild: boolean): string => {
10
let output: string = '';
11
const indent = isLastChild ? '└── ' : '├── ';
12
if (depth === 1) {
13
output = `${indent}${node.name}\n`;
14
} else if (depth > 1) {
15
output = `| ${' '.repeat(depth - 2)}${indent}${node.name}\n`;
16
}
17
if (node.children) {
18
const lastChildIndex = node.children.length - 1;
19
node.children.forEach((child, index) => {
20
output += printTree(child, depth + 1, index === lastChildIndex);
21
});
22
}
23
return output;
24
};
25
26
// Start with an empty string or the base directory if provided
27
let markdown = `${fileTree.baseUri.path.replace('/', '')}\n`;
28
const lastChildIndex = fileTree.value.length - 1;
29
fileTree.value.forEach((tree, index) => {
30
markdown += printTree(tree, 1, index === lastChildIndex);
31
});
32
return `\`\`\`filetree\n${markdown}\`\`\`\n`;
33
}
34