@@ -3,7 +3,7 @@ import * as path from 'path';
33import * as fs from 'fs' ;
44import { selectFiles } from './fileSelector' ;
55import { generateCvb , parseCvb , applyCvbToWorkspace , generateTimestamp } from './cvbManager' ;
6- import { queryCodeReDesign , generateFilenameFromRequest } from './deepseekApi' ;
6+ import { queryCodeReDesign , generateFilenameFromRequest , analyzeCode } from './deepseekApi' ;
77import { setupCvbAsMarkdown } from './cvbMarkdownHandler' ;
88import { 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