Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

intellij_idea style contradicts IntelliJ formatting for indentaton within when #2808

Closed
scottkennedy opened this issue Sep 19, 2024 · 2 comments · Fixed by #2838
Closed

intellij_idea style contradicts IntelliJ formatting for indentaton within when #2808

scottkennedy opened this issue Sep 19, 2024 · 2 comments · Fixed by #2838

Comments

@scottkennedy
Copy link

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

  • Version of ktlint used: 1.3.1
  • Relevant parts of the .editorconfig settings
ktlint_code_style = intellij_idea
  • Name and version (or code for custom task) of integration used (Gradle plugin, Maven plugin, command line, custom Gradle task): https://github.com/JLLeitschuh/ktlint-gradle 12.1.1
  • Version of Gradle used (if applicable): 8.10.1
  • Operating System and version: MacOS 14.6.1
@paul-dingemans
Copy link
Collaborator

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
Copy link
Collaborator

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.

paul-dingemans added a commit that referenced this issue Oct 19, 2024
Determines whether an arrow in a when-entry should be indented when it starts on a new line. For code styles `intellij_idea` and `android_studio` this property needs to be set to `true` to keep formatting compatible with default IDEA Formatting when using IDEA version `2024.2` or above.

Closes #2808
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants