如何在 VS Code 的扩展程序中隐藏调色板菜单中的命令

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

我正在构建一个从 this page. Now I want to hide in the palette menu the command extension.timerStart after I run it. I have read this 页面开始的 VS Code 扩展,但没有帮助。我有 package.json 的代码如下。如何使 varFromMyExtension===false 部分起作用?

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

我认为无法直接在 when 子句中访问扩展中的变量。但是,您可以访问 settings.json 的任何配置。

来自docs(在章节底部):

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

因此,当您的扩展贡献 boolean configuration called varFromMyExtension you should be able to use it in the when clause. This configuration then can be manipulated programmatically 时。

所以你的 package.json 可能包含这样的东西(未测试):

"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)"
            }
        }
    }
}

但请记住,用户也可以查看和编辑此设置