question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue
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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
fbriconcommented, Apr 2, 2020

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.

0reactions
kubajaslancommented, Sep 12, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I generate a constructor from class fields using Visual ...
Just by highlighting properties and then press Ctrl + . and then press Generate Constructor. For example, if you've highlighted two properties it...
Read more >
Generate a constructor quick action - Visual Studio (Windows)
Learn how to use the Quick Actions and Refactorings menu to immediately generate the code for a new constructor on a class.
Read more >
Constructor Already Exists, Can not create customize ...
What steps will reproduce the issue? 1.Create entity class 2.right click -> generate->Constructor. (It creates custom default constructor with out any error)
Read more >
Generate Class Constructors in Eclipse Based on Fields or ...
You'll often need to add a constructor to a class based on some/all of its fields or even based on constructors of its...
Read more >
Applying Refactoring Actions | Qt Creator Manual
You can specify settings for generating the functions either globally for all projects or separately for each project in the build and run...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found