pinterest / ktlint

An anti-bikeshedding Kotlin linter with built-in formatter
https://pinterest.github.io/ktlint/
MIT License
6.23k stars 507 forks source link

intellij_idea style contradicts IntelliJ formatting for indentaton within `when` #2808

Closed scottkennedy closed 4 weeks ago

scottkennedy commented 1 month ago
fun test(foo: Int) {
    when {
        foo < 0
            -> println("negative") // Unexpected indentation (12) (should be 8) (standard:indent)

        foo == 0
        -> println("zero") // No complaint

        foo > 0
            -> println("positive") // Unexpected indentation (12) (should be 8) (standard:indent)
    }
}

Expected Behavior

<!-- Note: Ktlint should not produce any code which conflict with IntelliJ default code formatting. So please verify that your expectation is accepted by IntelliJ default code formatting. -->

ktlint does not match IntelliJ code formatting. IntelliJ indents the -> another 4 spaces, but ktlint wants it back out to line up with foo

Observed Behavior

See comments in code

Steps to Reproduce

Run ktlint on the included code snippet.

Your Environment

paul-dingemans commented 1 month ago

Sometimes they fix bugs in the default formatting of Intellij IDEA as well. At least up and until 2024.1.6. (build 241.19072.14) the when arrow was not indented.

paul-dingemans commented 1 month ago

Changing the indent will break formatting for all users running on older versions of the IDEA. For now, you can keep ktlint and IDEA formatting compatible by setting .editorconfig property ij_kotlin_indent_before_arrow_on_new_line = false (this property is comparable to the format setting below), but you should prefer to use the .editorconfig property. Screenshot 2024-10-15 at 10 33 48

Note that Ktlint does not recognize this property at this moment. By setting it to false, you enforce IDEA formatting to comply with Ktlint formatting. Not setting the property, or setting it to true results in a mismatch between Ktlint and IDEA formatting.