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