19

For example, there are two VSCode extensions:

  • extension1 has registered command exCommand1
  • extension2 has registered command exCommand2

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>

If API Doc is correct then

  • extension1 can call exCommand2 provided by extension2
  • extension2 can call exCommand1 provided by extension1

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

zanedp
  • 409
  • 4
  • 9
gyeongseok seo
  • 331
  • 2
  • 10

1 Answers1

28

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);
        }
    )
};
Alex
  • 59,571
  • 22
  • 137
  • 126
Mesh
  • 6,262
  • 5
  • 34
  • 53
  • 2
    A brief stumbling block for me was figuring out what exactly to pass to `vscode.extensions.getExtension()`. To get a list of extension IDs, you can use `vscode.extensions.all.map(x => x.id)`. They are of the form `.`. – zanedp Jan 27 '20 at 16:22
  • I'm not the one that answered the question. You can try to pass them as a second parameter `vscode.commands.executeCommand("xmlTools.formatAsXml", HERE);` but no idea if it works between extensions. – Alex May 12 '20 at 02:59