PY-20803 Disable 'Replace with str.format method call' for string with * character in chunks

This commit is contained in:
Valentina Kiryushkina
2016-09-28 14:36:09 +03:00
parent fc00d0e0fc
commit 9dea66d46b
5 changed files with 33 additions and 3 deletions
@@ -30,6 +30,7 @@ import com.intellij.util.IncorrectOperationException;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.PyNames;
import com.jetbrains.python.PyTokenTypes;
import com.jetbrains.python.inspections.PyStringFormatParser;
import com.jetbrains.python.psi.*;
import com.jetbrains.python.psi.impl.PyBuiltinCache;
import com.jetbrains.python.psi.impl.PyPsiUtils;
@@ -235,10 +236,21 @@ public class ConvertFormatOperatorToMethodIntention extends BaseIntentionAction
if (binaryExpression.getLeftExpression() instanceof PyStringLiteralExpression
&& binaryExpression.getOperator() == PyTokenTypes.PERC) {
final PyStringLiteralExpression str = (PyStringLiteralExpression)binaryExpression.getLeftExpression();
if (!(str.getText().length() > 0 && Character.toUpperCase(str.getText().charAt(0)) == 'B')) {
setText(PyBundle.message("INTN.replace.with.method"));
return true;
if ((str.getText().length() > 0 && Character.toUpperCase(str.getText().charAt(0)) == 'B')) {
return false;
}
final List<PyStringFormatParser.SubstitutionChunk> chunks =
PyStringFormatParser.filterSubstitutions(PyStringFormatParser.parsePercentFormat(binaryExpression.getLeftExpression().getText()));
for (PyStringFormatParser.SubstitutionChunk chunk : chunks) {
if ("*".equals(chunk.getWidth()) || "*".equals(chunk.getPrecision())) {
return false;
}
}
setText(PyBundle.message("INTN.replace.with.method"));
return true;
}
return false;
}
@@ -0,0 +1 @@
s = '%.*s = %.*f' % (3, 'Gibberish', 3, 2.7182)
@@ -0,0 +1 @@
s = '%*d' % (4, 5)
@@ -0,0 +1 @@
s = '%*.*f' % (5, 2, 2.7182)
@@ -67,5 +67,20 @@ public class PyConvertFormatOperatorToMethodIntentionTest extends PyIntentionTes
// PY-20800
public void testRepr() {
doTest(PyBundle.message("INTN.replace.with.method"), LanguageLevel.PYTHON26);
}
// PY-20803
public void testStarWidth() {
doNegativeTest(PyBundle.message("INTN.replace.with.method"));
}
// PY-20803
public void testStarPrecision() {
doNegativeTest(PyBundle.message("INTN.replace.with.method"));
}
// PY-20803
public void testStarWidthPrecision() {
doNegativeTest(PyBundle.message("INTN.replace.with.method"));
}
}