是否可以在 VSCode 中的扩展之间调用命令? [英] Is it possible to call commands between extensions in VSCode?

查看:38
本文介绍了是否可以在 VSCode 中的扩展之间调用命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,有两个 VSCode 扩展:

For example, there are two VSCode extensions:

  • extension1 已注册命令 exCommand1
  • extension2 已注册命令 exCommand2
  • extension1 has registered command exCommand1
  • extension2 has registered command exCommand2

根据文档,VSCode 扩展可以调用命令(参考:https://code.visualstudio.com/docs/extensionAPI/vscode-api)

According to documentation, a VSCode extension can call commands (ref: https://code.visualstudio.com/docs/extensionAPI/vscode-api)

executeCommand<T>(command: string, ...rest: any[]): Thenable<T | undefined>

如果 API Doc 是正确的

If API Doc is correct then

  • extension1可以调用extension2
  • 提供的exCommand2
  • extension2可以调用extension1
  • 提供的exCommand1

但是,VSCode 的扩展是延迟加载的,那么如何从另一个可能尚未加载的扩展中调用命令?

But, VSCode's extensions are lazily loaded, so how does one call a command from another extension that may not already be loaded?

推荐答案

我知道这是一个旧帖子,如果您仍然有相同的要求或其他人在谷歌上搜索,我就是这样做的.

I know this is an old post, if you still have the same requirement or for others googling, this is how I have done it.

var xmlExtension =  vscode.extensions.getExtension( 'DotJoshJohnson.xml' );

// is the ext loaded and ready?
if( xmlExtension.isActive == false ){
    xmlExtension.activate().then(
        function(){
            console.log( "Extension activated");
            // comment next line out for release
            findCommand(); 
            vscode.commands.executeCommand("xmlTools.formatAsXml");
        },
        function(){
            console.log( "Extension activation failed");
        }
    );   
} else {
    vscode.commands.executeCommand("xmlTools.formatAsXml");
}


// dev helper function to dump all the command identifiers to the console
// helps if you cannot find the command id on github.
var findCommand = function(){
    vscode.commands.getCommands(true).then( 
        function(cmds){
            console.log("fulfilled");
            console.log(cmd);
        },
        function() {
            console.log("failed");
            console.log(arguments);
        }
    )
};

这篇关于是否可以在 VSCode 中的扩展之间调用命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