diff --git a/CHANGELOG.md b/CHANGELOG.md index 675f5ccab1..99a62d2e8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - Fix false positive after `else` keyword (`argument-list-wrapping`) ([#1047](https://github.com/pinterest/ktlint/issues/1047)) - Fix formatting with comments (`colon-spacing`) ([#1057](https://github.com/pinterest/ktlint/issues/1057)) - Fix IndexOutOfBoundsException in `argument-list-wrapping-rule` formatting file with many corrections ([#1081](https://github.com/pinterest/ktlint/issues/1081)) +- Fix formatting in arguments (`multiline-if-else`) ([#1079](https://github.com/pinterest/ktlint/issues/1079)) ### Changed - Update Gradle shadow plugin to `6.1.0` version diff --git a/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/MultiLineIfElseRule.kt b/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/MultiLineIfElseRule.kt index 549c82e7b3..9e11d82dd1 100644 --- a/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/MultiLineIfElseRule.kt +++ b/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/MultiLineIfElseRule.kt @@ -7,7 +7,8 @@ import com.pinterest.ktlint.core.ast.ElementType.LBRACE import com.pinterest.ktlint.core.ast.ElementType.RBRACE import com.pinterest.ktlint.core.ast.ElementType.RPAR import com.pinterest.ktlint.core.ast.ElementType.THEN -import com.pinterest.ktlint.core.ast.isWhiteSpaceWithNewline +import com.pinterest.ktlint.core.ast.isPartOfComment +import com.pinterest.ktlint.core.ast.isWhiteSpaceWithoutNewline import com.pinterest.ktlint.core.ast.prevLeaf import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace @@ -45,7 +46,8 @@ class MultiLineIfElseRule : Rule("multiline-if-else") { private fun autocorrect(node: ASTNode) { val prevLeaves = node.leaves(forward = false).takeWhile { it.elementType !in listOf(RPAR, ELSE_KEYWORD) }.toList().reversed() - val nextLeaves = node.leaves(forward = true).takeWhile { !it.isWhiteSpaceWithNewline() }.toList() + val nextLeaves = + node.leaves(forward = true).takeWhile { it.isWhiteSpaceWithoutNewline() || it.isPartOfComment() }.toList() val rightBraceIndent = node.treeParent .prevLeaf { it is PsiWhiteSpace && it.textContains('\n') }?.text.orEmpty() .let { "\n${it.substringAfterLast("\n")}" } diff --git a/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/MultiLineIfElseRuleTest.kt b/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/MultiLineIfElseRuleTest.kt index 1203490fb2..6ab67b4313 100644 --- a/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/MultiLineIfElseRuleTest.kt +++ b/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/MultiLineIfElseRuleTest.kt @@ -416,6 +416,38 @@ class MultiLineIfElseRuleTest { ) } + // https://github.com/pinterest/ktlint/issues/1079 + @Test + fun testInArgument() { + val actual = format( + """ + fun foo(x: Int, y: Int, z: Int) {} + fun test(a: Int, b: Int, c: Int, d: Int, bar: Boolean) { + foo( + a, + if (bar) b else + c, + d + ) + } + """.trimIndent() + ) + assertThat(actual).isEqualTo( + """ + fun foo(x: Int, y: Int, z: Int) {} + fun test(a: Int, b: Int, c: Int, d: Int, bar: Boolean) { + foo( + a, + if (bar) b else { + c + }, + d + ) + } + """.trimIndent() + ) + } + private fun assertOK(kotlinScript: String) { assertThat(format(kotlinScript)).isEqualTo(kotlinScript) assertThat(lint(kotlinScript)).isEqualTo(emptyList())