diff --git a/package.json b/package.json index 69b9164..5dab062 100644 --- a/package.json +++ b/package.json @@ -671,11 +671,19 @@ }, { "command": "devchat.explain", - "title": "Explain" + "title": "Generate Explain" }, { "command": "devchat.explain_chinese", "title": "代码解释" + }, + { + "command": "devchat.comments", + "title": "Generate Comments" + }, + { + "command": "devchat.comments_chinese", + "title": "生成注释" } ], "keybindings": [ @@ -769,6 +777,16 @@ "command": "devchat.explain_chinese", "when": "isChineseLocale && editorTextFocus && editorHasSelection", "group": "navigation" + }, + { + "command": "devchat.comments", + "when": "!isChineseLocale && editorTextFocus && editorHasSelection", + "group": "navigation" + }, + { + "command": "devchat.comments_chinese", + "when": "isChineseLocale && editorTextFocus && editorHasSelection", + "group": "navigation" } ] } diff --git a/src/contributes/commands.ts b/src/contributes/commands.ts index 463f09a..0f978f2 100644 --- a/src/contributes/commands.ts +++ b/src/contributes/commands.ts @@ -462,11 +462,9 @@ export function registerDevChatChatCommand(context: vscode.ExtensionContext) { context.subscriptions.push(disposable); } -export function registerCodeLensExplainCommand( - context: vscode.ExtensionContext -) { +export function registerCodeLensRangeCommand(context: vscode.ExtensionContext) { let disposable = vscode.commands.registerCommand( - "CodeLens.Explain", + "CodeLens.Range", async (message: string, pos: { start: number; end: number }) => { const editor = vscode.window.activeTextEditor; if (editor) { @@ -550,10 +548,30 @@ function registerExplainCommand(context: vscode.ExtensionContext) { ); } +function registerCommentCommand(context: vscode.ExtensionContext) { + const callback = async () => { + const editor = vscode.window.activeTextEditor; + if (editor) { + if (!(await ensureChatPanel(context))) { + return; + } + + chatWithDevChat(ExtensionContextHolder.provider?.view()!, "/comments"); + } + }; + context.subscriptions.push( + vscode.commands.registerCommand("devchat.comments", callback) + ); + context.subscriptions.push( + vscode.commands.registerCommand("devchat.comments_chinese", callback) + ); +} + export { registerOpenChatPanelCommand, registerAddContextCommand, registerAskForCodeCommand, registerAskForFileCommand, registerExplainCommand, + registerCommentCommand, }; diff --git a/src/extension.ts b/src/extension.ts index bfe8f08..a8ffbfa 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -15,7 +15,8 @@ import { registerDevChatChatCommand, registerHandleUri, registerExplainCommand, - registerCodeLensExplainCommand, + registerCodeLensRangeCommand, + registerCommentCommand, } from "./contributes/commands"; import { regLanguageContext } from "./contributes/context"; import { regDevChatView } from "./contributes/views"; @@ -441,6 +442,7 @@ async function activate(context: vscode.ExtensionContext) { registerAskForCodeCommand(context); registerAskForFileCommand(context); registerExplainCommand(context); + registerCommentCommand(context); registerStatusBarItemClickCommand(context); registerInstallCommandsCommand(context); @@ -453,7 +455,7 @@ async function activate(context: vscode.ExtensionContext) { regPythonPathCommand(context); registerDevChatChatCommand(context); - registerCodeLensExplainCommand(context); + registerCodeLensRangeCommand(context); registerCodeLensProvider(context); startRpcServer(); diff --git a/src/panel/codeLens.ts b/src/panel/codeLens.ts index b4be559..cc0da01 100644 --- a/src/panel/codeLens.ts +++ b/src/panel/codeLens.ts @@ -54,15 +54,20 @@ export class CodeLensManager { this.registrations = [ { elementType: "function", - objectName: "Add unit tests", + objectName: "unit tests", promptGenerator: "/unit_tests {__filename__}:::{__functionName__}:::{__functionStartLine__}:::{__functionEndLine__}:::{__containerStartLine__}:::{__containerEndLine__}", }, { elementType: "function", - objectName: "Explain", + objectName: "explain", promptGenerator: "/explain", }, + { + elementType: "function", + objectName: "docstring", + promptGenerator: "/docstring", + }, // { // elementType: 'function', // objectName: 'generate unit tests', @@ -212,10 +217,13 @@ class FunctionTestCodeLensProvider implements vscode.CodeLensProvider { `${parentRange ? parentRange.end.line : -1}` ) .replace(/{__functionCode__}/g, functionCode); // Fixed syntax to replace all occurrences - if (codelenRegister.objectName === "Explain") { + if ( + codelenRegister.objectName === "explain" || + codelenRegister.objectName === "docstring" + ) { const lens = new vscode.CodeLens(range, { title: codelenRegister.objectName, - command: "CodeLens.Explain", + command: "CodeLens.Range", // arguments: [document.uri.fsPath, range, funcDef.name] // Commented out as it's not used arguments: [ prompt,