Is there a setting to add trailing commas to funct...
# intellij
b
Is there a setting to add trailing commas to function arguments when reformatting? Adding them to the parameter declarations works. Before:
Copy code
fun f(
      a: Int,
      b: Int
  ) {
  }

  val a = f(
      1,
      2
  )
After:
Copy code
fun f(
      a: Int,
      b: Int, // \o/ 
  ) {
  }

  val a = f(
      1,
      2  // :(
  )
w
Deleted my original message, I was under impression that trailing commas code style setting should apply everywhere where trailing commas are supported. But I found IJ formatter to quite buggy wrt trailing commas and I rely on ktlint to add them when needed
b
Hmm sad. Do you use ktlint in the IDE (is there a plugin maybe?)
d
I wanted to reply with an IDE setting but it apparently disappeared in recent version 😞 I too use ktlint for formatting and yes there is a ktlint IDE plugin which can autoformat your code on save based on your editorconfig settings.
See also #CKS3XG0LS for further questions.
b
all right thanks! I'll check it out. (These commas are really handy especially with Compose where you tend to have functions with dozens of arguments 😅)
d
Using
.editorconfig
has been the most consistent way I've seen.
b
@Daniel Pitts does that support trailing commas? I don't see anything about it here
d
There are some intellij extensions... let me see if I can find them.
Copy code
ij_kotlin_allow_trailing_comma = true
ij_kotlin_allow_trailing_comma_on_call_site = false
🙏 1
b
ij_kotlin_allow_trailing_comma_on_call_site
looks promising!
d
If you create a
.editorconfig
file, and put in
[*.kt]
, you can then use intellij's "generate" hotkey (⌘N on Mac) and it will give you the open to create all of the possible keys that intellij knows about.
On my machine, it created 84
b
wow!
thanks a lot! I'll check that out 🙂
w
It also autocompletes nicely so you can type
ij
and/or
kt
and you'll see possible options. But fwiw editorconfig is pretty broken as well ☹️
b
all right it looks like this is a winner! just created a
.editorconfig
file and put this inside:
Copy code
[*.kt]
ij_kotlin_allow_trailing_comma_on_call_site = true
then reopened my project, now cmd+alt+L adds the sweet commas 🎉 Thanks all 🙏
nod 1