diff --git a/python/src/com/jetbrains/python/PyBundle.properties b/python/src/com/jetbrains/python/PyBundle.properties index a4a812658721..aef1faa6141e 100644 --- a/python/src/com/jetbrains/python/PyBundle.properties +++ b/python/src/com/jetbrains/python/PyBundle.properties @@ -59,6 +59,8 @@ QFIX.statement.effect.introduce.variable=Introduce variable QFIX.unresolved.reference=Reference can be resolved added self +QFIX.introduce.variable=Introduce variable for statement + # Intentions: INTN INTN.Family.convert.import.unqualify=Convert 'import module' to 'from module import' INTN.Family.convert.import.qualify=Convert 'from module import' to 'import module' diff --git a/python/src/com/jetbrains/python/actions/StatementEffectQuickFix.java b/python/src/com/jetbrains/python/actions/StatementEffectFunctionCallQuickFix.java similarity index 91% rename from python/src/com/jetbrains/python/actions/StatementEffectQuickFix.java rename to python/src/com/jetbrains/python/actions/StatementEffectFunctionCallQuickFix.java index 805c89d9d26a..094a22551ff8 100644 --- a/python/src/com/jetbrains/python/actions/StatementEffectQuickFix.java +++ b/python/src/com/jetbrains/python/actions/StatementEffectFunctionCallQuickFix.java @@ -17,9 +17,9 @@ import org.jetbrains.annotations.NotNull; * * QuickFix to replace statement that has no effect with function call */ -public class StatementEffectQuickFix implements LocalQuickFix { +public class StatementEffectFunctionCallQuickFix implements LocalQuickFix { - public StatementEffectQuickFix() { + public StatementEffectFunctionCallQuickFix() { } @NotNull diff --git a/python/src/com/jetbrains/python/actions/StatementEffectIntroduceVariableQuickFix.java b/python/src/com/jetbrains/python/actions/StatementEffectIntroduceVariableQuickFix.java new file mode 100644 index 000000000000..e82997b82d6c --- /dev/null +++ b/python/src/com/jetbrains/python/actions/StatementEffectIntroduceVariableQuickFix.java @@ -0,0 +1,52 @@ +package com.jetbrains.python.actions; + +import com.intellij.codeInspection.LocalQuickFix; +import com.intellij.codeInspection.ProblemDescriptor; +import com.intellij.ide.IdeBundle; +import com.intellij.openapi.application.Application; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.ui.Messages; +import com.intellij.psi.PsiElement; +import com.jetbrains.python.PyBundle; +import com.jetbrains.python.psi.*; +import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; + +/** + * User: catherine + * + * Quickfix to introduce variable if statement seems to have no effect + */ +public class StatementEffectIntroduceVariableQuickFix implements LocalQuickFix { + + public StatementEffectIntroduceVariableQuickFix() {} + + @NotNull + public String getName() { + return PyBundle.message("QFIX.introduce.variable"); + } + + @NonNls + @NotNull + public String getFamilyName() { + return PyBundle.message("INSP.GROUP.python"); + } + + public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) { + PsiElement expression = descriptor.getPsiElement(); + String name = "var"; + if (expression != null) { + Application application = ApplicationManager.getApplication(); + if (application != null && !application.isUnitTestMode()) { + name = Messages.showInputDialog(project, IdeBundle.message("prompt.enter.new.variable.name"), + IdeBundle.message("title.new.variable"), Messages.getQuestionIcon()); + if (name == null) return; + } + if (name.isEmpty()) return; + PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project); + expression.replace(elementGenerator.createFromText(LanguageLevel.forElement(expression), PyAssignmentStatement.class, + name + " = " + expression.getText())); + } + } +} diff --git a/python/src/com/jetbrains/python/codeInsight/intentions/PyConvertLambdaToFunctionIntention.java b/python/src/com/jetbrains/python/codeInsight/intentions/PyConvertLambdaToFunctionIntention.java index 0ab22729a568..d88af78f8ba6 100644 --- a/python/src/com/jetbrains/python/codeInsight/intentions/PyConvertLambdaToFunctionIntention.java +++ b/python/src/com/jetbrains/python/codeInsight/intentions/PyConvertLambdaToFunctionIntention.java @@ -1,10 +1,12 @@ package com.jetbrains.python.codeInsight.intentions; import com.intellij.codeInsight.intention.impl.BaseIntentionAction; +import com.intellij.ide.IdeBundle; import com.intellij.openapi.application.Application; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; +import com.intellij.openapi.ui.Messages; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import com.intellij.psi.util.PsiTreeUtil; @@ -50,15 +52,12 @@ public class PyConvertLambdaToFunctionIntention extends BaseIntentionAction { else { Application application = ApplicationManager.getApplication(); if (application != null && !application.isUnitTestMode()) { - AskNameDialog dialog = new AskNameDialog(project); - dialog.setTitle("Enter function name"); - dialog.show(); - if (!dialog.isOK()) return; // 'Cancel' button cancels everything - name = dialog.getAlias(); - if (name.isEmpty()) return; + name = Messages.showInputDialog(project, IdeBundle.message("prompt.enter.new.variable.name"), + IdeBundle.message("title.new.variable"), Messages.getQuestionIcon()); + if (name == null) return; } } - + if (name.isEmpty()) return; PyExpression body = lambdaExpression.getBody(); PyParameter[] parameters = lambdaExpression.getParameterList().getParameters(); StringBuilder stringBuilder = new StringBuilder(); diff --git a/python/src/com/jetbrains/python/inspections/PyStatementEffectInspection.java b/python/src/com/jetbrains/python/inspections/PyStatementEffectInspection.java index 6c4f634185fe..c7c9d1ae51ff 100644 --- a/python/src/com/jetbrains/python/inspections/PyStatementEffectInspection.java +++ b/python/src/com/jetbrains/python/inspections/PyStatementEffectInspection.java @@ -2,12 +2,12 @@ package com.jetbrains.python.inspections; import com.intellij.codeInspection.LocalInspectionToolSession; import com.intellij.codeInspection.ProblemsHolder; -import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElementVisitor; import com.intellij.psi.ResolveResult; import com.intellij.psi.util.PsiTreeUtil; import com.jetbrains.python.PyBundle; -import com.jetbrains.python.actions.StatementEffectQuickFix; +import com.jetbrains.python.actions.StatementEffectFunctionCallQuickFix; +import com.jetbrains.python.actions.StatementEffectIntroduceVariableQuickFix; import com.jetbrains.python.console.PydevConsoleRunner; import com.jetbrains.python.psi.*; import com.jetbrains.python.psi.resolve.PyResolveContext; @@ -16,8 +16,6 @@ import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.sql.Statement; - /** * @author Alexey.Ivanov */ @@ -64,7 +62,7 @@ public class PyStatementEffectInspection extends PyInspection { return; } } - registerProblem(expression, "Statement seems to have no effect"); + registerProblem(expression, "Statement seems to have no effect", new StatementEffectIntroduceVariableQuickFix()); } private boolean hasEffect(@Nullable PyExpression expression) { @@ -125,7 +123,7 @@ public class PyStatementEffectInspection extends PyInspection { ResolveResult[] results = referenceExpression.getReference().multiResolve(true); for (ResolveResult res : results) { if (res.getElement() instanceof PyFunction) { - registerProblem(expression, "Statement seems to have no effect and can be replaced with function call to have effect", new StatementEffectQuickFix()); + registerProblem(expression, "Statement seems to have no effect and can be replaced with function call to have effect", new StatementEffectFunctionCallQuickFix()); return true; } } diff --git a/python/testData/inspections/StatementEffectIntroduceVariable.py b/python/testData/inspections/StatementEffectIntroduceVariable.py new file mode 100644 index 000000000000..107d3e6a49ce --- /dev/null +++ b/python/testData/inspections/StatementEffectIntroduceVariable.py @@ -0,0 +1 @@ +a+ b \ No newline at end of file diff --git a/python/testData/inspections/StatementEffectIntroduceVariable_after.py b/python/testData/inspections/StatementEffectIntroduceVariable_after.py new file mode 100644 index 000000000000..0db79b1d5e6f --- /dev/null +++ b/python/testData/inspections/StatementEffectIntroduceVariable_after.py @@ -0,0 +1 @@ +var = a + b \ 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 0d8d046ffeb5..2f248397e95e 100644 --- a/python/testSrc/com/jetbrains/python/PyQuickFixTest.java +++ b/python/testSrc/com/jetbrains/python/PyQuickFixTest.java @@ -188,6 +188,11 @@ public class PyQuickFixTest extends PyLightFixtureTestCase { PyBundle.message("QFIX.statement.effect"), true, true); } + public void testStatementEffectIntroduceVariable() { // PY-1265 + doInspectionTest("StatementEffectIntroduceVariable.py", PyStatementEffectInspection.class, + PyBundle.message("QFIX.statement.effect.introduce.variable"), true, true); + } + @Override @NonNls protected String getTestDataPath() {