From 7f476d13025be79c0a2f1290d11538dac23ea6df Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Tue, 25 Oct 2016 19:50:36 +0300 Subject: [PATCH] PY-21161 Check that index inside .format() chunk doesn't contain quotes it will be wrapped into --- .../codeInsight/intentions/PyConvertToFStringIntention.java | 2 +- .../formatMethodIndexContainsHostAlternativeQuote.py | 1 + .../formatMethodIndexContainsHostAlternativeQuote_after.py | 1 + .../python/intentions/PyConvertToFStringIntentionTest.java | 4 ++++ 4 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 python/testData/intentions/PyConvertToFStringIntentionTest/formatMethodIndexContainsHostAlternativeQuote.py create mode 100644 python/testData/intentions/PyConvertToFStringIntentionTest/formatMethodIndexContainsHostAlternativeQuote_after.py diff --git a/python/src/com/jetbrains/python/codeInsight/intentions/PyConvertToFStringIntention.java b/python/src/com/jetbrains/python/codeInsight/intentions/PyConvertToFStringIntention.java index 1bdc2dc7efcb..b7a74852f6c8 100644 --- a/python/src/com/jetbrains/python/codeInsight/intentions/PyConvertToFStringIntention.java +++ b/python/src/com/jetbrains/python/codeInsight/intentions/PyConvertToFStringIntention.java @@ -366,7 +366,7 @@ public class PyConvertToFStringIntention extends PyBaseIntentionAction { continue; } final char quote = invertQuote(hostStringQuote); - if (indexText.indexOf('\'') >= 0 && indexText.indexOf(quote) >= 0) { + if (indexText.indexOf(hostStringQuote) >= 0 || indexText.indexOf(quote) >= 0) { return null; } escaped.add("[" + quote + indexText + quote + "]"); diff --git a/python/testData/intentions/PyConvertToFStringIntentionTest/formatMethodIndexContainsHostAlternativeQuote.py b/python/testData/intentions/PyConvertToFStringIntentionTest/formatMethodIndexContainsHostAlternativeQuote.py new file mode 100644 index 000000000000..e9ccdb92b45b --- /dev/null +++ b/python/testData/intentions/PyConvertToFStringIntentionTest/formatMethodIndexContainsHostAlternativeQuote.py @@ -0,0 +1 @@ +'{foo["]}'.format(foo=undefined) \ No newline at end of file diff --git a/python/testData/intentions/PyConvertToFStringIntentionTest/formatMethodIndexContainsHostAlternativeQuote_after.py b/python/testData/intentions/PyConvertToFStringIntentionTest/formatMethodIndexContainsHostAlternativeQuote_after.py new file mode 100644 index 000000000000..e9ccdb92b45b --- /dev/null +++ b/python/testData/intentions/PyConvertToFStringIntentionTest/formatMethodIndexContainsHostAlternativeQuote_after.py @@ -0,0 +1 @@ +'{foo["]}'.format(foo=undefined) \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/intentions/PyConvertToFStringIntentionTest.java b/python/testSrc/com/jetbrains/python/intentions/PyConvertToFStringIntentionTest.java index c79de4fd56aa..de1f4f3cb2d7 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 testFormatMethodIndexContainsHostAlternativeQuote() { + doTest(); + } + public void testPercentOperatorWidthAndPrecision() { doTest(); }