Skip to content

Commit 0ba9a2a

Browse files
committed
feat: 增加右键选中文件打包功能
1 parent 7ce2d2f commit 0ba9a2a

File tree

3 files changed

+93
-3
lines changed

3 files changed

+93
-3
lines changed

package.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"onCommand:codeReDesign.analyzeSingleFile",
2525
"onCommand:codeReDesign.redesignSingleFile",
2626
"onCommand:codeReDesign.startChat",
27-
"onCommand:codeReDesign.continueChat"
27+
"onCommand:codeReDesign.continueChat",
28+
"onCommand:codeReDesign.packupToCvb"
2829
],
2930
"repository": {
3031
"type": "git",
@@ -121,6 +122,13 @@
121122
"when": "false",
122123
"category": "CodeReDesign",
123124
"icon": "${bug}"
125+
},
126+
{
127+
"command": "codeReDesign.packupToCvb",
128+
"title": "CodeReDesign: Packup to CVB",
129+
"when": "false",
130+
"category": "CodeReDesign",
131+
"icon": "${add}"
124132
}
125133
],
126134
"viewsContainers": {
@@ -194,6 +202,10 @@
194202
"command": "codeReDesign.redesignSingleFile",
195203
"group": "cvb@1",
196204
"when": "resourceExtname in codeReDesign.supportedSourceFileTypeExt"
205+
},
206+
{
207+
"command": "codeReDesign.packupToCvb",
208+
"group": "cvb@1"
197209
}
198210
]
199211
},

src/extension.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { activateGuide } from './guide';
1111
import {ChatPanel} from './chatPanel';
1212
import { isUnderTokenLimit, initTokenizer } from './deepseekTokenizer';
1313
import * as ragService from './ragService';
14+
import {collectSupportedFiles} from './languageMapping';
1415

1516
let currentOperationController: AbortController | null = null;
1617

@@ -211,6 +212,36 @@ export function activate(context: vscode.ExtensionContext) {
211212
vscode.window.showInformationMessage(`CVB file generated at: ${cvbFilePath}`);
212213
});
213214

215+
// New command for the context menu
216+
let packupToCvbCommand = vscode.commands.registerCommand('codeReDesign.packupToCvb', async (uri: vscode.Uri, selectedUris: vscode.Uri[]) => {
217+
// Collect URIs (prioritize selectedUris for multi-selection)
218+
const uris: vscode.Uri[] = selectedUris && selectedUris.length > 0 ? selectedUris : uri ? [uri] : [];
219+
220+
if (uris.length === 0) {
221+
vscode.window.showErrorMessage('No files or folders selected.');
222+
return;
223+
}
224+
225+
// Collect all supported files (recursively for folders)
226+
const filePaths = await collectSupportedFiles(uris);
227+
228+
const userRequest = await vscode.window.showInputBox({
229+
prompt: 'Enter your refactoring request',
230+
placeHolder: 'e.g., Move all mouse event handling code to a single file',
231+
});
232+
233+
if (!userRequest) {
234+
return;
235+
}
236+
237+
try {
238+
const cvbFilePath = generateCvb(filePaths, userRequest);
239+
vscode.window.showInformationMessage(`CVB file generated at: ${cvbFilePath}`);
240+
} catch (error) {
241+
vscode.window.showErrorMessage(`Failed to generate CVB file: ${(error as Error).message}`);
242+
}
243+
});
244+
214245
// 注册命令:上传 CVB 并调用 API
215246
let redesignCvbCommand = vscode.commands.registerCommand('codeReDesign.redesignCvb', async () => {
216247
const workspaceFolders = vscode.workspace.workspaceFolders;
@@ -367,7 +398,7 @@ export function activate(context: vscode.ExtensionContext) {
367398
}
368399
});
369400

370-
context.subscriptions.push(generateCvbCommand, redesignCvbCommand, applyCvbCommand, stopOperation, analyzeCodeCommand, outputChannel, startChatCommand);
401+
context.subscriptions.push(generateCvbCommand, redesignCvbCommand, applyCvbCommand, stopOperation, analyzeCodeCommand, startChatCommand, packupToCvbCommand);
371402

372403
setupCvbAsMarkdown(context);
373404

src/languageMapping.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import * as vscode from 'vscode';
2+
import * as fs from 'fs';
3+
import * as path from 'path';
4+
15
// 语言映射表
26
export const g_objLanguageMapping: { [key: string]: string } = {
37
'cpp': 'c++',
@@ -36,4 +40,47 @@ export function getLanguageFromPath(filePath: string): string {
3640
}
3741

3842
export const SOURCE_FILE_EXTENSIONS_WITH_DOT = Object.keys(g_objLanguageMapping)
39-
.map(ext => `.${ext}`);
43+
.map(ext => `.${ext}`);
44+
45+
/**
46+
* Recursively collect all supported files from the given URIs (files or folders).
47+
* @param uris Array of URIs (files or folders) to process.
48+
* @returns Array of file paths with supported extensions.
49+
*/
50+
export async function collectSupportedFiles(uris: vscode.Uri[]): Promise<string[]> {
51+
const filePaths: string[] = [];
52+
53+
async function traverseFolder(folderPath: string): Promise<void> {
54+
const entries = await fs.promises.readdir(folderPath, { withFileTypes: true });
55+
for (const entry of entries) {
56+
const fullPath = path.join(folderPath, entry.name);
57+
if (entry.isDirectory()) {
58+
// Recursively traverse subfolders
59+
await traverseFolder(fullPath);
60+
} else if (entry.isFile()) {
61+
// Check if the file has a supported extension
62+
const ext = path.extname(entry.name).toLowerCase();
63+
if (SOURCE_FILE_EXTENSIONS_WITH_DOT.includes(ext)) {
64+
filePaths.push(fullPath);
65+
}
66+
}
67+
}
68+
}
69+
70+
// Process each URI
71+
for (const uri of uris) {
72+
const stat = await fs.promises.stat(uri.fsPath);
73+
if (stat.isDirectory()) {
74+
// If it's a folder, traverse it recursively
75+
await traverseFolder(uri.fsPath);
76+
} else if (stat.isFile()) {
77+
// If it's a file, check its extension
78+
const ext = path.extname(uri.fsPath).toLowerCase();
79+
if (SOURCE_FILE_EXTENSIONS_WITH_DOT.includes(ext)) {
80+
filePaths.push(uri.fsPath);
81+
}
82+
}
83+
}
84+
85+
return filePaths;
86+
}

0 commit comments

Comments
 (0)