diff --git a/python/src/com/jetbrains/python/codeInsight/intentions/ConvertFormatOperatorToMethodIntention.java b/python/src/com/jetbrains/python/codeInsight/intentions/ConvertFormatOperatorToMethodIntention.java index f66411819763..753d6db758b8 100644 --- a/python/src/com/jetbrains/python/codeInsight/intentions/ConvertFormatOperatorToMethodIntention.java +++ b/python/src/com/jetbrains/python/codeInsight/intentions/ConvertFormatOperatorToMethodIntention.java @@ -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 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; } diff --git a/python/testData/intentions/PyConvertFormatOperatorToMethodIntentionTest/starPrecision.py b/python/testData/intentions/PyConvertFormatOperatorToMethodIntentionTest/starPrecision.py new file mode 100644 index 000000000000..dbecdc8a1b9c --- /dev/null +++ b/python/testData/intentions/PyConvertFormatOperatorToMethodIntentionTest/starPrecision.py @@ -0,0 +1 @@ +s = '%.*s = %.*f' % (3, 'Gibberish', 3, 2.7182) \ No newline at end of file diff --git a/python/testData/intentions/PyConvertFormatOperatorToMethodIntentionTest/starWidth.py b/python/testData/intentions/PyConvertFormatOperatorToMethodIntentionTest/starWidth.py new file mode 100644 index 000000000000..289ab0d984db --- /dev/null +++ b/python/testData/intentions/PyConvertFormatOperatorToMethodIntentionTest/starWidth.py @@ -0,0 +1 @@ +s = '%*d' % (4, 5) \ No newline at end of file diff --git a/python/testData/intentions/PyConvertFormatOperatorToMethodIntentionTest/starWidthPrecision.py b/python/testData/intentions/PyConvertFormatOperatorToMethodIntentionTest/starWidthPrecision.py new file mode 100644 index 000000000000..5ee5040787d3 --- /dev/null +++ b/python/testData/intentions/PyConvertFormatOperatorToMethodIntentionTest/starWidthPrecision.py @@ -0,0 +1 @@ +s = '%*.*f' % (5, 2, 2.7182) \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/intentions/PyConvertFormatOperatorToMethodIntentionTest.java b/python/testSrc/com/jetbrains/python/intentions/PyConvertFormatOperatorToMethodIntentionTest.java index 62349ea11ad3..a8a09adb9f38 100644 --- a/python/testSrc/com/jetbrains/python/intentions/PyConvertFormatOperatorToMethodIntentionTest.java +++ b/python/testSrc/com/jetbrains/python/intentions/PyConvertFormatOperatorToMethodIntentionTest.java @@ -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")); } } \ No newline at end of file