Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to auto-fill arguments names?

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 = 
)
like image 661
Maksim Ostrovidov Avatar asked Apr 04 '17 03:04

Maksim Ostrovidov


6 Answers

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

like image 129
douglas.iacovelli Avatar answered Nov 16 '22 16:11

douglas.iacovelli


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

like image 15
GhostBytes Avatar answered Nov 16 '22 18:11

GhostBytes


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

like image 4
hotkey Avatar answered Nov 16 '22 18:11

hotkey


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.

like image 3
CrazyCoder Avatar answered Nov 16 '22 17:11

CrazyCoder


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.

like image 2
JPhi Denis Avatar answered Nov 16 '22 17:11

JPhi Denis


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

like image 1
HamidReza Heydari Avatar answered Nov 16 '22 16:11

HamidReza Heydari