-
Notifications
You must be signed in to change notification settings - Fork 509
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enforce spacing around rangeUntil operator
..<
similar to the range…
… operator `..` in `range-spacing` Closes #1858
- Loading branch information
1 parent
7f323be
commit b344c62
Showing
4 changed files
with
47 additions
and
4 deletions.
There are no files selected for viewing
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
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
15 changes: 11 additions & 4 deletions
15
...main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundRangeOperatorRule.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,45 +1,52 @@ | ||
package com.pinterest.ktlint.ruleset.standard.rules | ||
|
||
import com.pinterest.ktlint.rule.engine.core.api.ElementType.RANGE | ||
import com.pinterest.ktlint.rule.engine.core.api.ElementType.RANGE_UNTIL | ||
import com.pinterest.ktlint.rule.engine.core.api.RuleId | ||
import com.pinterest.ktlint.rule.engine.core.api.nextLeaf | ||
import com.pinterest.ktlint.rule.engine.core.api.prevLeaf | ||
import com.pinterest.ktlint.ruleset.standard.StandardRule | ||
import org.jetbrains.kotlin.com.intellij.lang.ASTNode | ||
import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace | ||
import org.jetbrains.kotlin.ir.expressions.IrStatementOrigin | ||
import org.jetbrains.kotlin.lexer.KtSingleValueToken | ||
import org.jetbrains.kotlin.lexer.KtToken | ||
import org.jetbrains.kotlin.lexer.KtTokens | ||
|
||
public class SpacingAroundRangeOperatorRule : StandardRule("range-spacing") { | ||
override fun beforeVisitChildNodes( | ||
node: ASTNode, | ||
autoCorrect: Boolean, | ||
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit, | ||
) { | ||
if (node.elementType == RANGE) { | ||
if (node.elementType == RANGE || node.elementType == RANGE_UNTIL) { | ||
val prevLeaf = node.prevLeaf() | ||
val nextLeaf = node.nextLeaf() | ||
when { | ||
prevLeaf is PsiWhiteSpace && nextLeaf is PsiWhiteSpace -> { | ||
emit(node.startOffset, "Unexpected spacing around \"..\"", true) | ||
emit(node.startOffset, "Unexpected spacing around \"${node.elementTypeDescription()}\"", true) | ||
if (autoCorrect) { | ||
prevLeaf.node.treeParent.removeChild(prevLeaf.node) | ||
nextLeaf.node.treeParent.removeChild(nextLeaf.node) | ||
} | ||
} | ||
prevLeaf is PsiWhiteSpace -> { | ||
emit(prevLeaf.node.startOffset, "Unexpected spacing before \"..\"", true) | ||
emit(prevLeaf.node.startOffset, "Unexpected spacing before \"${node.elementTypeDescription()}\"", true) | ||
if (autoCorrect) { | ||
prevLeaf.node.treeParent.removeChild(prevLeaf.node) | ||
} | ||
} | ||
nextLeaf is PsiWhiteSpace -> { | ||
emit(nextLeaf.node.startOffset, "Unexpected spacing after \"..\"", true) | ||
emit(nextLeaf.node.startOffset, "Unexpected spacing after \"${node.elementTypeDescription()}\"", true) | ||
if (autoCorrect) { | ||
nextLeaf.node.treeParent.removeChild(nextLeaf.node) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun ASTNode.elementTypeDescription() = (elementType as? KtSingleValueToken)?.value ?: elementType | ||
} | ||
|
||
public val SPACING_AROUND_RANGE_OPERATOR_RULE_ID: RuleId = SpacingAroundRangeOperatorRule().ruleId |
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