如何在Visual Studio代码中自定义上下文菜单?

时间:2017-10-12 16:13:28

标签: visual-studio-code contextmenu customization

是否可以在Visual Studio代码中自定义上下文菜单?





目前它看起来像这样。









我需要再添加两个菜单选项。





像“返回”和“前进”之类的东西。





可以这样做吗?




1 个答案:

答案 0 :(得分:1)

是的,扩展程序可以将菜单项添加到上下文菜单:在package.json中,添加contributes.menus部分。文本编辑器上下文菜单称为editor/context

执行此操作的扩展示例为Bookmarks,其中添加了三个上下文菜单项。其package.json的相关部分是:

{
    "name": "Bookmarks",
    ...
    "contributes": {
        ...
        "menus": {
            ...
            "editor/context": [
                {
                    "command": "bookmarks.toggle",
                    "group": "bookmarks",
                    "when": "editorTextFocus && config.bookmarks.showCommandsInContextMenu"
                },
                {
                    "command": "bookmarks.jumpToNext",
                    "group": "bookmarks@1",
                    "when": "editorTextFocus && config.bookmarks.showCommandsInContextMenu"
                },
                {
                    "command": "bookmarks.jumpToPrevious",
                    "group": "bookmarks@1",
                    "when": "editorTextFocus && config.bookmarks.showCommandsInContextMenu"
                }
            ],
            ....
        },
        ....
    },
    ....
}

API docs对于group属性的含义有些含糊:

  

最后,group属性定义了菜单项的排序和分组。

其含义在Sorting of groups下有更详细的描述。诸如“书签”之类的词建立了一组菜单条目,这些菜单条目通过水平规则与其他组分开,并且组按字母顺序排序,后缀“ @ ”控制每个组内的排序:

Screenshot of three added context menu items

相关问题