Search code examples
typescriptcommandvscode-extensionssubmenu

How to place a command into a submenu using the vscode extension api


I am creating a Visual studio code extension, and I would like to place a command into a submenu like thisenter image description here

here, the "Peek" submenu contains commands like "Peek Call Hierarchy".

Currently in my Package.json, I have

{
    "contributes": {
        "commands": [{
            "command":"myExtension.dothing",
            "title":"make the extension do a thing",
            "category":"myextensioncategory"
        }],
        "menus": [{
            "explorer/context": [{
                    "command": "myExtension.dothing",
                    "group": "myextension.myGroup",
                    "when": "!explorerResourceIsFolder"
                },
                {
                    "submenu": "myextensionsubmenu",
                    "group": "myextension.myGroup"
                }
            ]
        }],
        "submenus": [{
            "label": "my extension",
            "id": "myextensionsubmenu"
        }]
    }
}

(not relevant parts removed)

And it shows up with something like this

enter image description here

I would like to place the "make the extension do a thing" command into the "my extension" submenu, but I don't understand how I would do that from the docs Can anyone explain or point me to a resource/tutorial? Thanks!


Solution

  • As far as I understand the structure of submenus, you have to add the relevant entries in the menus array.

    Your package.json may look like this:

    {
        "contributes": {
            "commands": [{
                "command":"myExtension.dothing",
                "title":"make the extension do a thing",
                "category":"myextensioncategory"
            }],
            "menus": [{
                "explorer/context": [{
                        "command": "myExtension.dothing",
                        "group": "myextension.myGroup",
                        "when": "!explorerResourceIsFolder"
                    },
                    {
                        "submenu": "myextensionsubmenu",
                        "group": "myextension.myGroup"
                    }
                ],
                "myextensionsubmenu":[
                      {
                        "command":"myExtension.dothing",
                        "group":"myExtension.myGroup"
                       }
                ]
            }],
            "submenus": [{
                "label": "The Label for the menu that opens the submenu",
                "id": "myextensionsubmenu"
            }]
        }
    }
    

    The objects in "submenus" array seem to only define the label of the corresponding entries.

    You may also look at this. It may help you.