https://kotlinlang.org logo
#intellij
Title
# intellij
e

Eugen Martynov

02/28/2024, 4:43 PM
Is there way to force code style to have trailing comma but with one exception for when cases
Copy code
return when (view) {
                    is ContractView,
                    is ProductTile,
                    -> view.tag == text

                    else -> false
                }
Looks a bit odd.
youtrack 2
e

Emil Kantis

02/28/2024, 6:09 PM
You could write a custom Ktlint rule. I’m currently working on a package of rule flavors that adjusts a few things from the Ktlint official style. Feel free to check it out for examples (you’ll have to dig around in the code a bit)https://github.com/Kantis/kantis-ktlint-rules
e

Eugen Martynov

02/29/2024, 9:05 AM
Thanks!
e

Emil Kantis

03/04/2024, 10:57 AM
I think it's invalid to call it useless in 2nd case?
Copy code
fun foo(bar: Any)  {
    val a = when (bar) {
        is Int,  //"Useless trailing comma" not reported
        -> true
        else -> false
    }
}
Can be modified to
Copy code
fun foo(bar: Any)  {
    val a = when (bar) {
        is Int,  //"Useless trailing comma" not reported
        is Float,
        -> true
        else -> false
    }
}
Without having a diff on the line with "useless" comma.
a

Anton Mefodichev

03/04/2024, 11:05 AM
I reckon in this case it can be reported as useless on the line with a Float check.
Or we need to add an option to the inspection settings...
@Emil Kantis What do you think?
I have looked again, and it looks as by-design behaviour in the second case as we lose an initial sense of trailing comma.
e

Eugen Martynov

03/04/2024, 11:35 AM
Anton, sorry coming back to my original question - is it correct behaviour? I believe in some extent it is. So I wonder if we could have a settings to override it.
e

Emil Kantis

03/04/2024, 11:45 AM
I think it should be a separate rule all-together.. like "Single when-clause should be on a single line" to force
is X -> Y
to not contain linebreaks if it's just a single case
a

Anton Mefodichev

03/04/2024, 11:46 AM
It is quite unclear indeed how we should proceed in this case. Maybe the best way will be adding an additional option to the inspection settings for this case (when condition + several options to check in a raw). It more looks like an unsupported yet case.
7 Views