I am trying to create a VSCode extension. This extension provides two commands, never mind their implementation:
export function activate(context: ExtensionContext) {
const provider = new ContentProvider();
const providerRegistrations = Disposable.from(
workspace.registerTextDocumentContentProvider(ContentProvider.scheme, provider)
);
// Open the dynamic document, and shows it in the next editor
const openMyExtensionCommandRegistration = commands.registerTextEditorCommand('extension.openMyExtension', editor => {
// Activate the extension and do something
});
const useMyExtensionCommandRegistration = commands.registerTextEditorCommand('extension.useMyExtension', editor => {
// Do something
});
context.subscriptions.push(
provider,
openMyExtensionCommandRegistration,
useMyExtensionCommandRegistration,
providerRegistrations
);
}
And this is a part of my package.json
file:
"activationEvents": [
"onCommand:extension.openMyExtension"
],
"main": "./out/extension",
"contributes": {
"commands": [
{
"command": "extension.openMyExtension",
"title": "Open my extension",
"category": "MyExtension"
},
{
"command": "extension.useMyExtension",
"title": "Do something with my extension",
"category": "MyExtension"
}
],
The first command, which is supposed to activates my extension, works. It appears in the command palette, and actually does what it is supposed to do when invoked.
The second command however, despite appearing in the command palette, raise the following error message when called:
command 'extension.useMyExtension' not found
I find it weird that my first command works fine but not the second since the code is quite similar. Any ideas why?
Note that I obviously changed some variable names, I double checked for typos in the real code.
You need to add all registered commands to the activationEvents
list in package.json so that they are available on invocation. Update your package.json as such:
{
...
"activationEvents": [
"onCommand:extension.openMyExtension",
"onCommand:extension.useMyExtension"
]
...
}
You can find more details on activation events in the official VSCode Documentation.