Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

ktlint default settings disagree with IntelliJ IDEA Ultimate Edition on continuation indentation level (8 vs 12) #805

Closed
christianhujer opened this issue Jun 29, 2020 · 17 comments

Comments

@christianhujer
Copy link

christianhujer commented Jun 29, 2020

The bug you're experiencing might have already be reported!
Please search in the issues before creating one.

⇒ Maybe duplicate of #558

Expected Behavior

Without any further configuration, ktlint expects the same formatting as applied by IntelliJ IDEA Ultimate Edition, because the latter is from JetBrains, the creators of Kotlin.
This includes the behavior about indentation.

IntelliJ IDEA uses 4 spaces for block indentation and 8 spaces for continuation indentation.

The following code snippet was formatted by IntelliJ IDEA Ultimate Edition 2020.1.2 without any modification of the code style settings.

    @Test
    fun neighbors() = assertEquals(
            setOf(P(4, 4), P(4, 5), P(4, 6), P(5, 4), P(5, 6), P(6, 4), P(6, 5), P(6, 6)),
            P(5, 5).neighbors().toSet()
    )

The expected behavior is that ktlint accepts this code snipped as correctly formatted.

Observed Behavior

ktlint misbehaves regarding continuation indentation.
Although continuation indentation should be 8 spaces, ktlint expects continuation with 4 spaces instead.
Above example snippet triggers ktlint to report Unexpected indentation (12) (should be 8).

Steps to Reproduce

https://github.com/nelkinda/gameoflife-kotlin/tree/ktlint-805
Run ./gradlew build

Your Environment

  • Version of ktlint used: 0.37.2 via plugin org.jlleitschuh.gradle.ktlint version 9.2.1
  • Name and version (or code for custom task) of integration used (Gradle plugin, Maven plugin, command line, custom Gradle task):
  • Version of Gradle used (if applicable): 6.5
  • Operating System and version: Kubuntu 20.04
  • Link to your project (if it's a public repository): https://github.com/nelkinda/gameoflife-kotlin
@christianhujer christianhujer changed the title ktlint default settings disagree with IntelliJ IDEA Ultimate Edition ktlint default settings disagree with IntelliJ IDEA Ultimate Edition on continuation indentation level (8 vs 12) Jun 29, 2020
@romtsn
Copy link
Collaborator

romtsn commented Jun 29, 2020

As per Kotlin styleguide itself, this should be indented with 4 spaces (nothing about continuation though). Imho if it's not implemented on IDEA side, it doesn't necessarily mean it's correct, it might just mean "not yet".

@christianhujer
Copy link
Author

Oh, disagreement within JetBrains - unexpected.

@Tapchicoma
Copy link
Collaborator

Android style says about continuation that it should be 4: https://developer.android.com/kotlin/style-guide#block_tags

As a kind-of-workaround you could add following to project .editorconfig file:

[*.kt,*.kts]
continuation_indent_size=4

This should be picked up by IDEA kotlin style.

@romtsn
Copy link
Collaborator

romtsn commented Aug 14, 2020

Filed an issue in Kotlin to get some clarity from JB https://youtrack.jetbrains.com/issue/KT-41072

@christianhujer
Copy link
Author

I've seen that #805 remains open and #558 is closed. Shouldn't it be the other way round? I think that they are so much related that they can be seen as duplicates of each other, and #558 was first.

@romtsn
Copy link
Collaborator

romtsn commented Aug 17, 2020

There are multiple other comments re. the completely unrelated issue, so I've decided to track it here since it's solely focusing on the original topic + I've already posted here the reference to JB's tracker

@ZacSweers
Copy link

This sort of folds into #816 as well really. I think it's worth consolidating to there

@nokite
Copy link

nokite commented Sep 14, 2020

I have a similar issue when a method is called within an "if" with multi-line parameters. It's definitely looks like a bug in ktlint. This code looks fine to me (and to Android Studio).

    private fun trySomething() { // 4 chars
        if (Something.doSomething( // 8 chars
                this, // 16 chars - fails (expected 12, actual 16)
                SOMETHING // 16 chars - fails (expected 12, actual 16)
            ) // 12 chars - fails (expected 8, actual 12)
        ) {  // 8 chars
            val intent = something // 12 chars
            startActivityForResult(intent, something) // 12 chars
        }
    }

P.S. I started seeing multiple issues after upgrading ktlint from 0.31.0 to 0.38.1. I have manually verified that the settings of IntelliJ (Android studio) match the ktlint Readme. Deleting my .editorconfig file, and running "ktlint --android applyToIDEAProject" does not solve this issue.

@gigaSproule
Copy link

I'm getting an issue with this:

                    |"date_modified":"${
                        expectedSalesPersonEntity.dateModified.format(formatter)
                            .replaceFirst("[0]*$", "Z")
                            .replaceFirst("\\.Z", ".0Z")
                    }",

