Is there a way to auto-fill arguments names?

2019-02-12 17:18发布

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

3条回答
来,给爷笑一个
2楼-- · 2019-02-12 17:45

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.

查看更多
祖国的老花朵
3楼-- · 2019-02-12 17:51

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

查看更多
Ridiculous、
4楼-- · 2019-02-12 17:59

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

查看更多
登录 后发表回答