PY-21161 Don't flip quotes inside inlined expressions if it's not necessary

For instance, when inlining single-quoted string inside triple-quoted
string with the same type of quotes.
This commit is contained in:
Mikhail Golubev
2016-10-25 21:30:13 +03:00
parent a373a4d4ca
commit b5f9efb612
4 changed files with 16 additions and 2 deletions
@@ -148,13 +148,17 @@ public class PyConvertToFStringIntention extends PyBaseIntentionAction {
// Nest string contain the same type of quote as host string inside, and we cannot escape inside f-string -- retreat
final String content = info.getContent();
final char targetSingleQuote = flipQuote(hostQuote);
if (content.indexOf(hostQuote) >= 0 || content.indexOf(targetSingleQuote) >= 0) {
if (content.indexOf(hostQuote) >= 0) {
return null;
}
if (!info.isTerminated()) {
return null;
}
if (info.getSingleQuote() == hostQuote) {
if (info.getQuote().startsWith(hostInfo.getQuote())) {
if (content.indexOf(targetSingleQuote) >= 0) {
return null;
}
final String targetQuote = info.getQuote().replace(hostQuote, targetSingleQuote);
final String stringWithSwappedQuotes = info.getPrefix() + targetQuote + content + targetQuote;
final PsiElement replaced = literal.replace(generator.createStringLiteralAlreadyEscaped(stringWithSwappedQuotes));
@@ -120,6 +120,10 @@ public class PyConvertToFStringIntentionTest extends PyIntentionTestCase {
doTest();
}
public void testPercentOperatorInlineSingleQuotedStringInsideTripleQuotedString() {
doTest();
}
public void testExtractItemAndAttributeAccess() {
assertSameElements(PyConvertToFStringIntention.extractItemsAndAttributes("{0.foo.bar.baz}"), ".foo", ".bar", ".baz");
assertSameElements(PyConvertToFStringIntention.extractItemsAndAttributes("{0[foo][.!:][}]}"), "[foo]", "[.!:]", "[}]");