diff --git a/python/src/com/jetbrains/python/codeInsight/intentions/PyConvertToFStringIntention.java b/python/src/com/jetbrains/python/codeInsight/intentions/PyConvertToFStringIntention.java index 4cc83d400dc0..5c8d677e5840 100644 --- a/python/src/com/jetbrains/python/codeInsight/intentions/PyConvertToFStringIntention.java +++ b/python/src/com/jetbrains/python/codeInsight/intentions/PyConvertToFStringIntention.java @@ -46,6 +46,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.List; +import static com.jetbrains.python.codeInsight.intentions.ConvertFormatOperatorToMethodIntention.convertFormatSpec; import static com.jetbrains.python.psi.PyUtil.as; /** @@ -221,13 +222,12 @@ public class PyConvertToFStringIntention extends PyBaseIntentionAction { else { final SubstitutionChunk subsChunk = (SubstitutionChunk)chunk; final char conversionChar = subsChunk.getConversionType(); - final String widthAndPrecision; - if (StringUtil.isNotEmpty(subsChunk.getWidth()) || StringUtil.isNotEmpty(subsChunk.getPrecision())) { - widthAndPrecision = StringUtil.notNullize(subsChunk.getWidth()) + "." + StringUtil.notNullize(subsChunk.getPrecision()); - } - else { - widthAndPrecision = ""; + + String widthAndPrecision = StringUtil.notNullize(subsChunk.getWidth()); + if (StringUtil.isNotEmpty(subsChunk.getPrecision())) { + widthAndPrecision += "." + subsChunk.getPrecision(); } + final String conversionFlags = subsChunk.getConversionFlags(); result.append("{"); @@ -251,10 +251,7 @@ public class PyConvertToFStringIntention extends PyBaseIntentionAction { result.append(":"); } - if (StringUtil.isNotEmpty(conversionFlags)) { - final String conversionStr = String.valueOf(conversionChar); - result.append(ConvertFormatOperatorToMethodIntention.convertFormatSpec(conversionFlags, widthAndPrecision, conversionStr)); - } + result.append(convertFormatSpec(StringUtil.notNullize(conversionFlags), widthAndPrecision, String.valueOf(conversionChar))); if (StringUtil.isNotEmpty(widthAndPrecision)) { result.append(widthAndPrecision); diff --git a/python/testData/intentions/PyConvertToFStringIntentionTest/percentOperatorWidthAndPrecision.py b/python/testData/intentions/PyConvertToFStringIntentionTest/percentOperatorWidthAndPrecision.py new file mode 100644 index 000000000000..1cedeb56097f --- /dev/null +++ b/python/testData/intentions/PyConvertToFStringIntentionTest/percentOperatorWidthAndPrecision.py @@ -0,0 +1 @@ +'%.5d %3.5d %3d %20s %-#d' % (1, 2, 3, 'spam', 4) diff --git a/python/testData/intentions/PyConvertToFStringIntentionTest/percentOperatorWidthAndPrecision_after.py b/python/testData/intentions/PyConvertToFStringIntentionTest/percentOperatorWidthAndPrecision_after.py new file mode 100644 index 000000000000..b1de66542e99 --- /dev/null +++ b/python/testData/intentions/PyConvertToFStringIntentionTest/percentOperatorWidthAndPrecision_after.py @@ -0,0 +1 @@ +f'{1:.5d} {2:3.5d} {3:3d} {"spam":>20} {4:<#d}' diff --git a/python/testSrc/com/jetbrains/python/intentions/PyConvertToFStringIntentionTest.java b/python/testSrc/com/jetbrains/python/intentions/PyConvertToFStringIntentionTest.java index 6ac0274d3d8a..c8f3ab629ec9 100644 --- a/python/testSrc/com/jetbrains/python/intentions/PyConvertToFStringIntentionTest.java +++ b/python/testSrc/com/jetbrains/python/intentions/PyConvertToFStringIntentionTest.java @@ -96,6 +96,10 @@ public class PyConvertToFStringIntentionTest extends PyIntentionTestCase { doTest(); } + public void testPercentOperatorWidthAndPrecision() { + doTest(); + } + public void testExtractItemAndAttributeAccess() { assertSameElements(PyConvertToFStringIntention.extractItemsAndAttributes("{0.foo.bar.baz}"), ".foo", ".bar", ".baz"); assertSameElements(PyConvertToFStringIntention.extractItemsAndAttributes("{0[foo][.!:][}]}"), "[foo]", "[.!:]", "[}]");