mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
fixed PY-3094 Intention to transform conditional expression into if/else statement
This commit is contained in:
@@ -189,6 +189,11 @@
|
||||
<category>Python</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>com.jetbrains.python.codeInsight.intentions.PyTransformConditionalExpressionIntention</className>
|
||||
<category>Python</category>
|
||||
</intentionAction>
|
||||
|
||||
<stubElementTypeHolder class="com.jetbrains.python.PyElementTypes"/>
|
||||
<inspectionToolProvider implementation="com.jetbrains.python.inspections.PythonInspectionToolProvider"/>
|
||||
|
||||
@@ -432,6 +437,7 @@
|
||||
<annotator language="Locale" implementationClass="com.jetbrains.django.localization.validation.LocaleAnnotatingVisitor"/>
|
||||
<quoteHandler fileType="Locale" className="com.jetbrains.django.localization.editor.LocaleQuoteHandler"/>
|
||||
<problemFileHighlightFilter implementation="com.jetbrains.django.localization.LocaleProblemFileHighlightFilter"/>
|
||||
<!--<fileEditorProvider implementation="com.jetbrains.django.localization.editor.LocaleFileEditorProvider"/>-->
|
||||
</extensions>
|
||||
|
||||
<extensionPoints>
|
||||
|
||||
@@ -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.
|
||||
|
||||
+65
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
if cond:
|
||||
x = a
|
||||
else:
|
||||
x = b
|
||||
@@ -0,0 +1 @@
|
||||
x = a if <caret>cond else b
|
||||
@@ -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"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user