Implement function to execute VSCode commands

- Added a new function 'doCommand' in 'doCommand.ts' to execute VSCode commands.
- The function takes a message and a panel as arguments, and uses the 'executeCommand' method from the 'vscode' module.
- Updated 'commandManager.ts' to return an empty string instead of undefined.
- Registered the new 'doCommand' handler in 'loadHandlers.ts'.
This commit is contained in:
bobo.yang 2023-08-21 11:52:00 +08:00
parent 6b96d911f0
commit df679ea613
3 changed files with 26 additions and 1 deletions

View File

@ -23,7 +23,7 @@ class CommandManager {
description: 'ask code',
args: 0,
handler: async (commandName: string, userInput: string) => {
return;
return '';
}
});
}

21
src/handler/doCommand.ts Normal file
View File

@ -0,0 +1,21 @@
/*
execute vscode command
*/
import * as vscode from 'vscode';
import DtmWrapper from '../toolwrapper/dtm';
import { regInMessage, regOutMessage } from '../util/reg_messages';
import { runCommandAndWriteOutput } from '../util/commonUtil';
regInMessage({command: 'doCommand', content: ['command', 'arg1', 'arg2']});
export async function doCommand(message: any, panel: vscode.WebviewPanel|vscode.WebviewView): Promise<void> {
// execute vscode command
// message.content[0]: vscode command
// message.content[1:]: args for command
try {
await vscode.commands.executeCommand(message.content[0], ...message.content.slice(1));
} catch (error) {
console.error(`Failed to execute command ${message.content[0]}: ${error}`);
}
return;
}

View File

@ -15,6 +15,7 @@ import { contextDetail } from './contextDetail';
import { listAllMessages } from './listMessages';
import { regActionList } from './regActionList';
import { applyAction } from './applyAction';
import { doCommand } from './doCommand';
// According to the context menu selected by the user, add the corresponding context file
@ -78,3 +79,6 @@ messageHandler.registerHandler('applyAction', applyAction);
messageHandler.registerHandler('deleteChatMessage', deleteChatMessage);
messageHandler.registerHandler('askCode', askCode);
// Execute vscode command
// Response: none
messageHandler.registerHandler('doCommand', doCommand);