Skip to content

Source Actions Api proposal #47621

Closed
Closed

Description

@mjbvz

Motivation

JavaScript currently implements organize imports as a command. So does Java. Each extension also has its own keybinding for organize imports.

With common actions like organize imports that will be implemented by multiple extensions, we risk making each extension reinvent the wheel and possibly creating conflicts other extensions. It is already very easy to forget to properly scope your organize imports command when defining it, which may cause vscode to trigger the wrong language's organize imports for a file.

Proposal

Provide an API that allows extensions to register file-level actions, such as organize imports, in a standard way. VS Code can use this API to provide a standard keybinding for common actions such as organize imports, and also ensure that we correctly handle cases where multiple organize imports actions exist for a single file

Proposed Implementation

Leverage the existing CodeAction class to create the concept of a source code action. Introduce a new scope source.* scope to define source code actions.

We can then either leverage the existing CodeActionProvider api or introduce a new SourceCodeActionProvider class that allows extensions to return source code actions.

The expected ux in either case:

  • Do not show source code actions in the light bulb menu or when using the editor.action.quickFix keyboard shortcut

  • Provide a right context click menu that shows a list of all source code actions for the current file. Do not show an entry in this context menu if no source code action providers are registered for the file

  • Also provide a way to setup a keybinding for specific source code actions. This could either reuse the existing editor.action.codeAction command or introduce a new editor.action.sourceCodeAction command

Johannes Rieken (@jrieken) Let me know if you have any concerns or ideas about the proposal or implementation. We can also cover this at the API sync. Overall, I'm leaning towards reusing our the existing CodeActionProvider class for this. The main downside of this approach is that I don't think we could make the source context menu only show up if these is an actual source code action provider since we could not know whether a registered CodeActionProvider will returns source actions or not

Activity

  1. added
    under-discussion
    on Apr 11, 2018
  2. jrieken commented on Apr 11, 2018

    @jrieken
    Contributor

    I am 👍 on re-using the same provider. We could extend the registerCodeActionProvider-function to accept a set of code action kinds it will work on (similar to trigger characters and completion items).

  3. mjbvz commented on Apr 11, 2018

    @mjbvz
    ContributorAuthor

    Added basic CodeActionKind.Source implementation as outlined here. A bit unsure about the name Source but I don't really have a better suggestion. Maybe Document Actions? But then where would an action like format selection fit?

  4. removed
    under-discussion
    on Apr 12, 2018
  5. DanTup commented on Apr 24, 2018

    @DanTup
    Contributor

    Matt Bierner (@mjbvz) Is this going to come with support for running it on Save? (we currently have our own setting, but if you're going to ship with native support, I'll remove the setting on the branch where I've implemented this).

  6. mjbvz commented on Apr 24, 2018

    @mjbvz
    ContributorAuthor

    Yes, already implemented by #42092. Your extension can contribute a language specific setting that does something like:

    "[dart]": {
    	"editor.codeActionsOnSave": {
    		"source.organizeImports": true
    	}
    }

    in order to enable organize imports on save just for dart

  7. DanTup commented on Apr 25, 2018

    @DanTup
    Contributor

    Perfect, thanks!

  8. locked and limited conversation to collaborators on May 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions