mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
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:
+6
-2
@@ -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));
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
'''
|
||||
%s
|
||||
''' % '"'
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
f'''
|
||||
{'"'}
|
||||
'''
|
||||
@@ -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]", "[.!:]", "[}]");
|
||||
|
||||
Reference in New Issue
Block a user