1
votes

I wrote a simple extension for vscode, which registers a command, namely comandName.

In my package.json file I have:

"activationEvents": [
    "onLanguage:python",
    "onCommand:commandName"
]

I meant to execute this command only for Python. To my knowledge, however, the activationEvents in my package.json means that the command would be activated when a Python file is opened or the command is executed. But the command can be executed within any language. I searched the documentation but found no way to execute the command for certain languages.

Is there any way to achieve this target?

1

1 Answers

3
votes

I'm afraid this is impossible for now. However you can work around this to avoid any side effects on other files.

If you bind this command to some key combination or to context menu you can use when clause to limit the command to specific types of files.

It will still allow to execute the command from command palette though. To work around this you can just ignore it when being in file other than Python:

vsc.commands.registerTextEditorCommand('testCommand', editor => {
    if (editor.document.languageId !== 'python') {
        return
    }
    // command logic
}))

Notice I used registerTextEditorCommand instead of regular command. The difference is that this one requires user to be in text editor context to work which is what you probabl