From fc3609d40321536ad1429eddb03b29bf2443694f Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Tue, 12 May 2015 14:56:05 +0300 Subject: [PATCH] SimplifyNegatedBinaryExpressionIntention - no i18n + code simplification --- .../kotlin/idea/JetBundle.properties | 2 - ...implifyNegatedBinaryExpressionIntention.kt | 55 +++++++------------ 2 files changed, 21 insertions(+), 36 deletions(-) diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/JetBundle.properties b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/JetBundle.properties index e635a72d808..ec1338c6649 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/JetBundle.properties +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/JetBundle.properties @@ -77,8 +77,6 @@ remove.parameter=Remove parameter ''{0}'' change.signature.family=Change signature of function/constructor cast.expression.to.type=Cast expression ''{0}'' to ''{1}'' cast.expression.family=Cast Expression -simplify.negated.binary.expression=Simplify negated ''{0}'' expression to ''{1}'' -simplify.negated.binary.expression.family=Simplify Negated Binary Expression replace.call.error.skipped.defaults=Cannot skip default arguments. replace.call.error.undefined.returntype=Unable to determine the return type. diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/SimplifyNegatedBinaryExpressionIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/SimplifyNegatedBinaryExpressionIntention.kt index e30a4a3029b..5f7784dcace 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/SimplifyNegatedBinaryExpressionIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/SimplifyNegatedBinaryExpressionIntention.kt @@ -17,19 +17,14 @@ package org.jetbrains.kotlin.idea.intentions import com.intellij.openapi.editor.Editor -import org.jetbrains.kotlin.idea.JetBundle +import com.intellij.psi.tree.IElementType import org.jetbrains.kotlin.lexer.JetSingleValueToken -import org.jetbrains.kotlin.lexer.JetToken import org.jetbrains.kotlin.lexer.JetTokens import org.jetbrains.kotlin.psi.* -public class SimplifyNegatedBinaryExpressionIntention : JetSelfTargetingOffsetIndependentIntention("simplify.negated.binary.expression", javaClass()) { +public class SimplifyNegatedBinaryExpressionIntention : JetSelfTargetingOffsetIndependentIntention(javaClass(), "Simplify negated binary expression") { - private fun JetPrefixExpression.unparenthesize(): JetExpression? { - return (this.getBaseExpression() as? JetParenthesizedExpression)?.getExpression() - } - - public fun JetToken.negate(): JetSingleValueToken? = when (this) { + private fun IElementType.negate(): JetSingleValueToken? = when (this) { JetTokens.IN_KEYWORD -> JetTokens.NOT_IN JetTokens.NOT_IN -> JetTokens.IN_KEYWORD @@ -49,15 +44,19 @@ public class SimplifyNegatedBinaryExpressionIntention : JetSelfTargetingOffsetIn } override fun isApplicableTo(element: JetPrefixExpression): Boolean { - if (element.getOperationReference().getReferencedNameElementType() != JetTokens.EXCL) return false + if (element.getOperationToken() != JetTokens.EXCL) return false - val expression = element.unparenthesize() as? JetOperationExpression - if (!(expression is JetIsExpression || expression is JetBinaryExpression)) return false + val expression = JetPsiUtil.deparenthesize(element.getBaseExpression()) as? JetOperationExpression ?: return false + when (expression) { + is JetIsExpression -> { if (expression.getTypeReference() == null) return false } + is JetBinaryExpression -> { if (expression.getLeft() == null || expression.getRight() == null) return false } + else -> return false + } - val operation = (JetPsiUtil.getOperationToken(expression) as? JetSingleValueToken) ?: return false - val negOperation = operation.negate() ?: return false + val operation = expression.getOperationReference().getReferencedNameElementType() as? JetSingleValueToken ?: return false + val negatedOperation = operation.negate() ?: return false - setText(JetBundle.message("simplify.negated.binary.expression", operation.getValue(), negOperation.getValue())) + setText("Simplify negated '${operation.getValue()}' expression to '${negatedOperation.getValue()}'") return true } @@ -66,27 +65,15 @@ public class SimplifyNegatedBinaryExpressionIntention : JetSelfTargetingOffsetIn } public fun applyTo(element: JetPrefixExpression) { - // Guaranteed to succeed (by isApplicableTo) - val expression = element.unparenthesize()!! - val invertedOperation = JetPsiUtil.getOperationToken(expression as JetOperationExpression)!!.negate()!! + val expression = JetPsiUtil.deparenthesize(element.getBaseExpression())!! + val operation = (expression as JetOperationExpression).getOperationReference().getReferencedNameElementType().negate()!!.getValue() val psiFactory = JetPsiFactory(expression) - element.replace( - when (expression) { - is JetIsExpression -> { - psiFactory.createExpression( - "${expression.getLeftHandSide().getText() ?: ""} ${invertedOperation.getValue()} ${expression.getTypeReference()?.getText() ?: ""}" - ) - } - is JetBinaryExpression -> psiFactory.createExpressionByPattern("$0 $1 $2", - expression.getLeft(), - invertedOperation.getValue(), - expression.getRight() - ) - else -> throw IllegalStateException( - "Expression is neither a JetIsExpression or JetBinaryExpression (checked by isApplicableTo): ${expression.getText()}" - ) - } - ) + val newExpression = when (expression) { + is JetIsExpression -> psiFactory.createExpressionByPattern("$0 $1 $2", expression.getLeftHandSide(), operation, expression.getTypeReference()!!) + is JetBinaryExpression -> psiFactory.createExpressionByPattern("$0 $1 $2", expression.getLeft()!!, operation, expression.getRight()!!) + else -> throw IllegalArgumentException() + } + element.replace(newExpression) } } \ No newline at end of file