Skip to content

Commit 12b4e33

Browse files
committed
fix:修正文件路径匹配错误隐患
1 parent 1cb6665 commit 12b4e33

File tree

2 files changed

+46
-97
lines changed

2 files changed

+46
-97
lines changed

prompt/testdata2.txt

Lines changed: 45 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,72 @@
1-
## BEGIN_TCVB
1+
```TCVB
2+
## BEGIN_TCVB
3+
24
## FILE:k:\lab\CodeReDesign-dummy\src\siderBar.ts
35
## OPERATION:INSERT
46
## BEFORE_ANCHOR
57
```typescript
6-
getTreeItem(element: CvbFile): vscode.TreeItem {
7-
return element;
8-
}
9-
```
10-
## AFTER_ANCHOR
11-
```typescript
12-
async getChildren(element?: CvbFile): Promise<CvbFile[]> {
13-
if (element) {
14-
```
15-
## INSERT_CONTENT
16-
```typescript
17-
// 新增文件类型过滤
18-
private isSupportedFileType(filename: string): boolean {
19-
return filename.endsWith('.cvb') || filename.endsWith('.tcvb') || filename.endsWith('.md');
20-
}
21-
```
8+
export function registerCvbContextMenu(context: vscode.ExtensionContext) {
229

23-
## OPERATION:SINGLE-REPLACE
24-
## BEFORE_ANCHOR
25-
```typescript
26-
if (file.endsWith('.cvb')) {
27-
const filePath = path.join(targetFolder, file);
28-
cvbFiles.push(new CvbFile(file, vscode.Uri.file(filePath)));
29-
}
10+
// 注册右键菜单命令
11+
const applyCvbCommand = vscode.commands.registerCommand('codeReDesign.applyThisCvb', (cvb: CvbFile) => {
3012
```
3113
## AFTER_ANCHOR
3214
```typescript
33-
// 新增排序逻辑
34-
cvbFiles.sort((a, b) =>
35-
```
36-
## OLD_CONTENT
37-
```typescript
38-
if (file.endsWith('.cvb')) {
39-
const filePath = path.join(targetFolder, file);
40-
cvbFiles.push(new CvbFile(file, vscode.Uri.file(filePath)));
41-
}
42-
```
43-
## NEW_CONTENT
44-
```typescript
45-
if (this.isSupportedFileType(file)) {
46-
const filePath = path.join(targetFolder, file);
47-
cvbFiles.push(new CvbFile(file, vscode.Uri.file(filePath)));
48-
}
49-
```
15+
const workspaceFolders = vscode.workspace.workspaceFolders;
16+
if (workspaceFolders) {
17+
const targetFolder = path.join(workspaceFolders[0].uri.fsPath, '.CodeReDesignWorkSpace'); // 替换为你的子文件夹名称
5018

51-
## FILE:k:\lab\CodeReDesign-dummy\src\deepseekApi.ts
52-
## OPERATION:INSERT
53-
## BEFORE_ANCHOR
54-
```typescript
55-
fs.writeFileSync(newCvbFilePath, cvb.toString(), 'utf-8');
56-
vscode.window.showInformationMessage(`API response saved as CVB file: ${newCvbFilePath}`);
57-
```
58-
## AFTER_ANCHOR
59-
```typescript
60-
}
61-
clearCurrentOperationController();
19+
// 创建文件系统监听器
6220
```
6321
## INSERT_CONTENT
6422
```typescript
65-
// 保存原始TCVB内容
66-
const tcvbFilePath = path.join(tmpDir, fileName.replace(/\.cvb$/, '.tcvb'));
67-
fs.writeFileSync(tcvbFilePath, apiResponse, 'utf-8');
68-
```
23+
// 监听 .tcvb 和 .md 文件变化
24+
const markdownWatcher = vscode.workspace.createFileSystemWatcher(
25+
new vscode.RelativePattern(targetFolder, '**/*.md')
26+
);
6927

70-
## OPERATION:INSERT
71-
## BEFORE_ANCHOR
72-
```typescript
73-
if (analysisResult) {
74-
vscode.window.showInformationMessage('Analysis completed. Check the output channel for details.');
75-
```
76-
## AFTER_ANCHOR
77-
```typescript
78-
}
79-
clearCurrentOperationController();
80-
```
81-
## INSERT_CONTENT
82-
```typescript
83-
// 保存分析结果到Markdown
84-
const mdFileName = path.basename(filePath).replace(/\.cvb$/, '_analysis.md');
85-
const mdFilePath = path.join(path.dirname(filePath), mdFileName);
86-
fs.writeFileSync(mdFilePath, `# Analysis Report\n\n${analysisResult}`, 'utf-8');
28+
const tcvbWatcher = vscode.workspace.createFileSystemWatcher(
29+
new vscode.RelativePattern(targetFolder, '**/*.tcvb')
30+
);
31+
32+
// 当文件变化时刷新视图
33+
markdownWatcher.onDidCreate(() => cvbViewProvider.refresh());
34+
markdownWatcher.onDidDelete(() => cvbViewProvider.refresh());
35+
markdownWatcher.onDidChange(() => cvvViewProvider.refresh());
36+
37+
tcvbWatcher.onDidCreate(() => cvbViewProvider.refresh());
38+
tcvbWatcher.onDidDelete(() => cvbViewProvider.refresh());
39+
tcvbWatcher.onDidChange(() => cvbViewProvider.refresh());
40+
41+
// 将监听器添加到订阅中,确保扩展销毁时清理资源
42+
context.subscriptions.push(markdownWatcher, tcvbWatcher);
43+
}
8744
```
8845

89-
## FILE:k:\lab\CodeReDesign-dummy\src\cvbManager.ts
46+
## FILE:k:\lab\CodeReDesign-dummy\src\deepseekApi.ts
9047
## OPERATION:INSERT
9148
## BEFORE_ANCHOR
9249
```typescript
93-
class CvbFile extends vscode.TreeItem {
94-
constructor(
95-
public readonly label: string,
96-
public readonly uri: vscode.Uri
97-
) {
50+
if (apiResponse) {
51+
apiResponse = apiResponse.replace(/\r\n?/g, "\n");
52+
const tcvb = new TCVB(apiResponse);
9853
```
9954
## AFTER_ANCHOR
10055
```typescript
101-
// 设置图标(可选)
102-
this.iconPath = vscode.ThemeIcon.File;
56+
cvb.setMetaData("用户需求", userPrompt);
57+
const newCvbFilePath = path.join(tmpDir, fileName);
58+
fs.writeFileSync(newCvbFilePath, cvb.toString(), 'utf-8');
10359
```
10460
## INSERT_CONTENT
10561
```typescript
106-
// 根据文件类型设置不同图标
107-
if (uri.fsPath.endsWith('.tcvb')) {
108-
this.iconPath = new vscode.ThemeIcon('diff');
109-
} else if (uri.fsPath.endsWith('.md')) {
110-
this.iconPath = new vscode.ThemeIcon('markdown');
111-
}
62+
// 保存 TCVB 文件
63+
const tcvbFilename = path.join(tmpDir, `${path.basename(fileName, '.cvb')}.tcvb`);
64+
fs.writeFileSync(tcvbFilename, apiResponse, 'utf-8');
65+
66+
// 保存分析结果
67+
const analysisFilename = path.join(tmpDir, `${path.basename(fileName, '.cvb')}_analysis.md`);
68+
fs.writeFileSync(analysisFilename, '分析结果\n\n' + analysisResult, 'utf-8');
11269
```
11370

11471
## END_TCVB
115-
116-
这个TCVB实现了:
117-
1. 在侧边栏增加.tcvb和.md文件支持
118-
2. 上传TCVB时保存原始TCVB文件
119-
3. 分析结果保存为Markdown文件
120-
4. 不同文件类型显示不同图标
121-
5. 文件名保持与CVB文件一致(自动替换扩展名)
122-
123-
所有修改都保持原有功能完整,新增文件会在.CodReDesignWorkSpace目录生成,并能在侧边栏正确显示和操作。
72+
```

src/cvbManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export class Cvb
110110

111111
// 解析文件部分
112112
const recFiles: Record<string, string> = { };
113-
const regFile: RegExp = /^## FILE:(.*?)\n([\s\S]*?)(?=^## FILE:|^## END_CVB)/gm;
113+
const regFile: RegExp = /^## FILE:([^<\r\n]+)\n([\s\S]*?)(?=^## FILE:|^## END_CVB)/gm;
114114
let arrFileMatch: RegExpExecArray | null;
115115
while ((arrFileMatch = regFile.exec(strCvbContentPart)) !== null)
116116
{

0 commit comments

Comments
 (0)