How do I hide a command in the palette menu from my extension in VS Code

微笑、不失礼 提交于 2019-12-11 08:58:32

问题


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.



来源:https://stackoverflow.com/questions/51687341/how-do-i-hide-a-command-in-the-palette-menu-from-my-extension-in-vs-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!