fixed PY-13685 Replace function call: invalid parenthesis for calls with inline comments

This commit is contained in:
Ekaterina Tuzova
2014-08-25 13:39:38 +04:00
parent 61ec21d0e1
commit 8d690be3ee
7 changed files with 33 additions and 8 deletions
@@ -72,9 +72,6 @@ public class PyStatementEffectInspection extends PyInspection {
final PyTryPart tryPart = PsiTreeUtil.getParentOfType(node, PyTryPart.class);
if (tryPart != null) {
final PyStatementList statementList = tryPart.getStatementList();
if (statementList == null) {
return;
}
if (statementList.getStatements().length == 1 && statementList.getStatements()[0] == node) {
return;
}
@@ -18,14 +18,12 @@ package com.jetbrains.python.inspections.quickfix;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiComment;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiWhiteSpace;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.PyNames;
import com.jetbrains.python.psi.LanguageLevel;
import com.jetbrains.python.psi.PyElementGenerator;
import com.jetbrains.python.psi.PyExpression;
import com.jetbrains.python.psi.PyReferenceExpression;
import com.jetbrains.python.psi.*;
import org.jetbrains.annotations.NotNull;
/**
@@ -77,14 +75,23 @@ public class StatementEffectFunctionCallQuickFix implements LocalQuickFix {
RemoveUnnecessaryBackslashQuickFix.removeBackSlash(next);
if (whiteSpace != null) whiteSpace.delete();
String commentText = null;
if (next != null) {
final String text = next.getText();
final PsiElement lastChild = next.getLastChild();
if (lastChild instanceof PsiComment) {
commentText = lastChild.getText();
}
final String text = next instanceof PyExpressionStatement ? ((PyExpressionStatement)next).getExpression().getText() : next.getText();
stringBuilder.append(text);
if (text.endsWith(",") && PyNames.PRINT.equals(expressionText))
stringBuilder.append(" end=' '");
next.delete();
}
stringBuilder.append(")");
if (commentText != null) {
stringBuilder.append(commentText);
}
expression.replace(elementGenerator.createFromText(LanguageLevel.forElement(expression), PyExpression.class,
stringBuilder.toString()));
}
@@ -0,0 +1 @@
<warning descr="Statement seems to have no effect and can be replaced with function call to have effect">print</warning><error descr="End of statement expected"> </error><warning descr="Statement seems to have no effect">'%s %s %s %s' % bar</warning> # <- doesn't work either
@@ -0,0 +1 @@
print('%s %s %s %s' % bar) # <- doesn't work either
@@ -0,0 +1 @@
<warning descr="Statement seems to have no effect and can be replaced with function call to have effect">print</warning><error descr="End of statement expected"> </error><warning descr="Statement seems to have no effect">var,</warning>
@@ -0,0 +1 @@
print(var, end=' ')
@@ -251,6 +251,23 @@ public class PyQuickFixTest extends PyTestCase {
PyBundle.message("QFIX.statement.effect.introduce.variable"), true, true);
}
public void testReplacePrintEnd() {
runWithLanguageLevel(LanguageLevel.PYTHON34, new Runnable() {
@Override
public void run() {
doInspectionTest("ReplacePrintEnd.py", PyStatementEffectInspection.class, PyBundle.message("QFIX.statement.effect"), true, true);
}});
}
public void testReplacePrintComment() {
runWithLanguageLevel(LanguageLevel.PYTHON34, new Runnable() {
@Override
public void run() {
doInspectionTest("ReplacePrintComment.py", PyStatementEffectInspection.class, PyBundle.message("QFIX.statement.effect"), true,
true);
}});
}
public void testUnresolvedWith() { // PY-2083
setLanguageLevel(LanguageLevel.PYTHON25);
doInspectionTest("UnresolvedWith.py", PyUnresolvedReferencesInspection.class,