Skip to content

Commit 17b4e3d

Browse files
committed
输出文件也支持取名了
1 parent 28fc501 commit 17b4e3d

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

src/cvbManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function isLikelyGBK(buffer: Buffer): boolean {
7979
return false;
8080
}
8181

82-
function generateTimestamp(): string {
82+
export function generateTimestamp(): string {
8383
const now = new Date();
8484
const year = now.getFullYear().toString().slice(-2);
8585
const month = (now.getMonth() + 1).toString().padStart(2, '0');

src/extension.ts

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import * as vscode from 'vscode';
22
import * as path from 'path';
33
import * as fs from 'fs';
44
import { selectFiles } from './fileSelector';
5-
import { generateCvb, parseCvb, applyCvbToWorkspace } from './cvbManager';
6-
import { callDeepSeekApi } from './deepseekApi';
5+
import { generateCvb, parseCvb, applyCvbToWorkspace, generateTimestamp } from './cvbManager';
6+
import { callDeepSeekApi, generateFilenameFromRequest} from './deepseekApi';
77
import { setupCvbAsMarkdown } from './cvbMarkdownHandler';
88

99
// 插件激活时调用
@@ -48,49 +48,51 @@ export function activate(context: vscode.ExtensionContext) {
4848
vscode.window.showErrorMessage('No workspace folder found.');
4949
return;
5050
}
51-
51+
5252
const workspacePath = workspaceFolders[0].uri.fsPath;
5353
const tmpDir = path.join(workspacePath, 'CodeReDesignWorkSpace', 'tmp');
5454
const cvbFiles = fs.readdirSync(tmpDir).filter((file: string) => file.endsWith('.cvb'));
55-
55+
5656
if (cvbFiles.length === 0) {
5757
vscode.window.showErrorMessage('No CVB files found in the tmp directory.');
5858
return;
5959
}
60-
61-
// 让用户选择要上传的 CVB 文件
60+
6261
const selectedCvbFile = await vscode.window.showQuickPick(cvbFiles, {
6362
placeHolder: 'Select a CVB file to upload',
6463
});
65-
64+
6665
if (!selectedCvbFile) {
6766
return;
6867
}
69-
70-
// 让用户输入提示词
68+
7169
const userPrompt = await vscode.window.showInputBox({
7270
prompt: 'Enter your prompt for the refactoring',
7371
placeHolder: 'e.g., Refactor the code to improve readability',
7472
});
75-
73+
7674
if (!userPrompt) {
7775
return;
7876
}
79-
80-
// 读取 CVB 文件内容
77+
78+
const filenameSummary = await generateFilenameFromRequest(userPrompt);
79+
const timestamp = generateTimestamp();
80+
let baseFileName = `${timestamp}_${filenameSummary}.cvb`;
81+
let fileName = baseFileName;
82+
let i = 1;
83+
while (fs.existsSync(path.join(tmpDir, fileName))) {
84+
fileName = `${timestamp}_${filenameSummary}_${i}.cvb`;
85+
i++;
86+
}
87+
8188
const cvbFilePath = path.join(tmpDir, selectedCvbFile);
8289
const cvbContent = fs.readFileSync(cvbFilePath, 'utf-8');
83-
84-
// 调用 DeepSeek API(流式模式)
90+
8591
const apiResponse = await callDeepSeekApi(cvbContent, userPrompt, outputChannel);
8692
if (apiResponse) {
87-
// 解析 API 返回的 CVB 内容
88-
const { cvbContent, metadata, files } = parseCvb(apiResponse);
89-
90-
// 将 API 返回的 CVB 内容保存为新文件
91-
const newCvbFilePath = path.join(tmpDir, `${new Date().getTime()}.cvb`);
92-
fs.writeFileSync(newCvbFilePath, cvbContent, 'utf-8');
93-
93+
const { cvbContent: newCvbContent, metadata, files } = parseCvb(apiResponse);
94+
const newCvbFilePath = path.join(tmpDir, fileName);
95+
fs.writeFileSync(newCvbFilePath, newCvbContent, 'utf-8');
9496
vscode.window.showInformationMessage(`API response saved as CVB file: ${newCvbFilePath}`);
9597
}
9698
});

0 commit comments

Comments
 (0)