From f8cb5bb2a0e97000606dd9561ef7cde5b8a3a7f1 Mon Sep 17 00:00:00 2001 From: Ekaterina Tuzova Date: Thu, 7 Apr 2011 20:36:39 +0400 Subject: [PATCH] fixed PY-3239 "Remove redundant parentheses" quickfix doesn't work --- .../python/actions/RedundantParenthesesQuickFix.java | 11 ++++++++--- .../testData/inspections/RedundantParenthesesMore.py | 2 ++ .../inspections/RedundantParenthesesMore_after.py | 2 ++ .../testSrc/com/jetbrains/python/PyQuickFixTest.java | 5 +++++ 4 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 python/testData/inspections/RedundantParenthesesMore.py create mode 100644 python/testData/inspections/RedundantParenthesesMore_after.py diff --git a/python/src/com/jetbrains/python/actions/RedundantParenthesesQuickFix.java b/python/src/com/jetbrains/python/actions/RedundantParenthesesQuickFix.java index d717d4e7066f..daeba955680d 100644 --- a/python/src/com/jetbrains/python/actions/RedundantParenthesesQuickFix.java +++ b/python/src/com/jetbrains/python/actions/RedundantParenthesesQuickFix.java @@ -29,8 +29,11 @@ public class RedundantParenthesesQuickFix implements LocalQuickFix { public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiElement element = descriptor.getPsiElement(); PsiElement binaryExpression = ((PyParenthesizedExpression)element).getContainedExpression(); - if (binaryExpression instanceof PyBinaryExpression) - replaceBinaryExpression((PyBinaryExpression)binaryExpression); + if (binaryExpression instanceof PyBinaryExpression) { + if (!replaceBinaryExpression((PyBinaryExpression)binaryExpression)) { + element.replace(binaryExpression); + } + } else { while (element instanceof PyParenthesizedExpression) { PyExpression expression = ((PyParenthesizedExpression)element).getContainedExpression(); @@ -41,7 +44,7 @@ public class RedundantParenthesesQuickFix implements LocalQuickFix { } } - private static void replaceBinaryExpression(PyBinaryExpression element) { + private static boolean replaceBinaryExpression(PyBinaryExpression element) { PyExpression left = element.getLeftExpression(); PyExpression right = element.getRightExpression(); if (left instanceof PyParenthesizedExpression && @@ -51,7 +54,9 @@ public class RedundantParenthesesQuickFix implements LocalQuickFix { if (leftContained != null && rightContained != null) { left.replace(leftContained); right.replace(rightContained); + return true; } } + return false; } } diff --git a/python/testData/inspections/RedundantParenthesesMore.py b/python/testData/inspections/RedundantParenthesesMore.py new file mode 100644 index 000000000000..4b4f158262bf --- /dev/null +++ b/python/testData/inspections/RedundantParenthesesMore.py @@ -0,0 +1,2 @@ +while ((close_hr - current_hr_it) >= .5): + pass \ No newline at end of file diff --git a/python/testData/inspections/RedundantParenthesesMore_after.py b/python/testData/inspections/RedundantParenthesesMore_after.py new file mode 100644 index 000000000000..62f8f53a51de --- /dev/null +++ b/python/testData/inspections/RedundantParenthesesMore_after.py @@ -0,0 +1,2 @@ +while (close_hr - current_hr_it) >= .5: + pass \ 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 f2e894fbd6f1..9af97560e8c6 100644 --- a/python/testSrc/com/jetbrains/python/PyQuickFixTest.java +++ b/python/testSrc/com/jetbrains/python/PyQuickFixTest.java @@ -179,6 +179,11 @@ public class PyQuickFixTest extends PyLightFixtureTestCase { PyBundle.message("QFIX.redundant.parentheses"), true, true); } + public void testRedundantParenthesesMore() { // PY-3239 + doInspectionTest("RedundantParenthesesMore.py", PyRedundantParenthesesInspection.class, + PyBundle.message("QFIX.redundant.parentheses"), true, true); + } + public void testAugmentAssignment() { // PY-1415 doInspectionTest("AugmentAssignment.py", PyAugmentAssignmentInspection.class, PyBundle.message("QFIX.augment.assignment"), true, true);