Skip to content

Code actions contribution point #82718

Closed as not planned
Closed as not planned

Description

@mjbvz

Problem

For feature requests such as #52846 or any improved refactoring UX, VS Code needs to know all the code actions that are available for a language. Some information about available code actions is provided by CodeActionProviderMetadata but only if an extension has been activated and the provider has been registered

Proposal

To support this, we likely want a new contribution point that lets extensions statically tell us about the general classes of the code actions that they provide:

{
  "contributions": {
    "codeActions": [
      {
        "languages": [
          "javascript",
          "javascriptreact",
          "typescript",
          "typescriptreact"
        ],
        "actions": [
          {
            "kind": "refactor.extract.constant",
            "title": "Extract constant",
            "description": "Extract expression to a constant."
          }
        ]
      }
    ]
  }
}

Potential use cases for this info

/cc Kai Maetzel (@kieferrm)

Activity

  1. mjbvz commented on Oct 16, 2019

    @mjbvz
    Author
  2. added
    editor-code-actions
    on Oct 18, 2019
  3. mjbvz commented on Nov 6, 2019

    @mjbvz
    ContributorAuthor

    I have checked in a prototype of the static codeActions contribution point and have hooked up a few examples of how this information could be used by us, such as providing intellisense for editor.codeActionsOnSave and when configuring code action keybindings. I think that a static contribution point makes sense for use cases like these, although it means that extensions need to generate those static lists for us, see: microsoft/TypeScript#34859

    Johannes Rieken (@jrieken) brought up that not all extensions may be able to generate these lists ahead of time, such as those using Rosyln. For some use cases, we may also want to dynamically enabled/disable which code actions are shown to the user. If you are using an old TypeScript version for example, refactoring is not supported. An example of where code actions would be enabled/disabled would a improved refactor context menu in the editor

    My current thinking on static vs dynamic contribution points is that we likely want both:

    • The static info would document all possible code actions that may be available. Having this makes sense IMO when you are configuring keybindings. It also does not require activating extensions.

    • The dynamic info about what code actions are really available in the current file would be useful for the refactoring menu in the editor. The CodeActionProviderMetadata api already lets extensions provide this info to us

  4. mjbvz commented on Nov 6, 2019

    @mjbvz
    Author
  5. mjbvz commented on Nov 6, 2019

    @mjbvz
    ContributorAuthor

    Copying a few extension authors for feedback on contribution point and possible use cases

    Danny Tuppeny (@DanTup) for Dart
    Don Jayamanne (@DonJayamanne) for Python
    Dustin Campbell (@DustinCampbell) for C#

  6. mjbvz commented on Nov 7, 2019

    @mjbvz
    ContributorAuthor

    Based on trying this for JS/TS, I've added a description field and changed the schema to the following:

     "codeActions": [
          {
            "languages": [
              "javascript",
              "javascriptreact",
              "typescript",
              "typescriptreact"
            ],
            "actions": [
              {
                "kind": "refactor.extract.constant",
                "title": "%codeActions.refactor.extract.constant.title%",
                "description": "%codeActions.refactor.extract.constant.description%"
              },
              ....
            ]
          }
        ]
  7. DonJayamanne commented on Nov 7, 2019

    @DonJayamanne
    Contributor

    Are these code actions context aware (visa a callback)?
    I.e. can I have them show up in one file and not the other. Or one line and not the other?

  8. mjbvz commented on Nov 7, 2019

    @mjbvz
    ContributorAuthor

    This proposal does not effect the existing CodeActionProvider api (which lets your extension control which actual code actions are shown to the user in the editor). The static contribution point proposed here aims to supports some of the use cases outlined above without needing to activate any extensions

  9. 6 remaining items

  10. akaroml commented on Nov 8, 2019

    @akaroml
    Member

    While we do Java language support in VS Code, we always hoped there could be some way to fully expose all the capabilities. The problem with the dynamic way is that the options are contextual. So we constantly hear from the users asking for some refactoring features that are actually there. They just couldn't find them or figure out how to trigger them.

    That's why I really like this idea of having this new contribution point, which introduces the static way of exposing all possible code actions. The extension's contribution will make it even better by showing all the options in one place. Power users would want to set hot keys to specific actions, and this will also be possible and convenient.

    One feedback is still regarding discoverability. IntelliJ has this top menu entry to expose all refactoring options. It shows the full picture even when some options are not available at the moment. It would be great to have some equivalent in VS Code because refactoring is especially important for languages like Java, C#.
    image

  11. jrieken commented on Nov 8, 2019

    @jrieken
    Contributor

    Managing a cache of metadata about code actions will get very complicated. TS for example only contributes a refactor provider if you are using TS 2.8, and user can explicitly disable some code actions like organize imports through settings

    Yes, but applying the same logic how it is then better to "cache" them at compile time, e.g writing them upfront into package.json without knowing anything about the user or her/his settings nor being able to change that list? What I am suggesting is to cache them only between the time of deactivation and activation.

  12. mjbvz commented on Nov 9, 2019

    @mjbvz
    ContributorAuthor

    In our existing keybindings UX, we let users configure keybindings for any command. Many of these commands are only actually enabled when the user has enabled a setting or switched their language run time, but that does not prevent users from setting up the keybinding.

    I believe that is also the behavior we want when configure keybindings for code actions or refactors: I should always be able to configure a keybinding for extract constant if we know some extension provides it. A static contribution point enables that

  13. DanTup commented on Apr 20, 2020

    @DanTup
    Contributor

    Matt Bierner (@mjbvz) what's the status of this? If I want my code actions to show up in the keybinding list, should I be statically contributing them in package.json like this, or just providing them via documentation (like #89388)? It's not clear if documentation also includes this?

  14. DanTup commented on Apr 20, 2020

    @DanTup
    Contributor

    Strangely, although some code actions show up in the keybinding list, if I try to add my own based on the info above, I get this warning:

    Screenshot 2020-04-20 at 12 38 43

    If I remove the array (?!), then it breaks the intellisense for the existing ones, because it throws an error trying to call slice on the actions object.

  15. DanTup commented on Apr 20, 2020

    @DanTup
    Contributor

    (oh, ignoring the warning seems like the right thing - my code action then shows up! 🤨)

  16. added
    feature-request
    on Nov 3, 2020
  17. heejaechang commented on Jul 25, 2022

    @heejaechang

    Pylance team wants to expose all individuals fix all code actions to users so they can configure which fix all code actions run on file save. currently there seems no way to add those individual action names in editor.codeActionsOnSave schema. and no way to provide default value for code actions on save for each fix all code actions.

    I think that makes it very hard for users to discover each fix all actions and make default such as source.fixAll: true very intrusive since updating extension without knowing that it just added new fix all code action can change users code unexpectedly. furthermore, in current design, it will be very hard to find out which extension actually did it.

    Please, provide a way to configure this by each fix all code action provider

  18. added
    *out-of-scope
    on Dec 9, 2024
  19. vs-code-engineering commented on Dec 9, 2024

    @vs-code-engineering
    Contributor

    We closed this issue because we don't plan to address it in the foreseeable future. If you disagree and feel that this issue is crucial: we are happy to listen and to reconsider.

    If you wonder what we are up to, please see our roadmap and issue reporting guidelines.

    Thanks for your understanding, and happy coding!

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

Metadata

Metadata

Labels

*out-of-scopeapiapi-proposaleditor-code-actionsfeature-request

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions