Skip to content

Commit

Permalink
Fix formatting in arguments (multiline-if-else) (pinterest#1082)
Browse files Browse the repository at this point in the history
  • Loading branch information
t-kameyama authored and romtsn committed Aug 8, 2021
1 parent 221372e commit 9bde113
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")}" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<LintError>())
Expand Down

0 comments on commit 9bde113

Please sign in to comment.