fixed PY-3197 Quck fix doesn't work for string with string formatting symbols

This commit is contained in:
Ekaterina Tuzova
2011-04-07 20:19:02 +04:00
parent 2224b945a6
commit b86253fff9
4 changed files with 16 additions and 3 deletions
@@ -5,6 +5,7 @@ import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.PyTokenTypes;
import com.jetbrains.python.psi.*;
import com.jetbrains.python.psi.impl.PyAugAssignmentStatementImpl;
import org.jetbrains.annotations.NotNull;
@@ -48,9 +49,8 @@ public class AugmentedAssignmentQuickFix implements LocalQuickFix {
if (leftExpression != null
&& (leftExpression instanceof PyReferenceExpression || leftExpression instanceof PySubscriptionExpression)) {
if (leftExpression.getText().equals(target.getText())) {
if (rightExpression instanceof PyNumericLiteralExpression ||
rightExpression instanceof PyStringLiteralExpression
|| rightExpression instanceof PyReferenceExpression) {
if (rightExpression instanceof PyNumericLiteralExpression || rightExpression instanceof PyStringLiteralExpression
|| rightExpression instanceof PyReferenceExpression || isPercentage(rightExpression) ) {
PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
StringBuilder stringBuilder = new StringBuilder();
@@ -64,4 +64,10 @@ public class AugmentedAssignmentQuickFix implements LocalQuickFix {
}
}
}
private boolean isPercentage(PyExpression rightExpression) {
return (rightExpression instanceof PyBinaryExpression &&
((PyBinaryExpression)rightExpression).getLeftExpression() instanceof PyStringLiteralExpression &&
((PyBinaryExpression)rightExpression).getOperator() == PyTokenTypes.PERC);
}
}
@@ -0,0 +1 @@
<warning descr="Assignment can be replaced with augmented assignment">request = request + " FROM bugs WHERE bug_id = %s" % (str(bug_id))</warning>
@@ -0,0 +1 @@
request += " FROM bugs WHERE bug_id = %s" % (str(bug_id))
@@ -189,6 +189,11 @@ public class PyQuickFixTest extends PyLightFixtureTestCase {
PyBundle.message("QFIX.augment.assignment"), true, true);
}
public void testAugmentAssignmentPerc() { // PY-3197
doInspectionTest("AugmentAssignmentPerc.py", PyAugmentAssignmentInspection.class,
PyBundle.message("QFIX.augment.assignment"), true, true);
}
public void testChainedComparisons() { // PY-1020
doInspectionTest("ChainedComparisons.py", PyChainedComparisonsInspection.class,
PyBundle.message("QFIX.chained.comparison"), true, true);