例如,有两个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的扩展名是延迟加载的,因此如何从另一个尚未加载的扩展名中调用命令?
我知道这是一个旧帖子,如果您仍然有相同的要求或对其他人使用谷歌搜索,这就是我的做法。
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);
}
)
};