Skip to content

Commit dcc3ec0

Browse files
committed
feat: 新增解析代码功能
1 parent 37f0901 commit dcc3ec0

File tree

3 files changed

+106
-6
lines changed

3 files changed

+106
-6
lines changed

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"onCommand:codeReDesign.generateCvb",
1717
"onCommand:codeReDesign.uploadCvb",
1818
"onCommand:codeReDesign.applyCvb",
19-
"onCommand:codeReDesign.applyThisCvb"
19+
"onCommand:codeReDesign.applyThisCvb",
20+
"onCommand:codeReDesign.analyzeCode"
2021
],
2122
"repository": {
2223
"type": "git",
@@ -43,6 +44,10 @@
4344
{
4445
"command": "codeReDesign.stopOperation",
4546
"title": "CodeReDesign: Stop Operation"
47+
},
48+
{
49+
"command": "codeReDesign.analyzeCode",
50+
"title": "CodeReDesign: Analyze Code"
4651
}
4752
],
4853
"views": {

src/deepseekApi.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ async function callDeepSeekApi(
145145

146146
if (!shouldContinue) {break};
147147

148+
if (abortSignal?.aborted) {
149+
throw new Error('operation stop by user');
150+
}
151+
148152
vscode.window.showWarningMessage('超过最大Token数,正在重试...');
149153

150154
// 准备下一次请求
@@ -213,6 +217,38 @@ ${userRequest}
213217
return callDeepSeekApi(requestContent, undefined, outputChannel, true, '## END_CVB', abortSignal); // 添加结束字符串
214218
}
215219

220+
/**
221+
* 分析代码
222+
* @param cvbContent CVB 文件内容
223+
* @param userRequest 用户输入的分析需求
224+
* @param outputChannel 输出通道,用于实时显示流式内容
225+
* @returns API 返回的分析结果
226+
*/
227+
export async function analyzeCode(
228+
cvbContent: string,
229+
userRequest: string,
230+
outputChannel: vscode.OutputChannel,
231+
abortSignal?: AbortSignal
232+
): Promise<string | null> {
233+
const requestContent = `
234+
235+
这是 CVB 格式的说明:
236+
${getCvbFormatDescription()}
237+
238+
请读取以下 CVB 格式的代码,按照需求进行分析,
239+
240+
输入代码:
241+
${cvbContent}
242+
243+
这是我的需求:
244+
${userRequest}
245+
246+
请输出分析结果:
247+
`;
248+
249+
return callDeepSeekApi(requestContent, "你是一个代码分析助手", outputChannel, true, undefined, abortSignal);
250+
}
251+
216252
/**
217253
* 清理文件名
218254
* @param str 原始字符串

src/extension.ts

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as path from 'path';
33
import * as fs from 'fs';
44
import { selectFiles } from './fileSelector';
55
import { generateCvb, parseCvb, applyCvbToWorkspace, generateTimestamp } from './cvbManager';
6-
import { queryCodeReDesign, generateFilenameFromRequest } from './deepseekApi';
6+
import { queryCodeReDesign, generateFilenameFromRequest, analyzeCode } from './deepseekApi';
77
import { setupCvbAsMarkdown } from './cvbMarkdownHandler';
88
import { registerCvbContextMenu } from './siderBar';
99

@@ -13,6 +13,8 @@ export function activate(context: vscode.ExtensionContext) {
1313

1414
// 创建输出通道
1515
const outputChannel = vscode.window.createOutputChannel('CodeReDesign API Stream');
16+
// 用于存储当前的上传操作
17+
let currentOperationController: AbortController | null = null;
1618

1719
// 注册命令:选择文件并生成 CVB
1820
let generateCvbCommand = vscode.commands.registerCommand('codeReDesign.generateCvb', async () => {
@@ -41,9 +43,6 @@ export function activate(context: vscode.ExtensionContext) {
4143
vscode.window.showInformationMessage(`CVB file generated at: ${cvbFilePath}`);
4244
});
4345

44-
// 用于存储当前的上传操作
45-
let currentOperationController: AbortController | null = null;
46-
4746
// 注册命令:上传 CVB 并调用 API
4847
let uploadCvbCommand = vscode.commands.registerCommand('codeReDesign.uploadCvb', async () => {
4948
const workspaceFolders = vscode.workspace.workspaceFolders;
@@ -90,6 +89,11 @@ export function activate(context: vscode.ExtensionContext) {
9089

9190
const cvbFilePath = path.join(tmpDir, selectedCvbFile);
9291
const cvbContent = fs.readFileSync(cvbFilePath, 'utf-8');
92+
93+
if (currentOperationController){
94+
currentOperationController.abort();
95+
currentOperationController = null;
96+
}
9397

9498
// 创建新的 AbortController
9599
currentOperationController = new AbortController();
@@ -152,7 +156,62 @@ export function activate(context: vscode.ExtensionContext) {
152156
}
153157
});
154158

155-
context.subscriptions.push(generateCvbCommand, uploadCvbCommand, applyCvbCommand, stopOperation, outputChannel);
159+
// 注册命令:分析代码
160+
let analyzeCodeCommand = vscode.commands.registerCommand('codeReDesign.analyzeCode', async () => {
161+
const workspaceFolders = vscode.workspace.workspaceFolders;
162+
if (!workspaceFolders) {
163+
vscode.window.showErrorMessage('No workspace folder found.');
164+
return;
165+
}
166+
167+
const workspacePath = workspaceFolders[0].uri.fsPath;
168+
const tmpDir = path.join(workspacePath, '.CodeReDesignWorkSpace');
169+
const cvbFiles = fs.readdirSync(tmpDir).filter((file: string) => file.endsWith('.cvb'));
170+
171+
if (cvbFiles.length === 0) {
172+
vscode.window.showErrorMessage('No CVB files found in the tmp directory.');
173+
return;
174+
}
175+
176+
// 让用户选择要分析的 CVB 文件
177+
const selectedCvbFile = await vscode.window.showQuickPick(cvbFiles, {
178+
placeHolder: 'Select a CVB file to analyze',
179+
});
180+
181+
if (!selectedCvbFile) {
182+
return;
183+
}
184+
185+
// 读取 CVB 文件内容
186+
const cvbFilePath = path.join(tmpDir, selectedCvbFile);
187+
const cvbContent = fs.readFileSync(cvbFilePath, 'utf-8');
188+
189+
// 获取用户的分析需求
190+
const userRequest = await vscode.window.showInputBox({
191+
prompt: 'Enter your analysis request',
192+
placeHolder: 'e.g., Analyze the code for potential bugs',
193+
});
194+
195+
if (!userRequest) {
196+
return;
197+
}
198+
199+
if (currentOperationController){
200+
currentOperationController.abort();
201+
currentOperationController = null;
202+
}
203+
// 创建新的 AbortController
204+
currentOperationController = new AbortController();
205+
// 调用分析代码功能
206+
const analysisResult = await analyzeCode(cvbContent, userRequest, outputChannel, currentOperationController.signal);
207+
if (analysisResult) {
208+
vscode.window.showInformationMessage('Analysis completed. Check the output channel for details.');
209+
}
210+
211+
vscode.window.showInformationMessage('解析完毕');
212+
});
213+
214+
context.subscriptions.push(generateCvbCommand, uploadCvbCommand, applyCvbCommand, stopOperation, analyzeCodeCommand, outputChannel);
156215

157216
setupCvbAsMarkdown(context);
158217

0 commit comments

Comments
 (0)