Search code examples
visual-studio-codevscode-extensions

VSCode extension: how to hide/show an item in the context menu?


I'm developing an extension for VSCode that has a command linked to an item in the context menu. Everything works as expected.

I would like to give the user the additional possibility of hiding or showing that context menu item by acting on a specific option on the settings page.

  1. How do you programmatically hide or delete (and then reappear if necessary) an item in the context menu?

  2. Do you know of an extension that does something like this so I can go and study the code?

I'm asking here because I couldn't find anything explanatory elsewhere.


Solution

  • If you look for real world examples, many extensions are open sourced on GitHub.

    Below is an example from my extension.

    • The context menu/command resttext.table.dataToTable is registered in package.json and you can see it is associated with contexts (in when).
    • Some contexts like editorTextFocus are provided by VS Code itself.
    • You might want to add more contexts for your own scenarios, such as resttext.enter.enabled. To manipulate them in your extension, use setContext.