From 0f3e4fe39709fcf6314e3d74d278482e6a1bab68 Mon Sep 17 00:00:00 2001 From: Ekaterina Tuzova Date: Wed, 16 Jan 2013 16:54:27 +0400 Subject: [PATCH] fixed PY-7715 replace with augmented assignment doesn't work in case of subscription --- .../quickfix/AugmentedAssignmentQuickFix.java | 59 ++++--------------- .../AugmentAssignmentSubscription.py | 3 + .../AugmentAssignmentSubscription_after.py | 3 + .../com/jetbrains/python/PyQuickFixTest.java | 5 ++ 4 files changed, 22 insertions(+), 48 deletions(-) create mode 100644 python/testData/inspections/AugmentAssignmentSubscription.py create mode 100644 python/testData/inspections/AugmentAssignmentSubscription_after.py diff --git a/python/src/com/jetbrains/python/inspections/quickfix/AugmentedAssignmentQuickFix.java b/python/src/com/jetbrains/python/inspections/quickfix/AugmentedAssignmentQuickFix.java index 919b4d3887f0..88fb994f9d7e 100644 --- a/python/src/com/jetbrains/python/inspections/quickfix/AugmentedAssignmentQuickFix.java +++ b/python/src/com/jetbrains/python/inspections/quickfix/AugmentedAssignmentQuickFix.java @@ -5,14 +5,10 @@ import com.intellij.codeInspection.ProblemDescriptor; import com.intellij.openapi.project.Project; import com.intellij.psi.PsiComment; import com.intellij.psi.PsiElement; -import com.intellij.psi.tree.TokenSet; import com.intellij.psi.util.PsiTreeUtil; import com.jetbrains.python.PyBundle; -import com.jetbrains.python.PyTokenTypes; import com.jetbrains.python.psi.*; import com.jetbrains.python.psi.impl.PyAugAssignmentStatementImpl; -import com.jetbrains.python.psi.types.PyType; -import com.jetbrains.python.psi.types.TypeEvalContext; import org.jetbrains.annotations.NotNull; import java.util.List; @@ -59,54 +55,21 @@ public class AugmentedAssignmentQuickFix implements LocalQuickFix { if ((leftExpression instanceof PyReferenceExpression || leftExpression instanceof PySubscriptionExpression)) { if (leftExpression.getText().equals(targetText)) { - if (rightExpression instanceof PyNumericLiteralExpression || rightExpression instanceof PyStringLiteralExpression || - rightExpression instanceof PyCallExpression || rightExpression instanceof PyReferenceExpression - || isPercentage(rightExpression) || isCompound(rightExpression) || isMathOperation(rightExpression, expression.getOperator())) { - - final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project); - final StringBuilder stringBuilder = new StringBuilder(); - final PsiElement psiOperator = expression.getPsiOperator(); - if (psiOperator == null) return; - stringBuilder.append(targetText).append(" "). - append(psiOperator.getText()).append("= ").append(rightExpression.getText()); - final PyAugAssignmentStatementImpl augAssignment = elementGenerator.createFromText(LanguageLevel.forElement(element), - PyAugAssignmentStatementImpl.class, stringBuilder.toString()); - for (PsiComment comment : comments) - augAssignment.add(comment); - statement.replace(augAssignment); - } + final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project); + final StringBuilder stringBuilder = new StringBuilder(); + final PsiElement psiOperator = expression.getPsiOperator(); + if (psiOperator == null) return; + stringBuilder.append(targetText).append(" "). + append(psiOperator.getText()).append("= ").append(rightExpression.getText()); + final PyAugAssignmentStatementImpl augAssignment = elementGenerator.createFromText(LanguageLevel.forElement(element), + PyAugAssignmentStatementImpl.class, stringBuilder.toString()); + for (PsiComment comment : comments) + augAssignment.add(comment); + statement.replace(augAssignment); } } } } } - private static boolean isCompound(final PyExpression rightExpression) { - if (rightExpression instanceof PyCallExpression) { - final PyType type = rightExpression.getType(TypeEvalContext.fast()); - if (type != null && type.isBuiltin(TypeEvalContext.fast()) && - ("int".equals(type.getName()) || "str".equals(type.getName()))) return true; - } - return false; - } - - private static boolean isPercentage(final PyExpression rightExpression) { - return (rightExpression instanceof PyBinaryExpression && - ((PyBinaryExpression)rightExpression).getLeftExpression() instanceof PyStringLiteralExpression && - ((PyBinaryExpression)rightExpression).getOperator() == PyTokenTypes.PERC); - } - - private static boolean isMathOperation(final PyExpression rightExpression, final PyElementType mainOperator) { - final TokenSet first = TokenSet.create(PyTokenTypes.EXP, PyTokenTypes.FLOORDIV); - final TokenSet second = TokenSet.create(PyTokenTypes.MULT, PyTokenTypes.DIV, PyTokenTypes.PERC); - final TokenSet third = TokenSet.create(PyTokenTypes.PLUS, PyTokenTypes.MINUS); - if (rightExpression instanceof PyBinaryExpression){ - final PyElementType operator = ((PyBinaryExpression)rightExpression).getOperator(); - if (third.contains(mainOperator)) - return true; - else if (second.contains(mainOperator) && (second.contains(operator) || first.contains(operator))) - return true; - } - return false; - } } diff --git a/python/testData/inspections/AugmentAssignmentSubscription.py b/python/testData/inspections/AugmentAssignmentSubscription.py new file mode 100644 index 000000000000..fd57a20495a7 --- /dev/null +++ b/python/testData/inspections/AugmentAssignmentSubscription.py @@ -0,0 +1,3 @@ +current_sum = 1 +numbers = [1, 2, 3] +current_sum = current_sum + numbers[0] \ No newline at end of file diff --git a/python/testData/inspections/AugmentAssignmentSubscription_after.py b/python/testData/inspections/AugmentAssignmentSubscription_after.py new file mode 100644 index 000000000000..30e3c4cc8121 --- /dev/null +++ b/python/testData/inspections/AugmentAssignmentSubscription_after.py @@ -0,0 +1,3 @@ +current_sum = 1 +numbers = [1, 2, 3] +current_sum += numbers[0] \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/PyQuickFixTest.java b/python/testSrc/com/jetbrains/python/PyQuickFixTest.java index e0ee6d07c252..84b60a170548 100644 --- a/python/testSrc/com/jetbrains/python/PyQuickFixTest.java +++ b/python/testSrc/com/jetbrains/python/PyQuickFixTest.java @@ -241,6 +241,11 @@ public class PyQuickFixTest extends PyTestCase { PyBundle.message("QFIX.augment.assignment"), true, true); } + public void testAugmentAssignmentSubscription() { // PY-7715 + doInspectionTest("AugmentAssignmentFunction.py", PyAugmentAssignmentInspection.class, + PyBundle.message("QFIX.augment.assignment"), true, true); + } + public void testChainedComparisons() { // PY-1020 doInspectionTest("ChainedComparisons.py", PyChainedComparisonsInspection.class, PyBundle.message("QFIX.chained.comparison"), true, true);