From d08bdb75bb1ec9cb14cf7b017245e9c0ac45703c Mon Sep 17 00:00:00 2001 From: Ekaterina Tuzova Date: Sat, 12 Nov 2011 22:46:13 +0100 Subject: [PATCH] fixed PY-4697 "join 2 if's" inspection suggestion is invalid --- .../intentions/PyJoinIfIntention.java | 18 +++++++++++++----- .../testData/intentions/afterJoinIfBinary.py | 2 ++ .../testData/intentions/beforeJoinIfBinary.py | 3 +++ .../com/jetbrains/python/PyIntentionTest.java | 4 ++++ 4 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 python/testData/intentions/afterJoinIfBinary.py create mode 100644 python/testData/intentions/beforeJoinIfBinary.py diff --git a/python/src/com/jetbrains/python/codeInsight/intentions/PyJoinIfIntention.java b/python/src/com/jetbrains/python/codeInsight/intentions/PyJoinIfIntention.java index 720f3f82ba5a..3ee654506755 100644 --- a/python/src/com/jetbrains/python/codeInsight/intentions/PyJoinIfIntention.java +++ b/python/src/com/jetbrains/python/codeInsight/intentions/PyJoinIfIntention.java @@ -9,6 +9,7 @@ import com.intellij.psi.PsiFile; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.IncorrectOperationException; import com.jetbrains.python.PyBundle; +import com.jetbrains.python.PyTokenTypes; import com.jetbrains.python.psi.*; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -65,17 +66,24 @@ public class PyJoinIfIntention extends BaseIntentionAction { PyIfStatement ifStatement = getIfStatement(expression); PyStatement firstStatement = getFirstStatement(ifStatement); - + if (ifStatement == null) return; if (firstStatement != null && firstStatement instanceof PyIfStatement) { PyExpression condition = ((PyIfStatement)firstStatement).getIfPart().getCondition(); PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project); - PyExpression newCondition = elementGenerator.createExpressionFromText( - ifStatement.getIfPart().getCondition().getText() + " and " + condition.getText()); - ifStatement.getIfPart().getCondition().replace(newCondition); + PyExpression ifCondition = ifStatement.getIfPart().getCondition(); + if (ifCondition == null || condition == null) return; + StringBuilder replacementText = new StringBuilder(ifCondition.getText() + " and "); + if (condition instanceof PyBinaryExpression && ((PyBinaryExpression)condition).getOperator() == PyTokenTypes.OR_KEYWORD) { + replacementText.append("(").append(condition.getText()).append(")"); + } else + replacementText.append(condition.getText()); + + PyExpression newCondition = elementGenerator.createExpressionFromText(replacementText.toString()); + ifCondition.replace(newCondition); PyStatementList stList = ((PyIfStatement)firstStatement).getIfPart().getStatementList(); PyStatementList ifStatementList = ifStatement.getIfPart().getStatementList(); - + if (ifStatementList == null || stList == null) return; List comments = PsiTreeUtil.getChildrenOfTypeAsList(ifStatement.getIfPart(), PsiComment.class); comments.addAll(PsiTreeUtil.getChildrenOfTypeAsList(((PyIfStatement)firstStatement).getIfPart(), PsiComment.class)); comments.addAll(PsiTreeUtil.getChildrenOfTypeAsList(ifStatementList, PsiComment.class)); diff --git a/python/testData/intentions/afterJoinIfBinary.py b/python/testData/intentions/afterJoinIfBinary.py new file mode 100644 index 000000000000..f24f3b2c5930 --- /dev/null +++ b/python/testData/intentions/afterJoinIfBinary.py @@ -0,0 +1,2 @@ + if value is not None and (not validate_uint(value) or value <= self.begin): + print value \ No newline at end of file diff --git a/python/testData/intentions/beforeJoinIfBinary.py b/python/testData/intentions/beforeJoinIfBinary.py new file mode 100644 index 000000000000..e1ba2fc0abec --- /dev/null +++ b/python/testData/intentions/beforeJoinIfBinary.py @@ -0,0 +1,3 @@ + if value is not None: + if not validate_uint(value) or value <= self.begin: + print value \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/PyIntentionTest.java b/python/testSrc/com/jetbrains/python/PyIntentionTest.java index a79af4ae8c08..207baaa03beb 100644 --- a/python/testSrc/com/jetbrains/python/PyIntentionTest.java +++ b/python/testSrc/com/jetbrains/python/PyIntentionTest.java @@ -134,6 +134,10 @@ public class PyIntentionTest extends PyTestCase { doNegativeTest(PyBundle.message("INTN.join.if.text")); } + public void testJoinIfBinary() { //PY-4697 + doTest(PyBundle.message("INTN.join.if.text")); + } + public void testJoinIfMultiStatements() { //PY-2970 doNegativeTest(PyBundle.message("INTN.join.if.text")); }