From 94af316f4311795a77b82d8a2a8ff15310968336 Mon Sep 17 00:00:00 2001 From: Ekaterina Tuzova Date: Tue, 15 Mar 2011 16:21:48 +0300 Subject: [PATCH] fixed PY-3094 Intention to transform conditional expression into if/else statement --- python/src/META-INF/python-plugin-common.xml | 6 ++ .../com/jetbrains/python/PyBundle.properties | 3 + ...ansformConditionalExpressionIntention.java | 65 +++++++++++++++++++ .../afterTransformConditionalExpression.py | 4 ++ .../beforeTransformConditionalExpression.py | 1 + .../com/jetbrains/python/PyIntentionTest.java | 4 ++ 6 files changed, 83 insertions(+) create mode 100644 python/src/com/jetbrains/python/codeInsight/intentions/PyTransformConditionalExpressionIntention.java create mode 100644 python/testData/intentions/afterTransformConditionalExpression.py create mode 100644 python/testData/intentions/beforeTransformConditionalExpression.py diff --git a/python/src/META-INF/python-plugin-common.xml b/python/src/META-INF/python-plugin-common.xml index e4e9d582e90a..fbf92162505e 100644 --- a/python/src/META-INF/python-plugin-common.xml +++ b/python/src/META-INF/python-plugin-common.xml @@ -189,6 +189,11 @@ Python + + com.jetbrains.python.codeInsight.intentions.PyTransformConditionalExpressionIntention + Python + + @@ -432,6 +437,7 @@ + diff --git a/python/src/com/jetbrains/python/PyBundle.properties b/python/src/com/jetbrains/python/PyBundle.properties index 778b24972379..0c869716b856 100644 --- a/python/src/com/jetbrains/python/PyBundle.properties +++ b/python/src/com/jetbrains/python/PyBundle.properties @@ -155,6 +155,9 @@ INTN.convert.variadic.param=Convert from variadic to normal parameter(s) # PyConvertTripleQuotedStringIntention INTN.triple.quoted.string=Convert triple-quoted string to single-quoted string +# PyTransformConditionalExpressionIntention +INTN.transform.into.if.else.statement=Transform conditional expression into if/else statement + # 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/PyTransformConditionalExpressionIntention.java b/python/src/com/jetbrains/python/codeInsight/intentions/PyTransformConditionalExpressionIntention.java new file mode 100644 index 000000000000..c1b2ece36de5 --- /dev/null +++ b/python/src/com/jetbrains/python/codeInsight/intentions/PyTransformConditionalExpressionIntention.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.PsiFile; +import com.intellij.psi.util.PsiTreeUtil; +import com.intellij.util.IncorrectOperationException; +import com.jetbrains.python.PyBundle; +import com.jetbrains.python.psi.*; +import org.jetbrains.annotations.NotNull; + +/** + * User: catherine + * Intention to transform conditional expression into if/else statement + * For instance, + * + * x = a if cond else b + * + * into: + * + * if cond: + * x = a + * else: + * x = b + */ +public class PyTransformConditionalExpressionIntention extends BaseIntentionAction { + @NotNull + public String getFamilyName() { + return PyBundle.message("INTN.transform.into.if.else.statement"); + } + + @NotNull + public String getText() { + return PyBundle.message("INTN.transform.into.if.else.statement"); + } + + public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { + PyAssignmentStatement expression = + PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyAssignmentStatement.class); + if (expression != null && expression.getAssignedValue() instanceof PyConditionalExpression) { + return true; + } + return false; + } + + public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { + PyAssignmentStatement assignmentStatement = + PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyAssignmentStatement.class); + assert assignmentStatement != null; + PyConditionalExpression expression = + (PyConditionalExpression)assignmentStatement.getAssignedValue(); + assert expression != null; + String condition = expression.getCondition().getText(); + String truePart = expression.getTruePart().getText(); + String falsePart = expression.getFalsePart().getText(); + String target = assignmentStatement.getLeftHandSideExpression().getText(); + + PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project); + String text = "if " + condition + ":\n\t" + target + " = " + truePart + "\nelse:\n\t" + target + " = " + falsePart; + PyIfStatement ifStatement = elementGenerator.createFromText(LanguageLevel.forElement(expression), PyIfStatement.class, text); + assignmentStatement.replace(ifStatement); + } + +} diff --git a/python/testData/intentions/afterTransformConditionalExpression.py b/python/testData/intentions/afterTransformConditionalExpression.py new file mode 100644 index 000000000000..da84f9877c0c --- /dev/null +++ b/python/testData/intentions/afterTransformConditionalExpression.py @@ -0,0 +1,4 @@ +if cond: + x = a +else: + x = b \ No newline at end of file diff --git a/python/testData/intentions/beforeTransformConditionalExpression.py b/python/testData/intentions/beforeTransformConditionalExpression.py new file mode 100644 index 000000000000..2204e0db1aba --- /dev/null +++ b/python/testData/intentions/beforeTransformConditionalExpression.py @@ -0,0 +1 @@ +x = a if cond else b \ 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 19e3bed112a7..b4e7e07a58fe 100644 --- a/python/testSrc/com/jetbrains/python/PyIntentionTest.java +++ b/python/testSrc/com/jetbrains/python/PyIntentionTest.java @@ -164,4 +164,8 @@ public class PyIntentionTest extends PyLightFixtureTestCase { public void testConvertTripleQuotedString() { //PY-2697 doTest(PyBundle.message("INTN.triple.quoted.string")); } + + public void testTransformConditionalExpression() { //PY-3094 + doTest(PyBundle.message("INTN.transform.into.if.else.statement")); + } }