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 =
)
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 ;)
This is the way:
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:
See the following requests:
There is an experimental feature you can enable by adding java.completion.argument.live.template=true
into Help | Edit Custom Properties.
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
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With