Skip to content
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

Allow editor commands to be registered in extension code without adding to package.json #1422

Closed
daviwil opened this issue Dec 17, 2015 · 24 comments
Labels
api feature-request Request for new features or functionality menus Menu items and widget issues *out-of-scope Posted issue is not in scope of VS Code
Milestone

Comments

@daviwil
Copy link
Contributor

daviwil commented Dec 17, 2015

I've got an idea to allow extra extensibility of VS Code using PowerShell via my extension. Here's a proposal that I wrote up with some initial ideas:

PowerShell/PowerShellEditorServices#81

The core requirement for my extensibility model is to be able to add new commands to VS Code after my extension has already loaded, without also having to add them to my package.json. This will allow my users to write new actions, analyzers, and code formatters which they could invoke using VS Code's great keybinding system.

Currently the API lets you register the ID of the command and the function to be called. If we could also get parameters for the display name and category of the command it should be enough to accomplish full dynamic command registration. As a bonus it would nice to have a parameter for registering a default key binding for the command, but it's OK if that comes later.

I realize that there's probably a reason why you'd prefer to have commands be registered in package.json rather than completely in code. My guess is that it allows the commands to be displayed before the extension is fully loaded. The dynamic commands I'd like to have wouldn't need to be displayed until the extension is fully loaded anyway, so this doesn't matter as much to me.

Thanks for considering this!

@egamma egamma added feature-request Request for new features or functionality api labels Dec 17, 2015
@egamma egamma added this to the Backlog milestone Dec 17, 2015
@egamma
Copy link
Member

egamma commented Dec 17, 2015

@jrieken fyi

@egamma egamma closed this as completed Dec 17, 2015
@daviwil
Copy link
Contributor Author

daviwil commented Dec 17, 2015

Was this meant to be closed? I couldn't find a similar issue in the backlog :)

@egamma
Copy link
Member

egamma commented Dec 17, 2015

sorry, wrong button

@egamma egamma reopened this Dec 17, 2015
@daviwil
Copy link
Contributor Author

daviwil commented Feb 12, 2016

I've been talking to a few people about this recently and there seems to be a real interest from users of the PowerShell extension to be able to write new VS Code commands using PowerShell. I'd like to gauge your interest in providing the capability of dynamic command registration so that I can enable this scenario.

I know that on the list of valuable things you could do post-Build, this is probably not very high. However, I think it could open the door to even more ways to extend VS Code using any language without any significant changes to the current extensibility model.

Here's a description of how it would work:


The user installs the PowerShell extension for Visual Studio Code. One day, they hear about a new PowerShell module that provides a custom formatting command for PowerShell scripts. They install this module using the PowerShell Gallery.

The next time they start Visual Studio Code, the language service scans their locally installed PowerShell modules for any that contain editor command extensions. The new script formatting extension is found and loaded. This causes a special message to be sent from the language service to the PowerShell extension in VS Code to indicate that an editor command extension was loaded.

The PowerShell extension then uses the VS Code APIs to dynamically register this new command with its ID, display name, and default key binding. If the key binding is already in use, the default key binding is ignored. The user can now see and execute the "PowerShell: My custom formatter" command in VS Code's command palette. They can also bind this command to a key combination of their choosing in keybindings.json.

When the custom command is executed, the PowerShell extension will use VS Code's APIs to gather context about the user's currently open file and the current selection or cursor position. This information is then sent with a request to the PowerShell language service to execute a custom command with the selected ID. The custom command runs its logic in PowerShell and then returns a result which contains edits to be applied to the file.

When the response is returned from the language service, the PowerShell extension will translate the desired edits to the VS Code APIs so that they're applied to the user's file buffer.


Aside from code formatting extensions, I'd also be providing general action commands (execute some task) or analysis commands (run some special linter or script checker). The standard VS Code APIs will be used for all extension capabilities.

There are two major benefits to this approach for PowerShell users:

  1. They can write new editor commands in a language they're already comfortable with.
  2. These same editor commands can be made available in the PowerShell ISE and any other editor where our language service (PowerShell Editor Services) is used.

Would you be willing to accept a pull request from me to enable this after the appropriate design discussion has taken place?

Thanks!

@jrieken
Copy link
Member

jrieken commented Mar 7, 2016

A workaround is to have a command that opens quick pick with a list of (dynamic) commands. Omnisharp vscode does this for project specific commands and package restore: https://github.com/OmniSharp/omnisharp-vscode/blob/master/src/features/commands.ts#L109

@daviwil
Copy link
Contributor Author

daviwil commented Mar 7, 2016

Yep, this is what I was going to do for now. The main 2 downsides with this approach are the reduced discoverability and inability to bind a hotkey to those commands. I'd prefer it if the extension commands could show up as top-level commands under the "PowerShell" category.

@W4RH4WK
Copy link
Contributor

W4RH4WK commented Oct 21, 2016

