diff --git a/python/src/com/jetbrains/python/actions/AugmentedAssignmentQuickFix.java b/python/src/com/jetbrains/python/actions/AugmentedAssignmentQuickFix.java index 67585c68366b..bc6874fbf472 100644 --- a/python/src/com/jetbrains/python/actions/AugmentedAssignmentQuickFix.java +++ b/python/src/com/jetbrains/python/actions/AugmentedAssignmentQuickFix.java @@ -40,7 +40,11 @@ public class AugmentedAssignmentQuickFix implements LocalQuickFix { PyBinaryExpression expression = (PyBinaryExpression)statement.getAssignedValue(); PyExpression leftExpression = expression.getLeftExpression(); PyExpression rightExpression = expression.getRightExpression(); - + if (rightExpression.getText().equals(target.getText())) { + PyExpression tmp = rightExpression; + rightExpression = leftExpression; + leftExpression = tmp; + } if (leftExpression != null && (leftExpression instanceof PyReferenceExpression || leftExpression instanceof PySubscriptionExpression)) { if (leftExpression.getText().equals(target.getText())) { @@ -58,22 +62,6 @@ public class AugmentedAssignmentQuickFix implements LocalQuickFix { } } } - else if (rightExpression != null && - (rightExpression instanceof PyReferenceExpression || rightExpression instanceof PySubscriptionExpression)) { - if (rightExpression.getText().equals(target.getText())) { - if (leftExpression instanceof PyNumericLiteralExpression || - leftExpression instanceof PyStringLiteralExpression) { - - PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project); - StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.append(target.getText()).append(" "). - append(expression.getPsiOperator().getText()).append("= ").append(leftExpression.getText()); - PyAugAssignmentStatementImpl augAssignment = elementGenerator.createFromText(LanguageLevel.getDefault(), - PyAugAssignmentStatementImpl.class, stringBuilder.toString()); - statement.replace(augAssignment); - } - } - } } } } diff --git a/python/src/com/jetbrains/python/inspections/PyAugmentAssignmentInspection.java b/python/src/com/jetbrains/python/inspections/PyAugmentAssignmentInspection.java index 90239b8cd149..99f920a8aea2 100644 --- a/python/src/com/jetbrains/python/inspections/PyAugmentAssignmentInspection.java +++ b/python/src/com/jetbrains/python/inspections/PyAugmentAssignmentInspection.java @@ -6,6 +6,7 @@ import com.jetbrains.python.PyBundle; import com.jetbrains.python.PyTokenTypes; import com.jetbrains.python.actions.AugmentedAssignmentQuickFix; import com.jetbrains.python.psi.*; +import com.jetbrains.python.psi.types.PyType; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NotNull; @@ -41,6 +42,11 @@ public class PyAugmentAssignmentInspection extends PyInspection { PyBinaryExpression expression = (PyBinaryExpression)node.getAssignedValue(); PyExpression leftExpression = expression.getLeftExpression(); PyExpression rightExpression = expression.getRightExpression(); + if (rightExpression.getText().equals(target.getText())) { + PyExpression tmp = rightExpression; + rightExpression = leftExpression; + leftExpression = tmp; + } if (PyTokenTypes.ADDITIVE_OPERATIONS.contains(expression.getOperator()) || PyTokenTypes.MULTIPLICATIVE_OPERATIONS.contains(expression.getOperator())) { if (leftExpression != null @@ -54,17 +60,11 @@ public class PyAugmentAssignmentInspection extends PyInspection { registerProblem(node, "Assignment can be replaced with augmented assignment", quickFix); } } - } - } - else if (rightExpression != null && - (rightExpression instanceof PyReferenceExpression || rightExpression instanceof PySubscriptionExpression)) { - if (rightExpression.getText().equals(target.getText())) { - if (leftExpression instanceof PyNumericLiteralExpression) { - PyElementType op = expression.getOperator(); - if (op == PyTokenTypes.PLUS || op == PyTokenTypes.MINUS || - op == PyTokenTypes.MULT || op == PyTokenTypes.DIV) { - AugmentedAssignmentQuickFix quickFix = new AugmentedAssignmentQuickFix(); - registerProblem(node, "Assignment can be replaced with augmented assignment", quickFix); + else { + PyType type = rightExpression.getType(myTypeEvalContext); + if (type != null) { + if (type.getName().equals("int") || type.getName().equals("str")) + registerProblem(node, "Assignment can be replaced with augmented assignment", new AugmentedAssignmentQuickFix()); } } } diff --git a/python/testData/inspections/PyAugmentAssignmentInspection/test.py b/python/testData/inspections/PyAugmentAssignmentInspection/test.py index c36b6ef02c98..2859ece5a4bc 100644 --- a/python/testData/inspections/PyAugmentAssignmentInspection/test.py +++ b/python/testData/inspections/PyAugmentAssignmentInspection/test.py @@ -3,12 +3,12 @@ str = "string" -var_2 = var_2 + str +var_2 = var_2 + str var_3 = var_3 var_4 = 1 -var_5 = var_5 + "string" -var_6 = var_6 + var_4 +var_5 = var_5 + "string" +var_6 = var_6 + var_4 var_7 += 2