# vscodeのコンテキストメニューをカスタマイズする方法

2024-02-10 18:20

VS Codeの右クリックメニューに、既存のコマンドを追加したかった。 settings.jsonの editor/context にコマンドを並べると、コンテキストメニューに項目を追加できる。

# editor/context にメニュー項目を定義する

次の設定は、settings.jsonに追加する例である。

"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"
    }
]

各項目は、editor/context 内のコマンドを1つずつ定義している。 この例では次の3つのコマンドを指定している。

command group when
bookmarks.toggle bookmarks editorTextFocus && config.bookmarks.showCommandsInContextMenu
bookmarks.jumpToNext bookmarks@1 同上
bookmarks.jumpToPrevious bookmarks@1 同上

この設定で、右クリックからブックマークの切り替えと前後への移動を実行できる。

# 拡張機能が定義していないコマンドも追加できる

この設定は、コマンドが実際に拡張機能で定義されている必要がないことを示している。 既存のコマンドをメニューに追加するだけでなく、カスタムコマンドを作って追加することもできる。

設定方法は次の回答を参考にした。