Is there a way to auto-fill arguments names?

2019-02-12 17:41发布

问题:

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 = 
)

回答1:

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.



回答2:

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



回答3:

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 =:

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: