From fae5355877e6d82b3fdcb59d4be4d6b7fbdba6c9 Mon Sep 17 00:00:00 2001 From: Sha Sha Chu Date: Tue, 16 Jul 2019 22:07:39 -0700 Subject: [PATCH] Fixing bug with improper indentation for enums with multi-line initializers (#530) Fixes https://github.com/pinterest/ktlint/issues/518 --- .../ktlint/ruleset/experimental/IndentationRule.kt | 5 +++++ .../spec/indent/format-supertype-expected.kt.spec | 12 ++++++++++++ .../resources/spec/indent/format-supertype.kt.spec | 12 ++++++++++++ 3 files changed, 29 insertions(+) diff --git a/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/IndentationRule.kt b/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/IndentationRule.kt index e8be27ee42..297ab4cbcf 100644 --- a/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/IndentationRule.kt +++ b/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/IndentationRule.kt @@ -689,6 +689,11 @@ class IndentationRule : Rule("indent"), Rule.Modifier.RestrictToRootLast { } private fun adjustExpectedIndentInsideSuperTypeCall(n: ASTNode, ctx: IndentContext) { + // Don't adjust indents for initializer lists + if (n.treeParent?.elementType != SUPER_TYPE_LIST) { + return + } + if ( // n.treePrev == null && // n.treeParent.elementType == SUPER_TYPE_LIST && diff --git a/ktlint-ruleset-experimental/src/test/resources/spec/indent/format-supertype-expected.kt.spec b/ktlint-ruleset-experimental/src/test/resources/spec/indent/format-supertype-expected.kt.spec index 87cf77df76..e24c9130cf 100644 --- a/ktlint-ruleset-experimental/src/test/resources/spec/indent/format-supertype-expected.kt.spec +++ b/ktlint-ruleset-experimental/src/test/resources/spec/indent/format-supertype-expected.kt.spec @@ -61,3 +61,15 @@ class AndroidModuleDependency() class AndroidModuleDependency() : ModuleDependency(name, methodToCall, method), ModuleDependency(name, methodToCall, method) + +// https://github.com/pinterest/ktlint/issues/518 +enum class Color(val displayName: String, val value: Int) { + RED( + displayName = "Red", + value = 1 + ), + BLUE( + displayName = "Blue", + value = 2 + ); +} diff --git a/ktlint-ruleset-experimental/src/test/resources/spec/indent/format-supertype.kt.spec b/ktlint-ruleset-experimental/src/test/resources/spec/indent/format-supertype.kt.spec index f743ef6a26..20be56449e 100644 --- a/ktlint-ruleset-experimental/src/test/resources/spec/indent/format-supertype.kt.spec +++ b/ktlint-ruleset-experimental/src/test/resources/spec/indent/format-supertype.kt.spec @@ -56,3 +56,15 @@ class AndroidModuleDependency() class AndroidModuleDependency() : ModuleDependency(name, methodToCall, method), ModuleDependency(name, methodToCall, method) + +// https://github.com/pinterest/ktlint/issues/518 +enum class Color(val displayName: String, val value: Int) { + RED( + displayName = "Red", + value = 1 + ), + BLUE( + displayName = "Blue", + value = 2 + ); +}