Skip to content

Commit

Permalink
Fixed if/else without {} formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
shyiko committed Oct 10, 2017
1 parent 916f9d7 commit 30a476b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,17 @@ class SpacingAroundKeywordRule : Rule("keyword-spacing") {
if (noLFBeforeSet.contains(node.elementType)) {
val prevLeaf = PsiTreeUtil.prevLeaf(node)
if (prevLeaf is PsiWhiteSpaceImpl && prevLeaf.textContains('\n') &&
(node.elementType != ELSE_KEYWORD || node.parent !is KtWhenEntry) &&
(PsiTreeUtil.prevLeaf(prevLeaf)?.textMatches("}") ?: false)) {
emit(node.startOffset, "Unexpected newline before \"${node.text}\"", true)
if (autoCorrect) {
prevLeaf.rawReplaceWithText(" ")
(node.elementType != ELSE_KEYWORD || node.parent !is KtWhenEntry)) {
val presumablyCurly = PsiTreeUtil.prevLeaf(prevLeaf)
if (presumablyCurly != null &&
presumablyCurly.node.elementType == KtTokens.RBRACE &&
(node.elementType != ELSE_KEYWORD ||
// `if (...) v.let { } else` case
presumablyCurly.node.treeParent?.treeParent?.treeParent == node.treeParent)) {
emit(node.startOffset, "Unexpected newline before \"${node.text}\"", true)
if (autoCorrect) {
prevLeaf.rawReplaceWithText(" ")
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ class SpacingAroundKeywordRuleTest {
1 -> println("")
else -> println("")
}
if (V)
V.let { "" }
else
V
}
""".trimIndent()
)).isEqualTo(listOf(
Expand Down

0 comments on commit 30a476b

Please sign in to comment.