SimplifyNegatedBinaryExpressionIntention - no i18n + code simplification

This commit is contained in:
Valentin Kipyatkov
2015-05-12 14:56:05 +03:00
parent 0422cf402d
commit fc3609d403
2 changed files with 21 additions and 36 deletions
@@ -77,8 +77,6 @@ remove.parameter=Remove parameter ''{0}''
change.signature.family=Change signature of function/constructor change.signature.family=Change signature of function/constructor
cast.expression.to.type=Cast expression ''{0}'' to ''{1}'' cast.expression.to.type=Cast expression ''{0}'' to ''{1}''
cast.expression.family=Cast Expression 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.skipped.defaults=Cannot skip default arguments.
replace.call.error.undefined.returntype=Unable to determine the return type. replace.call.error.undefined.returntype=Unable to determine the return type.
@@ -17,19 +17,14 @@
package org.jetbrains.kotlin.idea.intentions package org.jetbrains.kotlin.idea.intentions
import com.intellij.openapi.editor.Editor 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.JetSingleValueToken
import org.jetbrains.kotlin.lexer.JetToken
import org.jetbrains.kotlin.lexer.JetTokens import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.*
public class SimplifyNegatedBinaryExpressionIntention : JetSelfTargetingOffsetIndependentIntention<JetPrefixExpression>("simplify.negated.binary.expression", javaClass()) { public class SimplifyNegatedBinaryExpressionIntention : JetSelfTargetingOffsetIndependentIntention<JetPrefixExpression>(javaClass(), "Simplify negated binary expression") {
private fun JetPrefixExpression.unparenthesize(): JetExpression? { private fun IElementType.negate(): JetSingleValueToken? = when (this) {
return (this.getBaseExpression() as? JetParenthesizedExpression)?.getExpression()
}
public fun JetToken.negate(): JetSingleValueToken? = when (this) {
JetTokens.IN_KEYWORD -> JetTokens.NOT_IN JetTokens.IN_KEYWORD -> JetTokens.NOT_IN
JetTokens.NOT_IN -> JetTokens.IN_KEYWORD JetTokens.NOT_IN -> JetTokens.IN_KEYWORD
@@ -49,15 +44,19 @@ public class SimplifyNegatedBinaryExpressionIntention : JetSelfTargetingOffsetIn
} }
override fun isApplicableTo(element: JetPrefixExpression): Boolean { 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 val expression = JetPsiUtil.deparenthesize(element.getBaseExpression()) as? JetOperationExpression ?: return false
if (!(expression is JetIsExpression || expression is JetBinaryExpression)) 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 operation = expression.getOperationReference().getReferencedNameElementType() as? JetSingleValueToken ?: return false
val negOperation = operation.negate() ?: 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 return true
} }
@@ -66,27 +65,15 @@ public class SimplifyNegatedBinaryExpressionIntention : JetSelfTargetingOffsetIn
} }
public fun applyTo(element: JetPrefixExpression) { public fun applyTo(element: JetPrefixExpression) {
// Guaranteed to succeed (by isApplicableTo) val expression = JetPsiUtil.deparenthesize(element.getBaseExpression())!!
val expression = element.unparenthesize()!! val operation = (expression as JetOperationExpression).getOperationReference().getReferencedNameElementType().negate()!!.getValue()
val invertedOperation = JetPsiUtil.getOperationToken(expression as JetOperationExpression)!!.negate()!!
val psiFactory = JetPsiFactory(expression) val psiFactory = JetPsiFactory(expression)
element.replace( val newExpression = when (expression) {
when (expression) { is JetIsExpression -> psiFactory.createExpressionByPattern("$0 $1 $2", expression.getLeftHandSide(), operation, expression.getTypeReference()!!)
is JetIsExpression -> { is JetBinaryExpression -> psiFactory.createExpressionByPattern("$0 $1 $2", expression.getLeft()!!, operation, expression.getRight()!!)
psiFactory.createExpression( else -> throw IllegalArgumentException()
"${expression.getLeftHandSide().getText() ?: ""} ${invertedOperation.getValue()} ${expression.getTypeReference()?.getText() ?: ""}" }
) element.replace(newExpression)
}
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()}"
)
}
)
} }
} }