Is it possible to call command between extensions in VSCode?

梦想与她 提交于 2019-12-21 07:33:56

问题


for example there are two VSCode extension.
- extension1 is regist command 'exCommand1'.
- extension2 is regist command 'exCommand2'.

VSCode extension can call command.
(ref: https://code.visualstudio.com/docs/extensionAPI/vscode-api)

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

If API Doc is correct then..

extension1 can call extension2's 'exCommand2'. and
extension2 can call extension1's 'exCommand1'.

is it possible?

VSCode's extension is Lazy Loading...

If inter extension's command call is possible then How can loading extension to another extension?


回答1:


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);
        }
    )
};


来源:https://stackoverflow.com/questions/41559471/is-it-possible-to-call-command-between-extensions-in-vscode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!