diff options
Diffstat (limited to 'ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/SpacingAroundCommaRule.kt')
-rw-r--r-- | ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/SpacingAroundCommaRule.kt | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/SpacingAroundCommaRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/SpacingAroundCommaRule.kt index 2ebca751..2be2935a 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/SpacingAroundCommaRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/SpacingAroundCommaRule.kt @@ -6,16 +6,28 @@ 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.impl.source.tree.PsiWhiteSpaceImpl import org.jetbrains.kotlin.com.intellij.psi.util.PsiTreeUtil +import org.jetbrains.kotlin.psi.psiUtil.startOffset class SpacingAroundCommaRule : Rule("comma-spacing") { - override fun visit(node: ASTNode, autoCorrect: Boolean, - emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) { - if (node is LeafPsiElement && node.textMatches(",") && !node.isPartOfString() && - PsiTreeUtil.nextLeaf(node) !is PsiWhiteSpace) { - emit(node.startOffset + 1, "Missing spacing after \"${node.text}\"", true) - if (autoCorrect) { - node.rawInsertAfterMe(PsiWhiteSpaceImpl(" ")) + override fun visit( + node: ASTNode, + autoCorrect: Boolean, + emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit + ) { + if (node is LeafPsiElement && node.textMatches(",") && !node.isPartOfString()) { + val prevLeaf = PsiTreeUtil.prevLeaf(node, true) + if (prevLeaf is PsiWhiteSpace) { + emit(prevLeaf.startOffset, "Unexpected spacing before \"${node.text}\"", true) + if (autoCorrect) { + prevLeaf.node.treeParent.removeChild(prevLeaf.node) + } + } + if (PsiTreeUtil.nextLeaf(node) !is PsiWhiteSpace) { + emit(node.startOffset + 1, "Missing spacing after \"${node.text}\"", true) + if (autoCorrect) { + node.rawInsertAfterMe(PsiWhiteSpaceImpl(" ")) + } } } } |