How do I hide a command in the palette menu from m

2019-08-11 09:59发布

问题:

I am building a VS Code extension starting from this page. Now I want to hide in the palette menu the command extension.timerStart after I run it. I have read this page, didn't helped. I have the code bellow for package.json. How do I make the varFromMyExtension===false part work?

  "contributes": {
    "commands": [
      {
        "command": "extension.timerStart",
        "title": "Timer Start"
      }
    ],
    "menus": {
      "commandPalette": [
        {
          "command": "extension.timerStart",
          "when": "varFromMyExtension===false"
        }
      ]
    }

回答1:

I think it is not possible to access variables from your extension directly in a when clause. However you can access any configuration of the settings.json.

From the docs (at the bottom of the chapter):

Note: You can use any user or workspace setting that evaluates to a boolean here with the prefix "config.".

So when your extension contributes a boolean configuration called varFromMyExtension you should be able to use it in the when clause. This configuration then can be manipulated programmatically, too.

So your package.json would probably contain something like this (not tested):

"contributes": {
    "commands": [
        {
            "command": "extension.timerStart",
            "title": "Timer Start"
        }
    ],
    "menus": {
        "commandPalette": [
            {
                "command": "extension.timerStart",
                "when": "!config.myextension.varFromMyExtension"
            }
        ]
    },
    "configuration": {
        "type": "object",
        "title": "Indicates whether ...",
        "properties": {
            "myextension.varFromMyExtension": {
                "title": "My title.",
                "description": "My description",
                "type": "boolean",
                "default": false,
                "pattern": "(true|false)"
            }
        }
    }
}

But bare in mind that the user can see and edit this setting, too.