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

Vampire

11/23/2023, 9:57 PM
Is there an official (documented) stance on how to format continuation lines on wrapped expressions? I don't find anything in the official coding conventions page. The only thing I found there is one wrapped expression but it is within an
if
condition and that is a special case that is also treated differently in the IDE.
ktlint
seems to use 4 spaces in that case. But IntelliJ by default uses 8 spaces as continuation indent. On the other hand, using "Set from ... -> Kotlin style guide" does not change the continuation indent value at all.
c

CLOVIS

11/24/2023, 8:50 AM
The IntelliJ defaults are the "old" code style, it's kept as a default to avoid breaking old projects. The recommended config is the "new" code style.
v

Vampire

11/24/2023, 11:51 AM
Thanks, but that doesn't answer my question at all. I neither found an official recommendation for the continuation indent for example when wrapping expressions. Neither does it get configured to any value when slecting "Set from ... -> Kotlin style guide" which is the new one.
c

CLOVIS

11/24/2023, 12:26 PM
The current coding style is to use a normal indent for continuation:
Copy code
val foo =
    if (...) ...
    else ...
v

Vampire

11/24/2023, 12:37 PM
I'm specifically asking for wrapped expressions, for example
Copy code
val foo = !true &&
        !true
vs.
Copy code
val foo = !true &&
    !true
or
Copy code
.filter { line ->
    (line == "a") ||
            (line == "b")
}
vs.
Copy code
.filter { line ->
    (line == "a") ||
        (line == "b")
}
And my main question is not how the convention is, but where it is documented. I do not find this case documented on the conventions page.
c

CLOVIS

11/24/2023, 1:08 PM
I don't think it's explicitly written anywhere, but you can find a lot of examples in the style guide that demonstrate that it must be 4 spaces:
Copy code
class Person(
    id: Int,                     // HERE
    name: String,
    surname: String
) : Human(id, name),
    KotlinMaker { /*...*/ }      // HERE
Copy code
fun longMethodName(
    argument: ArgumentType = defaultValue,    // HERE
    argument2: AnotherArgumentType,
): ReturnType {
    // body
}
Copy code
fun f(x: String, y: String, z: String) =
    veryLongFunctionCallWithManyWords(andLongParametersToo(), x, y, z)        // HERE
Copy code
if (!component.isSyncing &&
    !hasAnyKotlinRuntimeInScope(module)       // HERE
) {
    return createKotlinNotConfiguredPanel(module)
}
Copy code
private fun parsePropertyValue(propName: String, token: Token) {
    when (token) {
        is Token.ValueToken ->
            callback.visitValue(propName, token.value)                        // HERE

        Token.LBRACE -> { // ...
        }
    }
}
etc
v

Vampire

11/24/2023, 2:48 PM
All the examples you listed and all I find in the docs do not apply. All those examples have explicit handling that is also not influenced by the "continuation indent" setting. The examples I showed use the "continuation indent" setting and those are in no form shown in the docs page. The closest thing is the
if
condition, but that one has special handling and is also not influenced by the "continuation indent" setting.
c

CLOVIS

11/24/2023, 2:57 PM
Ah, my bad, I was convinced that's what continuation indent did.
v

Vampire

11/24/2023, 3:00 PM
As practically everywhere only one indent (4 spaces) is used, I also guess it indeed is also intended there. But I'm after an official documented stance on it or report a doc bug. And especially as the "import from official style" does not change the continuation indent value which would then maybe an additional code bug. 🙂
👍 1