Skip to content

Add "Generate constructor" option in "Show Fixes" options for fields #1358

Closed
eclipse-jdtls/eclipse.jdt.ls
#1405

Description

@rafaelrenanpacheco
Environment
  • Operating System: Linux x64 5.4.28-1-MANJARO
  • JDK version: OpenJDK 11.0.6
  • Visual Studio Code version: 1.43.2
  • Java extension version: 0.59.0
Steps To Reproduce

In IntelliJ is possible to generate a constructor from fields very easily. This enhances productivity because in Spring Boot projects there''s a lot of dependency injection through constructor. So usually we declare our dependencies as fields, then quick fix the field with a "Generate constructor" on them.

In VS Code this could be achieved through the Show Fixes options, which can be triggered by selecting the field and hitting the keyboard shortcut for the Show Fixes popup. That popup could have an option called Generate Constructors... that will open a list to choose which fields will be added in the constructor. But this option don't exists in the Show Fixes popup.

The Generate Constructors... feature already exists, but it's very unproductive to open this option. I have to right click on the editor and choose the option Source Action..., which will open a popup with some items. One of those items is the Generate Constructors... option.

The gif below shows how difficult it is to generate a constructor:

constructor

A productive approach would be to generate this constructor without any mouse interaction.

Activity

  1. fbricon commented on Apr 2, 2020

    @fbricon
    Collaborator

    You can bind source actions to a shortcut, for instance:

    {
      "key": "ctrl+shift+s",
      "command": "editor.action.sourceAction",
      "when": "editorHasCodeActionsProvider && editorTextFocus && !editorReadonly",
      "args": {
        "kind": "source"
      }
    }
    

    Make sure the key doesn't conflict with an existing keybinding. It's an editor-wide shortcut, nothing specific to vscode-java here.

  2. rafaelrenanpacheco commented on Apr 2, 2020

    @rafaelrenanpacheco
    ContributorAuthor

    Hello @fbricon!

    Your suggestion for the Source Action shortcut would indeed solve this issue. But wouldn't be better if this feature was available for default for every user? For example, I'm trying to migrate from IntelliJ to VS Code, and it's very natural (at least in my case) to look for all available "quick fix" options directly on the field, and not anywhere else on the code through the Source Actions popup.

  3. fbricon commented on Apr 2, 2020

    @fbricon
    Collaborator

    vscode-java is not the IDE. Source Actions are a common feature across vscode, I suggest you take it up there: https://github.com/microsoft/vscode/issues

  4. fbricon commented on Apr 2, 2020

    @fbricon
    Collaborator

    @snjeza please see how we can add the constructor quick fix to unused fields, like in IJ

  5. added this to the Mid April 2020 milestone on Apr 15, 2020
  6. kubajaslan commented on Sep 12, 2022

    @kubajaslan

    Just go to keyboard shortcuts in the file -> preferences - > keyboard shortcuts and look for "generate constructor" and then bind your combination of keys.

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions