Skip to content

feat: conditionallyExecuteCommand API proposal #249742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

joe-p
Copy link

@joe-p joe-p commented May 25, 2025

This PR is based on @jrieken comment for allowing access to context through the API:

You should not be reading and evaluating context keys. Instead you should be more like an "event source" and we should own the tuple [event, when-clause, command] - just like we do it for keybindings. Something like this

  1. extension registers event source
  2. vscode allows to bind events to commands using when-clauses
  3. extension fires event -> vscode evaluates the binding (using the focused/active element) and invokes the command

To implement this, I've added a new function to the commands API that allows conditional execution of commands based on the context of the active element. This could be a separate functionality from commands, but I figured it was better to add on to the existing commands implementation rather than creating an entirely new event registration system.

Tests implemented in:

  • extensions/vscode-api-tests/src/singlefolder-tests/commands.test.ts
  • src/vs/workbench/api/test/browser/mainThreadCommands.test.ts

Closes #249741

Addresses common requests in #10471

@@ -9,6 +9,7 @@
"authSession",
"chatParticipantPrivate",
"chatProvider",
"conditionallyExecuteCommand",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The linter is throwing an error here, but it was not obvious how I should fix it

@joe-p
Copy link
Author

joe-p commented May 25, 2025

@microsoft-github-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow conditional execution of commands via the API based on a when expression
3 participants