microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
164.18k stars 29.29k forks source link

`contributes.commands.*.when` clause not in schema and ignored in VS Code #177993

Closed fregante closed 1 year ago

fregante commented 1 year ago

Type: Bug

I'm working on an extension that adds two alternating commands, but VS Code is showing them both:

Screenshot

The conditions are simple and they cannot be both true at the same time:

{
  "contributes": {
    "title": "GhostText",
    "commands": [
      {
        "command": "ghostText.startServer",
        "title": "GhostText: Start server",
        "when": "!ghostText.server"
      },
      {
        "command": "ghostText.stopServer",
        "title": "GhostText: Stop server",
        "when": "ghostText.server"
      }
    ]
  }
}

It feels like I'm doing something wrong because when doesn't auto-complete at all:

2

Source: https://github.com/fregante/GhostText-for-VSCode/blob/87295321270a68074fd81c905ef6a6f768748d08/package.json/#L29

Extension: https://marketplace.visualstudio.com/items?itemName=fregante.ghost-text

VS Code version: Code 1.76.2 (ee2b180d582a7f601fa6ecfdad8d9fd269ab1884, 2023-03-14T17:54:09.061Z) OS version: Darwin x64 22.3.0 Modes: Sandboxed: No

Extensions (31) Extension|Author (truncated)|Version ---|---|--- vscode-css-formatter|aes|1.0.2 sidebar-markdown-notes|ass|1.1.0 vscode-fish|bma|1.0.33 clippy-ai|cli|0.5.0 js-codeformer|cms|2.6.1 vscode-mac-color-picker|dae|1.1.0 vscode-eslint|dba|2.4.0 gitlens|eam|13.4.0 prettier-vscode|esb|9.10.4 vscode-open-in-github|fab|1.3.0 ghost-text|fre|1.3.0 codespaces|Git|1.14.0 remotehub|Git|0.52.0 vscode-github-actions|git|0.25.0 beautify|Hoo|1.5.0 find-unused-exports|iul|1.11.1 ts-error-translator|mat|0.8.0 dotenv|mik|1.0.1 azure-repos|ms-|0.28.0 remote-repositories|ms-|0.30.0 sublime-keybindings|ms-|4.0.10 docthis|oou|0.8.2 vscode-yaml|red|1.12.2 synthwave-vscode|Rob|0.1.15 vscode-text-tables|Rom|0.1.5 linter-xo|sam|3.14.1 vscode-fileutils|sle|3.10.1 svelte-vscode|sve|107.2.5 dependencygraph|sz-|1.1.11 shellcheck|tim|0.29.4 change-case|wma|1.0.0
fregante commented 1 year ago

Pardon me, it's likely I'm doing something wrong but the docs are not helping: https://code.visualstudio.com/api/references/contribution-points#contributes.commands

I only see a mention for menus here: https://code.visualstudio.com/api/references/when-clause-contexts#in-and-not-in-conditional-operators

Is contributes.commands.*.when not set up?

fregante commented 1 year ago

It seems that this is the right configuration, which is both verbose and unclear from the docs (no such examples). Correct me if I'm wrong


    "menus": {
      "commandPalette": [
        {
          "command": "ghostText.startServer",
          "when": "!ghostText.server"
        },
        {
          "command": "ghostText.stopServer",
          "when": "ghostText.server"
        }
      ]
    },
    "commands": [
      {
        "command": "ghostText.startServer",
        "title": "GhostText: Start server"
      },
      {
        "command": "ghostText.stopServer",
        "title": "GhostText: Stop server"
      }
    ],
ulugbekna commented 1 year ago

Hi @fregante ,

Thanks for filing the issue and feedback :-)

What you're looking for is enablement instead of when, ie

{
  "contributes": {
    "title": "GhostText",
    "commands": [
      {
        "command": "ghostText.startServer",
        "title": "GhostText: Start server",
        "enablement": "!ghostText.server"
      },
      {
        "command": "ghostText.stopServer",
        "title": "GhostText: Stop server",
        "enablement": "ghostText.server"
      }
    ]
  }
}

You're right, the docs (link) mention that but could indeed be clearer:

Contribute the UI for a command consisting of a title and (optionally) an icon, category, and enabled state. Enablement is expressed with when clauses.

ulugbekna commented 1 year ago

Btw, there's this inconspicuous arrow you can press that shows info about the object field in a hover widget:

Before clicking on arrow:

image

After:

image