Skip to content

Native context menus

Enable context_menus in Copier (on by default except the minimal preset). The generated src/commands/context.ts contains small example handlers. The manifest is the source of truth for menu placement.

  1. Declare a command in contributes.commands in package.json.
  2. Register the same command in src/commands/context.ts. Return the result for testing.
  3. Add an entry under contributes.menus. Use a when clause to control visibility.
  4. Use command enablement to disable a visible command. Use commandPalette with when: "false" to hide context-only commands from the palette.
  5. Register disposables with the extension context.

Example entry (replace example-extension with your extension ID):

{"command":"example-extension.context.json","when":"resourceExtname == .json","group":"1_inspect@1"}

Declare submenu IDs in contributes.submenus; use the ID as a menu key to add children. Different group names add separators. @n sets order in a group. alt selects an alternative command with the Alt key.

Menu Context / example
explorer/context URI and selected URI array; file, folder and JSON filters
editor/context Active editor and selected text
editor/title/context Editor resource
editor/lineNumber/context Line number command argument
terminal/context, terminal/title/context Terminal context, when enabled
view/item/context Tree element and selected elements; inline actions
webview/context webviewId and webviewSection; native menu
notebook/cell/title Cell toolbar / overflow, not a right-click menu
testing/item/context, testing/item/result Test item or result context
scm/resourceState/context, scm/resourceFolder/context, scm/resourceGroup/context Existing SCM resources
timeline/item/context Existing timeline item
extension/context Existing extension item
debug/variables/context, debug/watch/context, debug/callstack/context Existing debugger context

Argument shapes vary by surface. The Inspect Context command reports selected safe fields and property names. Adapt the handler to the documented contract for your target surface. Do not serialize arbitrary objects or inspect debug variable values indiscriminately.

Set data-vscode-context to a JSON string on an element. Contribute webview/context entries with conditions on webviewId and webviewSection. Do not build a second context-menu widget in the frontend.

Run setContext to change a custom key. The Toggle Context Menu Examples command toggles the Explorer submenu. Inspect Workspace demonstrates enablement based on workspace trust and an open workspace.

Run task test:all and task package. The integration test executes resource, selection, text, clipboard and toggle commands. Template contract tests check enabled/disabled contributions. The screenshot workflow exercises real UI interactions and captures the VS Code HWND using WinApp. Its loopback-only DevTools connection is test-only; extension runtime code uses stable APIs.

Reference: https://code.visualstudio.com/api/references/contribution-points#contributes.menus