diff --git a/python/src/META-INF/python-plugin-common.xml b/python/src/META-INF/python-plugin-common.xml
index 9ea66976418b..f3aadb338fdf 100644
--- a/python/src/META-INF/python-plugin-common.xml
+++ b/python/src/META-INF/python-plugin-common.xml
@@ -128,6 +128,11 @@
Python
+
+ com.jetbrains.python.codeInsight.intentions.PyJoinIfIntention
+ Python
+
+
diff --git a/python/src/com/jetbrains/python/PyBundle.properties b/python/src/com/jetbrains/python/PyBundle.properties
index d16e5876029d..3ff7139dca89 100644
--- a/python/src/com/jetbrains/python/PyBundle.properties
+++ b/python/src/com/jetbrains/python/PyBundle.properties
@@ -55,6 +55,8 @@ QFIX.chained.comparison=Simplify chained comparison
QFIX.statement.effect=Statement can be replaced with function call
+QFIX.statement.effect.introduce.variable=Introduce variable
+
QFIX.unresolved.reference=Reference can be resolved added self
# Intentions: INTN
@@ -120,6 +122,8 @@ INTN.flip.$0.to.$1=Flip ''{0}'' to ''{1}''
INTN.convert.string=Convert single-quoted string to double-quoted
+INTN.join.if=Join if's
+
# Conflict checker
CONFLICT.name.$0.obscured=Name ''{0}'' obscured by local definitions
CONFLICT.name.$0.obscured.cannot.convert=Name ''{0}'' obscured. Cannot convert.
diff --git a/python/src/com/jetbrains/python/codeInsight/intentions/PyJoinIfIntention.java b/python/src/com/jetbrains/python/codeInsight/intentions/PyJoinIfIntention.java
new file mode 100644
index 000000000000..67726e15f934
--- /dev/null
+++ b/python/src/com/jetbrains/python/codeInsight/intentions/PyJoinIfIntention.java
@@ -0,0 +1,65 @@
+package com.jetbrains.python.codeInsight.intentions;
+
+import com.intellij.codeInsight.intention.impl.BaseIntentionAction;
+import com.intellij.openapi.editor.Editor;
+import com.intellij.openapi.project.Project;
+import com.intellij.psi.PsiElement;
+import com.intellij.psi.PsiFile;
+import com.intellij.psi.tree.IElementType;
+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;
+
+/**
+ * User: catherine
+ */
+public class PyJoinIfIntention extends BaseIntentionAction {
+ @NotNull
+ public String getFamilyName() {
+ return PyBundle.message("INTN.join.if");
+ }
+
+ @NotNull
+ public String getText() {
+ return PyBundle.message("INTN.join.if");
+ }
+
+ public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
+ PyIfStatement expression =
+ PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyIfStatement.class);
+ while (expression != null) {
+ PyStatement firstStatement = expression.getIfPart().getStatementList().getStatements()[0];
+ if (firstStatement instanceof PyIfStatement) {
+ return true;
+ }
+ expression = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyIfStatement.class);
+ }
+ return false;
+ }
+
+ public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
+ PyIfStatement expression =
+ PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyIfStatement.class);
+ PyStatement firstStatement = null;
+
+ while (expression != null) {
+ firstStatement = expression.getIfPart().getStatementList().getStatements()[0];
+ if (firstStatement instanceof PyIfStatement) {
+ break;
+ }
+ expression = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyIfStatement.class);
+ }
+ if (firstStatement != null && firstStatement instanceof PyIfStatement) {
+ PyExpression condition = ((PyIfStatement)firstStatement).getIfPart().getCondition();
+ PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
+ PyExpression newCondition = elementGenerator.createExpressionFromText(expression.getIfPart().getCondition().getText() + " and " + condition.getText());
+ expression.getIfPart().getCondition().replace(newCondition);
+
+ PyStatementList stList = ((PyIfStatement)firstStatement).getIfPart().getStatementList();
+ expression.getIfPart().getStatementList().replace(stList);
+ }
+ }
+}
diff --git a/python/src/com/jetbrains/python/codeInsight/intentions/ReplaceListComprehensionWithForIntention.java b/python/src/com/jetbrains/python/codeInsight/intentions/ReplaceListComprehensionWithForIntention.java
index fdb7b4cd86f1..b55f49fd53cb 100644
--- a/python/src/com/jetbrains/python/codeInsight/intentions/ReplaceListComprehensionWithForIntention.java
+++ b/python/src/com/jetbrains/python/codeInsight/intentions/ReplaceListComprehensionWithForIntention.java
@@ -1,6 +1,7 @@
package com.jetbrains.python.codeInsight.intentions;
import com.intellij.codeInsight.intention.IntentionAction;
+import com.intellij.lang.ASTNode;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.TextComponentAccessor;
@@ -13,6 +14,8 @@ import com.jetbrains.python.PyBundle;
import com.jetbrains.python.PyTokenSeparatorGenerator;
import com.jetbrains.python.PyTokenTypes;
import com.jetbrains.python.psi.*;
+import com.jetbrains.python.psi.impl.PyPsiUtils;
+import com.jetbrains.python.psi.impl.PyStatementListImpl;
import org.jetbrains.annotations.NotNull;
import sun.tools.tree.Statement;
@@ -57,18 +60,19 @@ public class ReplaceListComprehensionWithForIntention implements IntentionAction
PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
if (parent instanceof PyAssignmentStatement) {
- PsiElement leftExpr = ((PyAssignmentStatement)parent).getLeftHandSideExpression();
- PyAssignmentStatement initAssignment = elementGenerator.createFromText(LanguageLevel.getDefault(), PyAssignmentStatement.class,
- leftExpr.getText() + " = []");
- PsiElement lineBreak = elementGenerator.createFromText(LanguageLevel.getDefault(), PsiWhiteSpace.class, "\n");
- initAssignment.add(lineBreak);
+ PsiElement leftExpr = ((PyAssignmentStatement)parent).getLeftHandSideExpression();
+ PyAssignmentStatement initAssignment = elementGenerator.createFromText(LanguageLevel.forElement(expression), PyAssignmentStatement.class,
+ leftExpr.getText() + " = []");
PyStatement result = elementGenerator.createFromText(LanguageLevel.forElement(expression), PyStatement.class,
leftExpr.getText() + ".append("+ getResult(expression).getText() +")");
-
PyForStatement forStatement = createForLoop(expression, elementGenerator, result);
- initAssignment.add(forStatement);
- parent.replace(initAssignment);
+
+ PyStatementList stList = new PyStatementListImpl(initAssignment.getNode());
+ stList.add(initAssignment);
+ stList.add(forStatement);
+ stList.getStatements()[0].delete();
+ parent.replace(stList);
}
else if (parent instanceof PyPrintStatement) {
@@ -84,7 +88,7 @@ public class ReplaceListComprehensionWithForIntention implements IntentionAction
if (forComps.size() != 0) {
ComprhForComponent forComponent = forComps.get(0);
- PyForStatement forStatement = elementGenerator.createFromText(LanguageLevel.getDefault(), PyForStatement.class,
+ PyForStatement forStatement = elementGenerator.createFromText(LanguageLevel.forElement(expression), PyForStatement.class,
"for " + forComponent.getIteratorVariable().getText() + " in " +
forComponent.getIteratedList().getText() + ":\n a+1");
@@ -92,9 +96,7 @@ public class ReplaceListComprehensionWithForIntention implements IntentionAction
if (ifComps.size() != 0) {
addIfComponents(forStatement, ifComps, elementGenerator);
}
-
addForComponents(forStatement, expression.getResultExpression(), elementGenerator, result);
-
return forStatement;
}
return null;
@@ -105,7 +107,7 @@ public class ReplaceListComprehensionWithForIntention implements IntentionAction
PyElementGenerator elementGenerator) {
PyStatementList pyStatementList = forStatement.getForPart().getStatementList();
for (ComprhIfComponent ifComp : ifComps) {
- PyIfStatement ifStat = elementGenerator.createFromText(LanguageLevel.getDefault(), PyIfStatement.class,
+ PyIfStatement ifStat = elementGenerator.createFromText(LanguageLevel.forElement(forStatement), PyIfStatement.class,
"if " + ifComp.getTest().getText() + ":\n a+1");
pyStatementList.getStatements()[0].replace(ifStat);
pyStatementList = ((PyIfStatement)pyStatementList.getStatements()[0]).getIfPart().getStatementList();
@@ -131,7 +133,7 @@ public class ReplaceListComprehensionWithForIntention implements IntentionAction
List forComps = ((PyListCompExpression)expression).getForComponents();
if ( forComps.size() != 0) {
ComprhForComponent comp = forComps.get(0);
- PyForStatement pyForStatement = elementGenerator.createFromText(LanguageLevel.getDefault(), PyForStatement.class,
+ PyForStatement pyForStatement = elementGenerator.createFromText(LanguageLevel.forElement(expression), PyForStatement.class,
"for " + comp.getIteratorVariable().getText() + " in "+ comp.getIteratedList().getText() + ":\n a+1");
List ifComps = ((PyListCompExpression)expression).getIfComponents();
if (ifComps.size() != 0) {
diff --git a/python/testData/intentions/afterJoinIf.py b/python/testData/intentions/afterJoinIf.py
new file mode 100644
index 000000000000..91bf4784ca2e
--- /dev/null
+++ b/python/testData/intentions/afterJoinIf.py
@@ -0,0 +1,4 @@
+def foo():
+ if a + 2 > 3 and b < 4:
+ a = a and b
+ b = 4
diff --git a/python/testData/intentions/afterReplaceListComprehensionWithFor.py b/python/testData/intentions/afterReplaceListComprehensionWithFor.py
index 0ef69b1ecacc..5bf49d51dbdf 100644
--- a/python/testData/intentions/afterReplaceListComprehensionWithFor.py
+++ b/python/testData/intentions/afterReplaceListComprehensionWithFor.py
@@ -1,3 +1,4 @@
+a = 2
smth = []
for z in xrange(14):
if z != 3:
@@ -8,4 +9,6 @@ for z in xrange(14):
for x in xrange(10):
if x != 2:
if x != 5:
- smth.append(x + y + z)
\ No newline at end of file
+ smth.append(x + y + z)
+
+a = 5
\ No newline at end of file
diff --git a/python/testData/intentions/beforeJoinIf.py b/python/testData/intentions/beforeJoinIf.py
new file mode 100644
index 000000000000..aa9898d1b04d
--- /dev/null
+++ b/python/testData/intentions/beforeJoinIf.py
@@ -0,0 +1,5 @@
+def foo():
+ if a + 2 > 3:
+ if b < 4:
+ a = a and b
+ b = 4
diff --git a/python/testData/intentions/beforeReplaceListComprehensionWithFor.py b/python/testData/intentions/beforeReplaceListComprehensionWithFor.py
index e163fc3c7ea0..50d83f3957b8 100644
--- a/python/testData/intentions/beforeReplaceListComprehensionWithFor.py
+++ b/python/testData/intentions/beforeReplaceListComprehensionWithFor.py
@@ -1 +1,4 @@
-smth = [x+y+z for x in xrange(10) if x != 2 if x != 5 for y in xrange(12) if y != 1 if y != 4 for z in xrange(14) if z != 3 if z != 8]
\ No newline at end of file
+a = 2
+smth = [x+y+z for x in xrange(10) if x != 2 if x != 5 for y in xrange(12) if y != 1 if y != 4 for z in xrange(14) if z != 3 if z != 8]
+
+a = 5
\ 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 b9fba4992a90..a5279690ff5b 100644
--- a/python/testSrc/com/jetbrains/python/PyIntentionTest.java
+++ b/python/testSrc/com/jetbrains/python/PyIntentionTest.java
@@ -109,4 +109,7 @@ public class PyIntentionTest extends PyLightFixtureTestCase {
doTest(PyBundle.message("INTN.replace.list.comprehensions.with.for"));
}
+ public void testJoinIf() {
+ doTest(PyBundle.message("INTN.join.if"));
+ }
}