Add RunVSCodeCommandAction to execute VSCode commands

- Created a new file runVSCodeCommand.ts to define the RunVSCodeCommandAction class.
- This class implements the Action interface and provides a handlerAction method to execute VSCode commands.
- Updated actionManager.ts to register the RunVSCodeCommandAction.
This commit is contained in:
bobo.yang 2023-08-09 00:16:03 +08:00
parent 895e2435f9
commit 27bece07dc
2 changed files with 55 additions and 0 deletions

View File

@ -11,6 +11,7 @@ import { AskInputAction } from './askInputAction';
import { SymbolInFileAction } from './symbolInFileAction'; import { SymbolInFileAction } from './symbolInFileAction';
import { CurrentDocumentAction } from './currentDocumentAction'; import { CurrentDocumentAction } from './currentDocumentAction';
import { SelectTextAction, SelectBlockAction } from './selectContextAction'; import { SelectTextAction, SelectBlockAction } from './selectContextAction';
import { RunVSCodeCommandAction } from './runVSCodeCommand';
// extend Action // extend Action
@ -64,6 +65,7 @@ export default class ActionManager {
ActionManager.instance.registerAction(new CurrentDocumentAction()); ActionManager.instance.registerAction(new CurrentDocumentAction());
ActionManager.instance.registerAction(new SelectTextAction()); ActionManager.instance.registerAction(new SelectTextAction());
ActionManager.instance.registerAction(new SelectBlockAction()); ActionManager.instance.registerAction(new SelectBlockAction());
ActionManager.instance.registerAction(new RunVSCodeCommandAction());
} }
return ActionManager.instance; return ActionManager.instance;

View File

@ -0,0 +1,53 @@
/*
vscode commandRunVSCommandAction
*/
import * as vscode from 'vscode';
import { Action } from './customAction';
import { CommandResult } from '../util/commonUtil';
import { logger } from '../util/logger';
export class RunVSCodeCommandAction implements Action {
name: string;
description: string;
type: string[];
action: string;
handler: string[];
args: { "name": string, "description": string, "type": string, "as"?: string, "required": boolean, "from": string }[];
constructor() {
this.name = 'run_vscode_command';
this.description = 'Run VSCode command';
this.type = ['command'];
this.action = 'run_vscode_command';
this.handler = [];
this.args = [
{
"name": "command",
"description": 'VSCode command to run.',
"type": "string",
"required": true,
"from": "content.content.command"
},
{
"name": "args",
"description": 'Arguments for the command, separated by comma.',
"type": "string",
"required": false,
"from": "content.content.args"
}
];
}
async handlerAction(args: {[key: string]: any}): Promise<CommandResult> {
try {
const commandArgs = args.args ? args.args.split(',') : [];
const result = await vscode.commands.executeCommand(args.command, ...commandArgs);
return {exitCode: 0, stdout: JSON.stringify(result), stderr: ""};
} catch (error) {
logger.channel()?.error(`Failed to run VSCode command: ${error}`);
logger.channel()?.show();
return {exitCode: -1, stdout: '', stderr: `Failed to run VSCode command: ${error}`};
}
}
}