The errors I'm getting are Unexpected indentation (24) (should be 20) and Unexpected indentation (28) (should be 24)

I understand that there is an open ticket with Jetbrains to make it more explicit what the behaviour should be, but according to the code style:

When wrapping chained calls, put the . character or the ?. operator on the next line, with a single indent

I read that as being continuation indent should be a singular indent (4 spaces) and if there are any characters (e.g. . or .?) they should also be included on the new line. As Intellij follows that logic, I would expect ktlint to also follow that logic.

@christianhujer
Copy link
Author

The latest version of IntelliJ IDEA (2020.3) has an explicit setting on the page EditorCode StyleKotlin to Set from… where one can select Kotlin style guide. Interestingly, while this changes lots of stuff, the setting for Tabs and Indents, Continuation indent seems unaffected by this. It still defaults to 8, and when choosing Kotlin style guide, it remains at 8. When setting it to 4, and choosing Kotlin style guide again, it remains at 4.

@boxleytw
Copy link

boxleytw commented Jan 24, 2021

This issue has been relevant to me for a long while. I recommend ktlint to all Kotlin dev teams I advice at my company and at clients, and this crops up when using IntelliJ. The example I point to most:

    // Formatted by ktlint
    override fun equals(other: Any?) = this === other ||
        other is Chocolate &&
        beans == other.beans

vs

    // Formatted by IntelliJ
    override fun equals(other: Any?) = this === other ||
            other is Chocolate &&
            beans == other.beans

That JetBrains has not changed this for the life of their Kotlin plugin, nor is an option provided in IntelliJ, leads me to believe that--documented or not--8 spaces is the correct continuation indent. As remarked in Indent size for 'Method formatting' in plugin differs from official style guide:

It seems that contiuation_indent_size is set to 8 by default in the Kotlin plugin

At the moment, we consider this to be the correct behavior.

And there seems to be some JB activity to correct an issue with .editorconfig.

Could ktlint be updated to use 8 spaces for continuation indents?

@ZacSweers
Copy link

8 space continuation indent is excessive, please don't.

@boxleytw
Copy link

@ZacSweers I agree aesthetically with you on 8 spaces. However, my interest is in being "standard". I'd like ktllint to default out-of-the-box to whatever is standard Kotlin formatting. To address our own tastes, a configuration to adjust to 4 spaces would be a nice addition.

@JGulbronson
Copy link

Would that mean adding back support for continuation indent size which was removed here? #171

@qlihdunn
Copy link

qlihdunn commented Mar 4, 2021

Apparently the Google Kotlin Style Guide for continuation indent has changed. Thus it is a migration strategy. We need to keep the old 8 space(2*4 space tab) continuation intent for a base line and merge slowly to 4 space continuation. Ktlint should have a settable continuation_indent configuration property that can be set in .editorconfig.

@boxleytw
Copy link

boxleytw commented Mar 5, 2021 via email

splix added a commit to emeraldpay/dshackle that referenced this issue Oct 21, 2021
vlsi added a commit to vlsi/jmeter that referenced this issue Nov 16, 2021
It looks like there's an open IDEA issue regarding the use of continuation indent size.
Kotlin official style guide recomments using *single* indent for continuations,
however, IDEA wants to use double indent for some reason.

So far ij_kotlin_continuation_indent_size = 4 workaround seems to be sufficient
to make IDEA and ktlint format in the same way.

https://youtrack.jetbrains.com/issue/KTIJ-17907
pinterest/ktlint#805
vlsi added a commit to vlsi/jmeter that referenced this issue Nov 17, 2021
It looks like there's an open IDEA issue regarding the use of continuation indent size.
Kotlin official style guide recomments using *single* indent for continuations,
however, IDEA wants to use double indent for some reason.

So far ij_kotlin_continuation_indent_size = 4 workaround seems to be sufficient
to make IDEA and ktlint format in the same way.

https://youtrack.jetbrains.com/issue/KTIJ-17907
pinterest/ktlint#805
@romtsn
Copy link
Collaborator

romtsn commented Dec 1, 2021

Let's track the general continuation indent support in #816. As for the original report, I see there's some movement in https://youtrack.jetbrains.com/issue/KTIJ-17907, hopefully, the official styleguide will become a default one for editorconfig and bring less confusion

@romtsn romtsn closed this as completed Dec 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants