'Can I have a TreeView action that doesn't show up in the command palette?
I want to have a TreeView in my VS Code extension, and I want to be able to right-click on items in the view and have a context menu with actions in it. The official way to do this is to add it to the menus
object of the manifest:
"view/item/context": [
{
"command": "myExtension.myAction",
"when": "view == myExtension",
"group": "inline"
}
]
The command myExtension.myAction
now (as far as I can tell) has to be added to the commands
section of the manifest. This causes it to show up in the command palette. This is very inconvenient. My contextual action is meaningless outside of the context of right-clicking on an item, it would just clutter up the command palette.
Is there any way of hooking view actions up with commands without having them show up in the command palette?
Solution 1:[1]
In my testing (by no means absolutely thorough) it also works, more simply, to add this enablement clause to your commands:
"contributes": {
"commands": [
{
"command": "myExtension.myAction",
"when": "view == myExtension",
"enablement": "view == <your treeview id here>"
"group": "inline"
}
]
}
When I do that the command doesn't show in the Command Palette for me. And then you don't need the commandPalette
menu entry.
Solution 2:[2]
It's not the most intuitive thing in the world, but it's pretty simple. First, as mentioned in the answer, you must list your command in the commands
section of the extension manifest (package.json
):
"contributes": {
"commands": [
{
"command": "myExtension.myAction",
"when": "view == myExtension",
"group": "inline"
}
]
}
Now, in the menus
section of your contributes
, set the when
expression for the command to false:
"contributes": {
"menus": {
"commandPalette": [
{
"command": "myExtension.myAction",
"when": "false"
}
]
}
}
The when expression controls when the command should show up in the command palette, so this means it will be absent permanently. See the documentation.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Mark |
Solution 2 |