Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS Code extension: Hide commands from command palette

Background:

I'm currently working on a simple VS Code extension that will provide dynamically set command variables to my build & debug tasks. The plan is to have a series of attributes that will be accessible in launch.json, etc. through the "${command:extension.myCommand}" syntax.

Registering commands like this is as simple as adding an entry to the package.json file, e.g.:

"contributes": {
    "commands": [
        {
            "command": "extension.myCommand",
            "title": ""
        }
    ]
}

and implementing the corresponding commands in my main extension file:

let disposable = vscode.commands.registerCommand('extension.myCommand', () => {
    return "dynvar";
});
context.subscriptions.push(disposable);

The Problem:

Unfortunately these commands now also appear in the command palette, and as they don't have any interactive use that's quite annoying.

Question:

Is there any way I can hide commands contributed through extensions from VS Code's command palette?

like image 714
Glutanimate Avatar asked Mar 20 '19 22:03

Glutanimate


2 Answers

If you don't need to associate an "icon" or a "title" with your command, you can simply omit it from "commands" - commands that are not listed there can still be called, as long as they have been registered via vscode.commands.

Otherwise, you can use the following trick to hide it from the command palette:

"contributes": {
    "menus": {
        "commandPalette": [
            {
                "command": "extension.myCommand",
                "when": "false"
            }
        ]
    }
}
like image 167
Gama11 Avatar answered Oct 16 '22 14:10

Gama11


The following snippet illustrates how to hide a command registered as 'extension.myHiddenCommand':

"contributes": {
    "commands": [
    {
        "command": "extension.myHiddenCommand",
        "title": "Compile folder"
    }],
    "menus": {
        "commandPalette": [
        {
            "command": "extension.myHiddenCommand",
            "when": "false"
        }]
    }
}
like image 38
rygo18 Avatar answered Oct 16 '22 15:10

rygo18