Is there a feature to automatically add in named parameters for new functions?

I saw that there was an intention to 'use named arguments for current and all subsequent arguments'

is there any way to have the plugin automatically generate these for a new method?

e.g.

I have this function:

def foo(a:Int, b: String, c: Long) {???}

when I type:

fo<ctrl+space>

I'd end up with
foo($cursor$)

at this point it would be nice to have an intention "add all named arguments"
which if selected would produce this:

foo(a=,b=,c=)

and then cursor would jump from a to b to c just like in a live template so that they can be quickly filled in

评论操作 固定链接

Hi, Benjamin,
There is no such action. Do you often use named arguments? If so, can you describe your usage scenarios?

If you just want to check signature of a method, you can use "Parameter Info" feature (Ctrl + P). It shows names, types and default values of parameters. It is also handy if there are overloaded alternatives.



Attachment(s):
parameter info.jpg
2
评论操作 固定链接

Yeah, I use named parameters quite frequently, basically whenever the order of arguments to a method don't have a natural, well known order, and the types are primitives.

In this case named parameters makes the code more readable at the call site, and guarantees that if someone changes order at the declaration site, the call site isn't affected.

So my workflow ends up writing the method name, using that to jump to the declaration, selecting the parameters, copy them, going back to the call site, pasting them, erasing the types, adding a bunch of '=' and then filling the method while the IDE blast a bunch of red-squiggly lines at me.

edit: Yep thanks for the suggesting of ctrl+p, but unfortunately, when you have a method that has several parameters, helpful as ctrl+p is to put them in the right order, it's a lot of rote typing to get the names all filled in.

edit2: perhaps a better workflow would be to use ctrl+p to just put them in the right order initially then use the intention to add the names after I do that.

2
评论操作 固定链接

This really improves readability of the code. Feature requested! 👍

9
评论操作 固定链接

+1, would be a huge time-saver!

2
评论操作 固定链接

+1 yes, please add this feature.  

2
评论操作 固定链接

There is an intention called "Add names to call arguments", but for some reason it is not always available.

edit: The intention is not always available because "named arguments are not allowed for non-Kotlin functions."

0
评论操作 固定链接

Any update on this from Jetbrains? I would like this feature as well.

1
评论操作 固定链接

+100000

Named args are a de-facto best practice, and it would help a lot to have a method call pre-filled with e.g.:

```

myFunc(id =  , numerator =  , denomenator =  )

```

0
评论操作 固定链接

too bad it isnt compatible with current versions

0
评论操作 固定链接

There is a new plugin, kotlin-function-arguments-helper (https://plugins.jetbrains.com/plugin/14168-kotlin-function-arguments-helper) that does this and is more compatible.

It's still not nearly as good as it could be -- I really want the tab-through-arguments behavior.

I get the sense that the JetBrains folks don't use named arguments nearly as much as some of us do. I use them constantly, basically any method with more than one argument. I think it makes code much more readable. I also put each argument on it's own line, which helps a lot with that.

0
评论操作 固定链接

Is there any plugin or other way to add these names in bulk? i.e. if I wanted all callers of and methods in a package to get the named parameters added?

0

请先登录再写评论。