diff options
Diffstat (limited to 'ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/SpacingAroundOperatorsRule.kt')
-rw-r--r-- | ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/SpacingAroundOperatorsRule.kt | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/SpacingAroundOperatorsRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/SpacingAroundOperatorsRule.kt index 4bc9448a..06dd72ee 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/SpacingAroundOperatorsRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/SpacingAroundOperatorsRule.kt @@ -36,24 +36,36 @@ import org.jetbrains.kotlin.psi.KtSuperExpression import org.jetbrains.kotlin.psi.KtTypeArgumentList import org.jetbrains.kotlin.psi.KtTypeParameterList import org.jetbrains.kotlin.psi.KtValueArgument +import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType +import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes class SpacingAroundOperatorsRule : Rule("op-spacing") { private val tokenSet = TokenSet.create(MUL, PLUS, MINUS, DIV, PERC, LT, GT, LTEQ, GTEQ, EQEQEQ, EXCLEQEQEQ, EQEQ, EXCLEQ, ANDAND, OROR, ELVIS, EQ, MULTEQ, DIVEQ, PERCEQ, PLUSEQ, MINUSEQ, ARROW) - override fun visit(node: ASTNode, autoCorrect: Boolean, - emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) { + override fun visit( + node: ASTNode, + autoCorrect: Boolean, + emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit + ) { if (tokenSet.contains(node.elementType) && node is LeafPsiElement && !node.isPartOf(KtPrefixExpression::class) && // not unary - !node.isPartOf(KtTypeParameterList::class) && // fun <T>fn(): T {} !node.isPartOf(KtTypeArgumentList::class) && // C<T> - !node.isPartOf(KtValueArgument::class) && // fn(*array) + !(node.elementType == MUL && node.isPartOf(KtValueArgument::class)) && // fn(*array) !node.isPartOf(KtImportDirective::class) && // import * !node.isPartOf(KtSuperExpression::class) // super<T> ) { - val spacingBefore = PsiTreeUtil.prevLeaf(node, true) is PsiWhiteSpace - val spacingAfter = PsiTreeUtil.nextLeaf(node, true) is PsiWhiteSpace + if ((node.elementType == GT || node.elementType == LT) && + // fun <T>fn(): T {} + node.getNonStrictParentOfType(KtTypeParameterList::class.java)?.parent?.node?.elementType != + KtStubElementTypes.FUNCTION) { + return + } + val spacingBefore = PsiTreeUtil.prevLeaf(node, true) is PsiWhiteSpace || + node.elementType == GT + val spacingAfter = PsiTreeUtil.nextLeaf(node, true) is PsiWhiteSpace || + node.elementType == LT when { !spacingBefore && !spacingAfter -> { emit(node.startOffset, "Missing spacing around \"${node.text}\"", true) |