@@ -20,18 +20,32 @@ import * as vscode from "vscode";
2020import * as fs from "fs" ;
2121import * as path from "path" ;
2222
23- /** 需要注入的规则文件相对路径列表 */
24- const RULES_FILES : string [ ] = [
25- "AGENTS.md" ,
26- "CLAUDE.md" ,
27- ".github/copilot-instructions.md" ,
28- ".cursor/rules/testpilot.md" ,
29- ".windsurf/rules/testpilot.md" ,
30- ".trae/rules/testpilot.md" ,
31- ".clinerules/testpilot.md" ,
32- ".aiassistant/rules/testpilot.md" ,
33- ".augment/rules/testpilot.md" ,
34- ] ;
23+ /** IDE类型到规则文件的映射 */
24+ const IDE_RULES_MAP : Record < string , string > = {
25+ cursor : ".cursor/rules/testpilot.md" ,
26+ windsurf : ".windsurf/rules/testpilot.md" ,
27+ vscode : ".github/copilot-instructions.md" ,
28+ vscodium : ".github/copilot-instructions.md" ,
29+ cline : ".clinerules/testpilot.md" ,
30+ jetbrains : ".aiassistant/rules/testpilot.md" ,
31+ trae : ".trae/rules/testpilot.md" ,
32+ augment : ".augment/rules/testpilot.md" ,
33+ claude : "CLAUDE.md" ,
34+ } ;
35+
36+ /**
37+ * 检测当前运行的IDE类型
38+ * @returns IDE类型标识符(小写)
39+ */
40+ function detectCurrentIDE ( ) : string {
41+ const appName = ( vscode . env . appName || "" ) . toLowerCase ( ) ;
42+
43+ if ( appName . includes ( "windsurf" ) ) return "windsurf" ;
44+ if ( appName . includes ( "cursor" ) ) return "cursor" ;
45+ if ( appName . includes ( "vscodium" ) ) return "vscodium" ;
46+ // 默认是 VS Code
47+ return "vscode" ;
48+ }
3549
3650/** 规则模板内容(内嵌,避免依赖外部文件) */
3751function getTemplateContent ( ) : string {
@@ -189,20 +203,42 @@ function getTemplateContent(): string {
189203}
190204
191205/**
192- * 在指定工作区目录中注入规则文件
206+ * 智能注入规则文件(只注入当前IDE对应的文件)
193207 * @param workspaceRoot 工作区根目录绝对路径
194208 * @param outputChannel 日志输出通道
209+ * @param forceAll 是否强制注入所有IDE规则(手动触发时用)
195210 * @returns 注入结果 { created: string[], skipped: string[] }
196211 */
197212export function injectRules (
198213 workspaceRoot : string ,
199214 outputChannel ?: vscode . OutputChannel ,
215+ forceAll = false ,
200216) : { created : string [ ] ; skipped : string [ ] } {
201217 const template = getTemplateContent ( ) ;
202218 const created : string [ ] = [ ] ;
203219 const skipped : string [ ] = [ ] ;
204220
205- for ( const relPath of RULES_FILES ) {
221+ // 确定要注入的文件列表
222+ let filesToInject : string [ ] ;
223+
224+ if ( forceAll ) {
225+ // 强制注入所有IDE规则(手动触发时)
226+ filesToInject = [ "AGENTS.md" , ...Object . values ( IDE_RULES_MAP ) ] ;
227+ } else {
228+ // 智能注入:AGENTS.md + 当前IDE专用文件
229+ const currentIDE = detectCurrentIDE ( ) ;
230+ filesToInject = [ "AGENTS.md" ] ;
231+
232+ const ideRuleFile = IDE_RULES_MAP [ currentIDE ] ;
233+ if ( ideRuleFile ) {
234+ filesToInject . push ( ideRuleFile ) ;
235+ }
236+
237+ outputChannel ?. appendLine ( `[TestPilot AI] 检测到当前IDE: ${ currentIDE } ` ) ;
238+ }
239+
240+ // 注入文件
241+ for ( const relPath of filesToInject ) {
206242 const fullPath = path . join ( workspaceRoot , relPath ) ;
207243
208244 // 已存在则跳过(不覆盖用户自定义内容)
@@ -242,12 +278,11 @@ export function injectRules(
242278}
243279
244280/**
245- * 检查工作区是否需要注入规则(没有任何规则文件时才需要 )
281+ * 检查工作区是否需要注入规则(没有AGENTS.md时才需要 )
246282 */
247283export function needsInjection ( workspaceRoot : string ) : boolean {
248- return ! RULES_FILES . some ( ( relPath ) =>
249- fs . existsSync ( path . join ( workspaceRoot , relPath ) ) ,
250- ) ;
284+ // 只检查 AGENTS.md 是否存在(跨工具通用文件)
285+ return ! fs . existsSync ( path . join ( workspaceRoot , "AGENTS.md" ) ) ;
251286}
252287
253288/**
0 commit comments