https://kotlinlang.org logo
#getting-started
Title
# getting-started
p

Paulius O

10/30/2021, 6:50 AM
Hello I have a question about auto-formatting, Why is it that we get this uneven indentation in the following situation(where `a`’s indentation is smaller than the rest?):
Copy code
val myVal = (
    a.size +
        b.size +
        c.size)
Is there a way to change klint settings to align it automatically? I would prefer all to be offset at the same level. If there isn’t maybe someone can explain why indentation the way that it’s working now is better?
here’s a screenshot if that’s easier: https://imgur.com/a/FUTPXuF
v

Vampire

10/30/2021, 9:29 AM
Who formatted it like that? Klint or your ide? I think intellij used to do it like that, but was long ago changed for Kotlin top match the official coding conventions, but you might still user the old settings. There continuation indent was double and closing parentheses were not on a new line. The thought behind the double indent is, that you see the expression of the previous line continues. If it is aligned and you have the operator at the previous line, you could easily miss it and read the second line as individual statement.
p

Paulius O

10/30/2021, 9:40 AM
thank you, Björn! we ran Ktlint via
./gradlew ktlintFormat
We are using the latest version -> 0.42.1 are Klint and Ktlint different things?
v

Vampire

10/30/2021, 5:17 PM
You said klint, I just repeated. I don't use ktlint so I have no idea about its options or usage.
k

Klitos Kyriacou

11/01/2021, 9:11 AM
IntelliJ IDEA also does it like this (but with 8-space indentation on the a.size line and 16-space indentation on the following lines) when it is set to use the Kotlin Style Guide. I too would prefer the lines to be indented equally, but there are some other situations in which this style make more sense.
v

Vampire

11/01/2021, 6:01 PM
Ah, ok, it's because they are simple grouping parentheses and not method call parentheses. If it were
= bar(
it formats with the closing parenthesis on a new line and only indented 4 spaces. Might be a bug in the default Kotlin format settings in IntelliJ.
k

Klitos Kyriacou

11/01/2021, 6:03 PM
In fact the parentheses only served to confuse, as they are not needed in the OP's question.
p

Paulius O

11/02/2021, 9:13 AM
parentheses are for sure not necessary for this example. In the real case I’m referring to I am comparing the value from parentheses to another value. e.g.
Copy code
val myVal = (
    a.size +
        b.size +
        c.size) == 1
but ignoring that, even without the comparison to another value and parentheses the indentation is still different:
Copy code
val myVal =
    a.size +
        b.size +
        c.size
and yes, one could say ” just put it on one line and be done with it!” - again the example is simplified and the real names are longer so one line is not enough. If this is a potential bug, should I create a ticket?
v

Vampire

11/02/2021, 11:34 AM
Yep, as I said, the parentheses just confused me to think it might be the old IntelliJ Kotlin formatter settings, but it isn't as it is not a function call. And yes, as I said, imho this might be a bug in the default Kotlin format settings in IntelliJ and you should open an issue for it.
6 Views