forked from pinterest/ktlint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplified no-line-break-after-else logic (pinterest#125)
- Loading branch information
Showing
2 changed files
with
18 additions
and
23 deletions.
There are no files selected for viewing
34 changes: 14 additions & 20 deletions
34
...ard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoLineBreakAfterElseRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,25 @@ | ||
package com.github.shyiko.ktlint.ruleset.standard | ||
|
||
import com.github.shyiko.ktlint.core.Rule | ||
import org.jetbrains.kotlin.KtNodeTypes | ||
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.lexer.KtKeywordToken | ||
import org.jetbrains.kotlin.lexer.KtTokens | ||
|
||
class NoLineBreakAfterElseRule : Rule(RULE_ID) { | ||
override fun visit(node: ASTNode, autoCorrect: Boolean, emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) { | ||
if (node is PsiWhiteSpace) { | ||
if ( | ||
node.nextSibling?.node?.elementType == KtNodeTypes.ELSE | ||
&& node.prevSibling?.node?.elementType is KtKeywordToken | ||
&& node.prevSibling.text == "else" | ||
&& node.getText().contains("\n")) { | ||
emit(node.startOffset + 1, | ||
"Unexpected line break after \"else\"", | ||
true) | ||
if (autoCorrect) { | ||
(node as LeafPsiElement).rawReplaceWithText(" ") | ||
} | ||
class NoLineBreakAfterElseRule : Rule("no-line-break-after-else") { | ||
|
||
override fun visit( | ||
node: ASTNode, | ||
autoCorrect: Boolean, | ||
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(" ") | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
const val RULE_ID = "no-line-break-after-else" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters