Reputation: 6093
How can we access to the list of commands of extensions inside a extension pack? e.g. if we have an extension pack A which is consisted of 2 extensions B,C, and these 2 have their own commands that can be run via command plate. We need to access these commands and be able to run them.
How we can run them?
Upvotes: 0
Views: 591
Reputation: 1018
You can access the list of all extensions known to the system like this:
const allExtensions: readonly any[] = vscode.extensions.all;
You can access the list of commands of a particular extension by activating or not activating that extension. For example, let's say allExtensions
is an array where your extension is positioned at the i
-th index. Without activating that extension, you can obtain its list of commands like this:
const listOfCommands = allExtensions[i]?.packageJSON.contributes.commands;
However, since you want to run a command from this extension, it is in your interest to activate it first. To activate the extension from your main one, do this:
const otherExtension = vscode.extensions.all[i];
otherExtension.activate();
After the extension is activated, although not very useful at this point, you can get the same extension by using its id
:
otherExtension.activate().then(() => {
const sameExtension = vscode.extensions.getExtension('extensionId');
const listOfCommands = sameExtension?.packageJSON.contributes.commands;
});
You won't be able to get the other extension by its id
if you don't activate it first, so at least this will give you assurance that the other extension was indeed activated from your main one.
At this point, you can run a command from your other extension just as you'd run it from your main one:
otherExtension.activate.then(() => {
vscode.commands.executeCommand('otherExtensionCommandId');
});
Upvotes: 3