Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode Extension API - Identify file or folder click in explorer context menu

VSCode 1.3 has added support for adding commands to context menus. Is there a way to identify whether a file or folder is clicked to open the explorer context menu?

"menus": {
    "explorer/context": [
        {
            "when": "????",
            "command": "extension.myCommand",
            "group": "myGroup"
        }
    ]
}

Also, is there a comprehensive(ish) list of expressions that might be checked in the when clause here?

like image 344
bingles Avatar asked Jul 08 '16 12:07

bingles


3 Answers

You can use "when": "explorerResourceIsFolder".

I had to dig through the code to find it (I was actually writing up a response to say it didn't exist and enumerating the possible clause values when I saw it).

As of v1.10.1:

config.<any_config_path_here>
editorIsOpen
explorerResourceIsFolder
explorerViewletFocus
explorerViewletVisible
filesExplorerFocus
globalMessageVisible
inDebugMode
inQuickOpen
inZenMode
listFocus
openEditorsFocus
resource (Uri information: path, query, scheme, etc)
resourceFilename
resourceLangId
resourceScheme
scmProvider
textCompareEditorVisible

I've submitted an issue to improve the documentation for this.

like image 163
mdickin Avatar answered Oct 17 '22 16:10

mdickin


Regarding obtaining a comprehensive list of context keys: in recent VSCode versions, there's a Developer: Inspect Context Keys command. After executing the command, it lets you pick a UI element:

After that, the dev console opens, and you can expand the logged object that contains a full list of context keys and their current values in this "scope":

like image 8
Gama11 Avatar answered Oct 17 '22 17:10

Gama11


https://code.visualstudio.com/docs/getstarted/keybindings#_when-clause-contexts

is file: "when": "!explorerResourceIsFolder"

is dir: "when": "explorerResourceIsFolder"

like image 4
Alex Popkov Avatar answered Oct 17 '22 15:10

Alex Popkov