73

If my class has a non-empty constructor, is it possible to auto-complete parameters in the new expression?

With Eclipse, if you press ctrl+space when the cursor is between the parenthesis:

MyClass myObject = new MyClass();

it will find the appropriate parameters.

-->  MyClass myObject = new MyClass(name, value);

When I use ctrl+shift+spacebar after the new, Intellij shows me the constructors, but I can't choose one for auto-completion. Am I missing an option?

Pang
  • 9,564
  • 146
  • 81
  • 122
Loic
  • 3,310
  • 4
  • 25
  • 43
  • why isn't such a simple feature available out of the box when you press ctrl+space?? – ACV Jun 27 '19 at 14:59
  • Why did you change the accepted answer in 2016 to one with three upvotes when the answer from 2011 has 116? – Noumenon Feb 17 '21 at 01:57

5 Answers5

125

I usually start with CtrlP (Parameter Info action) to see what arguments are accepted (auto guess complete is way to error prone in my opinion). And if as in your case you want to fill in name type n a dropdown menu appears with all available variables/fields (etc) starting with n Arrow Up/Down and Tab to select name, or CtrlSpace to select a method (or even CtrlAltSpace to be killed by suggestions;-), followed by , and v Tab for value.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
osundblad
  • 2,675
  • 1
  • 29
  • 34
19

Well I used the eclipse key map where Parameter Info is unassigned. Here is how to change that:

enter image description here

joecks
  • 4,539
  • 37
  • 48
13

Well there's the Ctrl+Shift+Space combination, which tries to come up with a set of possible arguments. And if you press the Ctrl+Shift+Space a second time, Idea tries find arguments which fit across multiple calls & conversions.

So in your example Ctrl+Shift+Space would almost certainly bring up the 'name' as suggestion. And the next Ctrl+Shift+Space would bring up 'value' as suggestion.

osundblad
  • 2,675
  • 1
  • 29
  • 34
Gamlor
  • 12,978
  • 7
  • 43
  • 70
5

In Intellij Idea 2016.3 you can use option + return. It will ask you if you want to introduce the named argument for the argument you are on and all the followers.

enter image description here

Filippo De Luca
  • 704
  • 5
  • 21
  • 2
    That doesn't work for me. Is it specific to a certain language? – aakoch Mar 08 '17 at 23:01
  • I have tested in in Scala and Mac OSX, not sure about other languages/environment. – Filippo De Luca Mar 22 '17 at 20:44
  • 1
    Most versions of Java that are in production use today don't support named arguments. Named arguments helper won't auto-complete the constructor anyway, it will just change the style of the already specified constructor invocation. – milosmns Dec 31 '20 at 11:45
  • 1
    In case you switched to Kotlin by now, there's a plugin for that: https://plugins.jetbrains.com/plugin/10942-kotlin-fill-class – milosmns Dec 31 '20 at 11:48
2

There's no such possibility yet. As IDEA doesn't fill the arguments automatically, distinguishing the constructors in the lookup makes no sense. There's a request for that (http://youtrack.jetbrains.net/issue/IDEABKL-5496) although I sincerely believe such a behavior is too dangerous and error-prone.

Peter Gromov
  • 17,615
  • 7
  • 49
  • 35
  • 3
    Ctrl+P, Parameter info (within method call arguments). – user7610 Jun 26 '14 at 23:08
  • 9
    I disagree - why would it be so dangerous that a user could at least opt to enable it? It is a very useful function and probably easy to implement it - so I don't see the value in not implementing this feature. – quantum Oct 10 '14 at 22:03
  • 1
    It's always funny when any vendor want to tell me what is a good or bad development behaviour. And it makes very well sense because when I create a class, I need to choose which ctor to use. Otherwise ctors would not make sense. – Bevor Sep 17 '16 at 14:46
  • 1
    Jens Nerche seems to have developed a plugin for this now, see https://github.com/kontext-e/AutoFillingCallArguments. It's added to the Intellij's plugin repo. – AndiDev Sep 27 '16 at 18:03
  • 1
    The Eclipse foundation apparently does not think this feature is "too dangerous and error-prone" as it's been in their IDE for years. :) – Oliver Hernandez Jul 24 '18 at 18:22
  • Too dangerous? Don't use it. Simple and tidy. – Kramer Nov 18 '18 at 03:01