执行插件内置命令
本部分在vscode插件开发教程-helloworld插件的基础上继续开发。
vscode内置命令有两种使用方式,一种是在插件里直接调用;还有将命令解析成uri,给用户选择。比如在鼠标hover的时候,自动生成一句注释本行?,而注释本行? 本身可以点击,在点击后该行代码被注释。
直接在程序里执行命令
无参命令
有一些命令直接执行即可,无需额外参数,也不会返回内容,比如注释语句,下面是使用示例:
首先在src下新建目录command,新建文件commandExecute.ts,输入下列内容:
import * as vscode from 'vscode';
let executeCommandNoArgs = vscode.commands.registerCommand('helloworld.executeCommandNoArgs', () => {
vscode.commands.executeCommand('editor.action.addCommentLine');
});
export {
executeCommandNoArgs };
这里和helloworld里面一样,注册了一个新命令,该命令会执行vscode的内置命令:editor.action.addCommentLine
。
在extension.ts
里面导入刚才建好的文件import { executeCommandNoArgs } from './command/commandExecute';
在activate
方法里面新增一行context.subscriptions.push(executeCommandNoArgs);
在package.json命令列表里面增加一个新命令Execute CommandNoArgs
,并指定在命令触发时插件激活:
"activationEvents": [
...
"onCommand:helloworld.executeCommandNoArgs"
],
"contributes": {
"commands": [
...
{
"command": "helloworld.executeCommandNoArgs",
"title":