From 0acbc72f4ba951225cf90b001b1a122f7cd1e982 Mon Sep 17 00:00:00 2001 From: Paul Dingemans Date: Sat, 11 Dec 2021 18:01:24 +0100 Subject: [PATCH] Fix indentation of delegated super type call entry Forces the BY keyword in a super type call entry to be indented when preceded by a new line Closes #1210 --- CHANGELOG.md | 1 + .../ruleset/standard/IndentationRule.kt | 5 +- .../ruleset/standard/IndentationRuleTest.kt | 57 +++++++++++++++---- 3 files changed, 51 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7139e7ca4d..750a6a33d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ This project adheres to [Semantic Versioning](https://semver.org/). ### Fixed - Fix false positive in rule spacing-between-declarations-with-annotations ([#1281](https://github.com/pinterest/ktlint/issues/1281)) - Fix NoSuchElementException for property accessor (`trailing-comma`) ([#1280](https://github.com/pinterest/ktlint/issues/1280)) +- Fix indent of delegated super type entry (`indent`) ([#1210](https://github.com/pinterest/ktlint/issues/1210)) ### Changed - Update Kotlin version to `1.6.0` release diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/IndentationRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/IndentationRule.kt index b50dbf294d..69486ba666 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/IndentationRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/IndentationRule.kt @@ -955,13 +955,14 @@ class IndentationRule : Rule("indent"), Rule.Modifier.RestrictToRootLast { // instead of expected // val i: Int // by lazy { 1 } - nextLeafElementType == BY_KEYWORD -> - if (node.isPartOf(DELEGATED_SUPER_TYPE_ENTRY)) { + nextLeafElementType == BY_KEYWORD -> { + if (node.isPartOf(DELEGATED_SUPER_TYPE_ENTRY) && node.text != "\n") { 0 } else { expectedIndent++ 1 } + } // IDEA quirk: // var value: DataClass = // DataClass("too long line") diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/IndentationRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/IndentationRuleTest.kt index d42d1036ca..c04f545e32 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/IndentationRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/IndentationRuleTest.kt @@ -952,23 +952,42 @@ internal class IndentationRuleTest { } @Test - fun `lint delegation 2`() { - assertThat( - IndentationRule().lint( - """ - interface Foo + fun `lint and format delegation 2`() { + val code = + """ + interface Foo - class Bar(a: Int, b: Int, c: Int) : Foo + class Bar(a: Int, b: Int, c: Int) : Foo - class Test2 : Foo + class Test2 : Foo + by Bar( + a = 1, + b = 2, + c = 3 + ) + """.trimIndent() + val formattedCode = + """ + interface Foo + + class Bar(a: Int, b: Int, c: Int) : Foo + + class Test2 : Foo by Bar( a = 1, b = 2, c = 3 ) - """.trimIndent() - ) - ).isEmpty() + """.trimIndent() + + assertThat(IndentationRule().lint(code)).containsExactly( + LintError(6, 1, "indent", "Unexpected indentation (0) (should be 4)"), + LintError(7, 1, "indent", "Unexpected indentation (4) (should be 8)"), + LintError(8, 1, "indent", "Unexpected indentation (4) (should be 8)"), + LintError(9, 1, "indent", "Unexpected indentation (4) (should be 8)"), + LintError(10, 1, "indent", "Unexpected indentation (0) (should be 4)"), + ) + assertThat(IndentationRule().format(code)).isEqualTo(formattedCode) } @Test @@ -1343,6 +1362,24 @@ internal class IndentationRuleTest { assertThat(IndentationRule().format(codeTabs, INDENT_STYLE_TABS)).isEqualTo(codeTabs) } + @Test + fun `Issue 1210 - format by`() { + val code = + """ + object ApplicationComponentFactory : ApplicationComponent.Factory + by DaggerApplicationComponent.factory() + """.trimIndent() + val formattedCode = + """ + object ApplicationComponentFactory : ApplicationComponent.Factory + by DaggerApplicationComponent.factory() + """.trimIndent() +// assertThat(IndentationRule().lint(code)).containsExactly( +// LintError(2, 1, "indent", "Unexpected indentation (0) (should be 4)") +// ) + assertThat(IndentationRule().format(code)).isEqualTo(formattedCode) + } + private companion object { const val MULTILINE_STRING_QUOTE = "${'"'}${'"'}${'"'}" const val TAB = "${'\t'}"