11import * as vscode from 'vscode' ;
2-
2+ import * as fs from 'fs' ;
3+ import * as path from 'path' ;
34import { g_objLanguageMapping } from './languageMapping' ;
45
56// 配置常量
67const ALLOWED_FILENAMES = [ 'package.json' ] ; // 文件名白名单
78const INCLUDED_EXTENSIONS = Object . keys ( g_objLanguageMapping ) ; // 扩展名白名单
89const EXCLUDED_DIRECTORIES = [ 'node_modules' , '.git' , 'build' , 'dist' , 'out' , 'vendor' ] ; // 排除目录
910
11+ /**
12+ * 解析 ignore 文件内容并返回匹配模式数组
13+ * @param filePath ignore 文件路径
14+ * @returns 过滤模式数组
15+ */
16+ function parseIgnoreFile ( filePath : string ) : string [ ] {
17+ try {
18+ const content = fs . readFileSync ( filePath , 'utf8' ) ;
19+ return content
20+ . split ( '\n' )
21+ . map ( line => line . trim ( ) )
22+ . filter ( line =>
23+ line && // 非空行
24+ ! line . startsWith ( '#' ) && // 不是注释
25+ ! line . startsWith ( '!' ) // 不是反向模式
26+ )
27+ . map ( pattern => {
28+ // 转换 gitignore 风格的模式为 glob 模式
29+ if ( pattern . endsWith ( '/' ) ) {
30+ return `**/${ pattern } **` ;
31+ }
32+ return `**/${ pattern } ` ;
33+ } ) ;
34+ } catch ( error ) {
35+ return [ ] ;
36+ }
37+ }
38+
1039/**
1140 * 显示文件选择器,并返回用户选择的文件列表
1241 * @returns 用户选择的文件路径数组
@@ -19,13 +48,30 @@ export async function selectFiles(): Promise<string[]> {
1948 return [ ] ;
2049 }
2150
22- const excludePattern = `{${ EXCLUDED_DIRECTORIES . map ( dir => `**/${ dir } /**` ) . join ( ',' ) } }` ;
51+ const rootPath = workspaceFolders [ 0 ] . uri . fsPath ;
52+
53+ // 构建默认排除模式数组
54+ const defaultExcludePatterns = EXCLUDED_DIRECTORIES . map ( dir => `**/${ dir } /**` ) ;
55+
56+ // 获取 ignore 文件的模式
57+ let ignorePatterns : string [ ] = [ ] ;
58+ const gitignorePath = path . join ( rootPath , '.gitignore' ) ;
59+
60+ if ( fs . existsSync ( gitignorePath ) ) {
61+ ignorePatterns = ignorePatterns . concat ( parseIgnoreFile ( gitignorePath ) ) ;
62+ }
63+
64+ // 合并所有排除模式并确保是扁平结构
65+ const allExcludePatterns = [ ...defaultExcludePatterns , ...ignorePatterns ] ;
66+ const excludePattern = allExcludePatterns . length > 0
67+ ? `{${ allExcludePatterns . join ( ',' ) } }`
68+ : undefined ;
2369
2470 // 1. 匹配白名单文件名
2571 const filenamePattern = `**/{${ ALLOWED_FILENAMES . join ( ',' ) } }` ;
2672 const filenameFiles = await vscode . workspace . findFiles ( filenamePattern , excludePattern ) ;
2773
28- // 2. 匹配扩展名文件,并排除指定目录
74+ // 2. 匹配扩展名文件
2975 const extensionPattern = `**/*.{${ INCLUDED_EXTENSIONS . join ( ',' ) } }` ;
3076 const extensionFiles = await vscode . workspace . findFiles ( extensionPattern , excludePattern ) ;
3177
0 commit comments