I had the idea of creating an extensions which allows you to setup commands (and bind them to hotkeys) in a more generic way. This way all the "open-in" / "open-with" / filter selected lines with shell command, ... extensions could be substituted with this generic one.

But since it is not possible to add / remove commands dynamically I did not bother implementing a prototype. I asked (#11045) whether they'd implement such functionality into vscode itself, but they declined.

@geddski
Copy link

geddski commented Nov 18, 2016

I wrote an extension that could help with this. It allows you to create custom macros and bind them to hotkeys.

https://marketplace.visualstudio.com/items?itemName=geddski.macros

@bcardarella
Copy link

@daviwil this issue is over a year old, has there been any movement on it?

@daviwil
Copy link
Contributor Author

daviwil commented Jul 1, 2017

@bcardarella Not yet, but I'd love to see it get added

@fabiospampinato
Copy link
Contributor

This functionality would be pretty useful to me. I'm making an extension for adding commands like "Open in Chrome/Firefox" to the palette, but configurable, so that if you need to use Yandex or something you can add a command for it, but turns out there's no API for doing this.

Resolving to a QuickPick is a sub-optimal solution.

@pranaygp

This comment has been minimized.

@OmarTawfik
Copy link

OmarTawfik commented Apr 2, 2019

This feature would be really useful here as well.
@jrieken @egamma are there blockers that prevent this from happening?
Are you willing to accept a PR to add this functionality to vscode?

@OmarTawfik
Copy link

Any thoughts on this? there has been multiple asks from multiple people. Please let us know if it is something that you're willing to take into the platform, or you decided to not support it.

@OmarTawfik
Copy link

@jrieken @egamma can you please advise?
This would be useful in case of workspace commands as well, not just editor commands.
Would this be an API change you're willing to accept?

@OmarTawfik
Copy link

@jrieken @egamma can you please advise? :)

@ghost
Copy link

ghost commented May 28, 2019

Yes found that out yesterday it is working now on all editions of windows

@OmarTawfik
Copy link

@snewfie is this comment intended here? Which API did you try?

@SeeminglyScience
Copy link

@OmarTawfik I believe they meant to respond in SeeminglyScience/EditorServicesCommandSuite#41.

@DavidGoldman
Copy link
Contributor

Friendly ping, this would also be useful for source control to allow users to define their own aliases (e.g. hg amend && hg push)

@jedwards1211
Copy link
Contributor

jedwards1211 commented Jan 17, 2020

I'm wanting to contribute to an extension for doing jscodeshift transforms on the current editor, and it would be really nice to be able to map each transform to a separate command. Then users could also assign keyboard shortcuts to each transform command.

As it stands, (and as others have mentioned) I have to use a static command that brings up a quick pick of transforms, so I can't assign a keyboard shortcut to an individual transform.

Static configuration usually leads to an awkward DSL that papers over some of the static limitations but don't fully solve the problem. (extends and env in .babelrc files, intrinsic functions in CloudFormation templates, etc.)

@jrieken jrieken added *out-of-scope Posted issue is not in scope of VS Code menus Menu items and widget issues labels Nov 3, 2020
@mrienstra
Copy link
Contributor

@jrieken, 2.7 years later, is this request still "out of scope"?

Would limiting the possibilities to mitigate abuse make it more palatable?

  • Limit the number that an extension can add (ideally user overridable).
  • Dynamic user facing commands could be required to contain a static prefix or suffix, or match a static regex.
  • When (if?) extension permissions exist (see [Feature Request] Extension Permissions, Security Sandboxing & Update Management Proposal #52116), that would be another avenue, letting the user know the extension wants the ability to create dynamic user facing commands.
  • Not sure if there is any precedent for this, but it could also require explicit permission from the user for each command or batch of commands added by an extension, e.g. via a confirmation dialog.

@tiejunhu
Copy link

This is a major issue blocks vscode to be a hackable editor(like emacs or vim). The editor's user (not plugin/addon author) cannot add a command to the command palette.

@jurgenvinju
Copy link

Thanks for making VScode! Having great traction with it for DSL design and implementation.

For extension generators such as Rascal (produces a VScode extension from a "language description") this feature would be very useful. Currently we register one LSP server that multiplexes on file extension to redirect every LSP call to the right language. Every programmable feature that is locked in package.json is therefore out of our reach. The great benefit is that we can roll experiments with new LSP servers without a second level or rebooting. That's worth a lot to our users (typically DSL engineers).

We currently have to give every new DSL-specific command a place as a "lense" instead. It would be better for our users if they can also access DSL-specific commands that are not exposed as lenses but simply in the command palette.

By the way, we have similar issues with other package.json-locked features like language-specific bracket-balancing and comment prefixes/postfixes. A programmable alternative would be great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api feature-request Request for new features or functionality menus Menu items and widget issues *out-of-scope Posted issue is not in scope of VS Code
Projects
None yet
Development

No branches or pull requests

16 participants