是否可以在VSCode中的扩展之间调用命令?

问题描述 投票:13回答:1

例如,有两个VSCode扩展名:

  • [extension1已注册命令exCommand1
  • [extension2已注册命令exCommand2

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

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

如果API Doc是正确的,那么

  • [extension1可以呼叫exCommand2提供的extension2
  • [extension2可以呼叫exCommand1提供的extension1

但是,VSCode的扩展名是延迟加载的,因此如何从另一个尚未加载的扩展名中调用命令?

visual-studio-code vscode-extensions
1个回答
15
投票

我知道这是一个旧帖子,如果您仍然有相同的要求或对其他人使用谷歌搜索,这就是我的做法。

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);
        }
    )
};
© www.soinside.com 2019 - 2024. All rights reserved.