I want to bind a command that triggers only when I'm in a javascript or javascript react (jsx) file.
This is ideally something along what I want:
{
"key": "f9",
"command": "sortImports.sort",
"when": "editorTextFocus && (editorLangId == 'javascriptreact' && editorLangId == 'javascript')"
}
I have tried with (editorLangId == 'javascriptreact' || editorLangId == 'javascript') but that fails.
I can get it to work by doing 2 key bindings (one with javascript and one with javascript react).
I'm not quite sure what the 'when' condition really supports? Maybe a startsWith...but I couldn't get that to work though.
If it's a snippet, adding the snippet in a global snippets file with multiple languages in the scope property seems to work for me. (Type "User snippets" in help and click on "New Global Snippets file"). Then add your snippet to the file.
"myFavSnippet": {
"scope": "javascript,javascriptreact",
"prefix": "console.log",
"body": [
"console.log(${1:}$SELECTION)${0};"
],
"description": "console.log"
},
This is what I put in the keybindings.json file:
{
"key": "shift+cmd+l",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"name": "myFavSnippet"
}
},
Despite what is written in the documentation, it seems to work without adding the langId property to args. This works for me even when the snippet is put in a language specific snippets file.
What worked for me was:
{
"key": "f9",
"command": "sortImports.sort",
"when": "editorTextFocus && editorLangId == 'javascriptreact' || editorLangId == 'javascript'"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With