Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | import * as vscode from 'vscode' import * as path from 'path' export interface WorkspaceTreeNode { name: string key: string isLeaf: boolean type: 'file' | 'folder' children?: WorkspaceTreeNode[] } /** * 获取工作区目录结构 */ export async function getWorkspaceTree(): Promise<WorkspaceTreeNode[]> { const workspaceFolders = vscode.workspace.workspaceFolders if (!workspaceFolders || workspaceFolders.length === 0) { return [] } const trees: WorkspaceTreeNode[] = [] for (const folder of workspaceFolders) { const tree = await buildTreeFromUri(folder.uri, folder.name) if (tree) { trees.push(tree) } } return trees } /** * 从URI构建目录树 */ async function buildTreeFromUri(uri: vscode.Uri, name?: string): Promise<WorkspaceTreeNode | null> { try { const stat = await vscode.workspace.fs.stat(uri) const isDirectory = stat.type === vscode.FileType.Directory const node: WorkspaceTreeNode = { name: name || path.basename(uri.fsPath), key: uri.fsPath, isLeaf: !isDirectory, type: isDirectory ? 'folder' : 'file', } if (isDirectory) { try { const entries = await vscode.workspace.fs.readDirectory(uri) const children: WorkspaceTreeNode[] = [] // 过滤和排序 const filteredEntries = entries .filter(([name]) => !shouldIgnoreFile(name)) .sort(([nameA, typeA], [nameB, typeB]) => { // 文件夹优先,然后按名称排序 if (typeA === vscode.FileType.Directory && typeB !== vscode.FileType.Directory) { return -1 } if (typeA !== vscode.FileType.Directory && typeB === vscode.FileType.Directory) { return 1 } return nameA.localeCompare(nameB) }) // 限制深度和数量,避免过大的目录树 const maxChildren = 50 const limitedEntries = filteredEntries.slice(0, maxChildren) for (const [entryName, entryType] of limitedEntries) { const childUri = vscode.Uri.joinPath(uri, entryName) const childNode = await buildTreeFromUri(childUri) if (childNode) { children.push(childNode) } } node.children = children } catch (error) { // 如果读取目录失败,仍然返回节点,但没有子节点 console.warn(`Failed to read directory ${uri.fsPath}:`, error) node.children = [] } } return node } catch (error) { console.warn(`Failed to stat ${uri.fsPath}:`, error) return null } } /** * 判断是否应该忽略文件/文件夹 */ function shouldIgnoreFile(name: string): boolean { const ignorePatterns = [ // 系统文件 '.DS_Store', 'Thumbs.db', // 版本控制 '.git', '.svn', '.hg', // 依赖目录 'node_modules', '.pnpm', 'bower_components', 'vendor', // 构建输出 'dist', 'build', 'out', '.next', '.nuxt', // 缓存和临时文件 '.cache', '.tmp', '.temp', '.vscode', '.idea', // 日志文件 '*.log', 'logs', // 其他常见忽略文件 '.env', '.env.local', '.env.development.local', '.env.test.local', '.env.production.local', ] return ignorePatterns.some(pattern => { if (pattern.includes('*')) { // 简单的通配符匹配 const regex = new RegExp(pattern.replace('*', '.*')) return regex.test(name) } return name === pattern }) } /** * 将树形结构转换为扁平列表(用于提及功能) */ export function flattenWorkspaceTree(nodes: WorkspaceTreeNode[], parentPath = ''): Array<{ label: string key: string isLeaf: boolean type: 'file' | 'folder' relativePath: string }> { let files: Array<{ label: string key: string isLeaf: boolean type: 'file' | 'folder' relativePath: string }> = [] for (const node of nodes) { const relativePath = parentPath ? `${parentPath}/${node.name}` : node.name files.push({ label: node.name, key: node.key, isLeaf: node.isLeaf, type: node.type, relativePath, }) if (node.children && node.children.length > 0) { files = files.concat(flattenWorkspaceTree(node.children, relativePath)) } } return files } |