added (PY-1265) "Introduce Variable" quickfix for "Statement has no effect" inspection

changed dialog in Convertlambda inspection
This commit is contained in:
Ekaterina Tuzova
2010-11-26 16:16:35 +03:00
parent 978aba1166
commit 25a8e92ff5
8 changed files with 73 additions and 15 deletions
@@ -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'
@@ -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
@@ -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()));
}
}
}
@@ -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();
@@ -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;
}
}
@@ -0,0 +1 @@
<warning descr="Statement seems to have no effect">a<caret>+ b</warning>
@@ -0,0 +1 @@
var = a + b
@@ -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() {