Fix ReplaceWithOperatorAssignmentIntention in some cases

#KT-7831 Fixed
This commit is contained in:
Pavel V. Talanov
2015-06-01 15:36:54 +03:00
parent 1334f0352b
commit d531641459
5 changed files with 30 additions and 8 deletions
@@ -17,6 +17,7 @@
package org.jetbrains.kotlin.idea.intentions
import com.intellij.openapi.editor.Editor
import com.intellij.psi.tree.IElementType
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.analyze
@@ -49,17 +50,20 @@ public class ReplaceWithOperatorAssignmentIntention : JetSelfTargetingOffsetInde
val operationToken = expression.getOperationToken()
setText("Replace with ${expression.getOperationReference().getText()}= expression")
val expressionLeft = expression.getLeft()
val expressionRight = expression.getRight()
return when {
variableExpression.matches(expression.getLeft()) -> {
operationToken == JetTokens.PLUS || operationToken == JetTokens.MINUS || operationToken == JetTokens.MUL || operationToken == JetTokens.DIV || operationToken == JetTokens.PERC
variableExpression.matches(expressionLeft) -> {
isArithmeticOperation(operationToken)
}
variableExpression.matches(expression.getRight()) -> {
isPrimitiveOperation && (operationToken == JetTokens.PLUS || operationToken == JetTokens.MUL)
variableExpression.matches(expressionRight) -> {
isPrimitiveOperation && isCommutative(operationToken)
}
expression.getLeft() is JetBinaryExpression -> {
isPrimitiveOperation && checkExpressionRepeat(variableExpression, expression.getLeft() as JetBinaryExpression)
expressionLeft is JetBinaryExpression -> {
val sameCommutativeOperation = expressionLeft.getOperationToken() == operationToken && isCommutative(operationToken)
isPrimitiveOperation && sameCommutativeOperation && checkExpressionRepeat(variableExpression, expressionLeft)
}
else -> {
@@ -68,6 +72,13 @@ public class ReplaceWithOperatorAssignmentIntention : JetSelfTargetingOffsetInde
}
}
private fun isCommutative(operationToken: IElementType) = operationToken == JetTokens.PLUS || operationToken == JetTokens.MUL
private fun isArithmeticOperation(operationToken: IElementType) = operationToken == JetTokens.PLUS ||
operationToken == JetTokens.MINUS ||
operationToken == JetTokens.MUL ||
operationToken == JetTokens.DIV ||
operationToken == JetTokens.PERC
override fun applyTo(element: JetBinaryExpression, editor: Editor) {
val replacement = buildOperatorAssignmentText(
element.getLeft() as JetSimpleNameExpression,
@@ -1,5 +1,5 @@
// IS_APPLICABLE: false
fun foo() {
var x = 0
x = x / 1 + 1
x =<caret> x / 1 + 1
}
@@ -2,5 +2,5 @@
fun foo() {
var x = 0
val y = 0
x = y / x + 0
x =<caret> y / x + 0
}
@@ -0,0 +1,5 @@
// IS_APPLICABLE: false
fun foo() {
var x = 0
x =<caret> x - 1 - 1
}
@@ -6059,6 +6059,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("nonCommutativeRepeat.kt")
public void testNonCommutativeRepeat() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/replaceWithOperatorAssignment/nonCommutativeRepeat.kt");
doTest(fileName);
}
@TestMetadata("nonRepeatingAssignment.kt")
public void testNonRepeatingAssignment() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/replaceWithOperatorAssignment/nonRepeatingAssignment.kt");