Skip to content

Commit

Permalink
Relaxed no-line-break-after-else rule to allow multi-line if/else wit…
Browse files Browse the repository at this point in the history
…hout curly braces
  • Loading branch information
shyiko committed Mar 21, 2018
1 parent 8c06614 commit f2747f3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- `+`, `-`, `*`, `/`, `%`, `&&`, `||` wrapping ([#168](https://github.com/shyiko/ktlint/issues/168)).

### Changed
- `comma-spacing` rule to be more strict ([#173](https://github.com/shyiko/ktlint/issues/173)).
- `comma-spacing` rule to be more strict ([#173](https://github.com/shyiko/ktlint/issues/173)).
- `no-line-break-after-else` rule to allow multi-line `if/else` without curly braces.

## [0.19.0] - 2018-03-04

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.github.shyiko.ktlint.core.Rule
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
import org.jetbrains.kotlin.com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.lexer.KtTokens

class NoLineBreakAfterElseRule : Rule("no-line-break-after-else") {
Expand All @@ -14,11 +15,13 @@ class NoLineBreakAfterElseRule : Rule("no-line-break-after-else") {
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
) {
if (node is PsiWhiteSpace &&
node.textContains('\n') &&
node.prevSibling?.node?.elementType == KtTokens.ELSE_KEYWORD) {
emit(node.startOffset + 1, "Unexpected line break after \"else\"", true)
if (autoCorrect) {
(node as LeafPsiElement).rawReplaceWithText(" ")
node.textContains('\n')) {
if (PsiTreeUtil.prevLeaf(node, true)?.node?.elementType == KtTokens.ELSE_KEYWORD &&
PsiTreeUtil.nextLeaf(node, true)?.node?.elementType.let { it == KtTokens.IF_KEYWORD || it == KtTokens.LBRACE }) {
emit(node.startOffset + 1, "Unexpected line break after \"else\"", true)
if (autoCorrect) {
(node as LeafPsiElement).rawReplaceWithText(" ")
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ class NoLineBreakAfterElseRuleTest {
doAnotherThing()
}
""".trimIndent()
)).isEqualTo(listOf(
LintError(5, 1, "no-line-break-after-else", "Unexpected line break after \"else\"")
))
)).isEmpty()
}

@Test
Expand Down

0 comments on commit f2747f3

Please sign in to comment.