57

When I instantiate classes (or call methods) with a large number of parameters I'm always using named arguments. But it's tiring to type each argument name every time:

data class User(val id: String, 
                val name: String,
                val age: Int)

val user = User(id = "1", name = "John", age = 99)

Can IDEA pre-fill parameters like this?

val user = User(
    id = ,
    name = ,
    age = 
)
Maksim Ostrovidov
  • 10,720
  • 8
  • 42
  • 57
  • 2
    Would be a cool feature, handy for groovy too. Something you can bring up with the existing Alt+Insert menu. Lodge a request at https://youtrack.jetbrains.com – Strelok Apr 04 '17 at 03:48

6 Answers6

35

There's a great plugin for that: https://plugins.jetbrains.com/plugin/10942-kotlin-fill-class

It autofills the constructor with some default parameters so you can override the ones you want ;)

douglas.iacovelli
  • 1,346
  • 2
  • 14
  • 12
22

This is the way:

  1. Right click on the constructor method
  2. Show Context Actions
  3. Add names to call arguments
  4. Profit

Right click on the constructor method

Show Context Actions

Add names to call arguments

Profit

GhostBytes
  • 542
  • 5
  • 11
  • 2
    Love it! Why it's not the right answer?! It should be – Asimaruk Sep 22 '21 at 13:41
  • 12
    This only works if you have already added the call arguments in place. Remove "someCoolValue" and then try this. You won't see "Add names to call arguments". – tir38 Apr 22 '22 at 19:50
4

Though this is not actually generating the whole call template with all the parameter names, it might be helpful anyway.

Kotlin IDEA plugin 1.1.1 suggests the parameter names in auto completion as you start typing them. For the User constructor from the example, start typing:

val u = User(i
              ^

There should be a suggestion id =:

enter image description here

It is inserted if you press Enter or Tab. Then you can continue with the other arguments:

val u = User(id = "123", n
                          ^

Here, name = should appear in suggestions, and so on.

Also, the parameters info popup should help you with it:

enter image description here

hotkey
  • 140,743
  • 39
  • 371
  • 326
3

See the following requests:

  • IDEABKL-6690 Automatic code completion when choosing a signature
  • IDEABKL-5496 Auto-filling the actual Java call arguments

There is an experimental feature you can enable by adding java.completion.argument.live.template=true into Help | Edit Custom Properties.

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
  • 1
    The experimental feature worked quite well with Java for method calls but not for constructors. However, the OP has a Kotlin tag, and I didn't see any difference with this plugin. – Gobe Jul 29 '18 at 20:53
2

If you already added all the params values in the constructor, Android studio will help you to do that. Just click on the Object, in your case on User, then click on option + enter (on mac) and you will have add names to call arguments.

JPhi Denis
  • 335
  • 5
  • 5
1

you can use Live template:

setting > Editor > Live Templates

choice code group and add by Green Plus 1.live Template

now you need fill items

Abbreviation is name for call template code.

in template type your code like it:

    val user = User(
            id = $arg1$,
            name = $arg2$,
            age = $arg3$
    )

$arg1$ means where you can type new and jump by Tab

in code when you type Abbreviation name of your code, can selected and Code Generate there

GoodLuck