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

前端 未结 5 1830
野趣味
野趣味 2020-12-06 18:42

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?

相关标签:
5条回答
  • 2020-12-06 18:57

    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":

    0 讨论(0)
  • 2020-12-06 18:59

    You can get the list of language ids like this...

    vscode.languages.getLanguages().then(l => console.log('languages', l));
    

    I still haven't figured out how to determine if the item that was right clicked is a directory. If someone figures it out please let me know.

    0 讨论(0)
  • 2020-12-06 19:00

    A write up about the feature is here. But basically:

    • the when is the same as the keybindings-when and can use the same keys
    • the when can use two new keys resourceScheme and resourceLangId which are available without an editor - think of the explorer context menu
    • the when can be a boolean configuration value, e.g config.editor.lineNumbers

    My menu:

    "menus":{
        "explorer/context": [
            {
                "when": "resourceLangId == sql",
                "command": "extension.myCmd"
            }
        ]
    
    0 讨论(0)
  • 2020-12-06 19:03

    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.

    0 讨论(0)
  • 2020-12-06 19:03

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

    is file: "when": "!explorerResourceIsFolder"

    is dir: "when": "explorerResourceIsFolder"

    0 讨论(0)
提交回复
热议问题