The bug you're experiencing might have already be reported!
Please search in the issues before creating one.
⇒ Maybe duplicate of #558
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.
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).
https://github.com/nelkinda/gameoflife-kotlin/tree/ktlint-805
Run ./gradlew build
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".
Oh, disagreement within JetBrains - unexpected.
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.
Filed an issue in Kotlin to get some clarity from JB https://youtrack.jetbrains.com/issue/KT-41072
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.
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
This sort of folds into #816 as well really. I think it's worth consolidating to there
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.
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.
The latest version of IntelliJ IDEA (2020.3) has an explicit setting on the page _Editor_ ⇒ _Code Style_ ⇒ _Kotlin_ 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.
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?
8 space continuation indent is excessive, please don't.
@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.
Would that mean adding back support for continuation indent size which was removed here? https://github.com/pinterest/ktlint/issues/171
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.
This confuses me. JetBrains has provided a definition for standard Kotlin
style. Perhaps this kind of indentation is not covered? Without other
reasons, I follow that.
Brian Oxley
Principal Consultant
Email [email protected]
Telephone +6 <+1+713+231+3265>5 8028 2709
[image: ThoughtWorks]
http://www.thoughtworks.com/?utm_campaign=brian-oxley-signature&utm_medium=email&utm_source=thoughtworks-email-signature-generator
On Fri, Mar 5, 2021 at 4:59 AM qlihdunn notifications@github.com wrote:
Apparently the Google Kotlin Style Guide
https://developer.android.com/kotlin/style-guide#indentation 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.—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/pinterest/ktlint/issues/805#issuecomment-790937367,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AIOR2SXCJ2QIGUCAOAECS6LTB7YD3ANCNFSM4OK746IQ
.
Most helpful comment
@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.