mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
PY-21245 Handle quotes in lookup items inside multiline string literals better
Namely, allow lookup items that contain quotes in some cases if the containing string literal is multiline, hence there is still a suitable type of quotes to wrap injected string literal into. The orignal error described in the issue has been fixed earlier as part of other changes for PY-21161.
This commit is contained in:
+12
-6
@@ -355,7 +355,7 @@ public class PyConvertToFStringIntention extends PyBaseIntentionAction {
|
||||
if (adjusted == null) return false;
|
||||
|
||||
newStringText.append(adjusted.getText());
|
||||
final String quotedAttrsAndItems = quoteItemsInFragments(field, stringInfo.getSingleQuote());
|
||||
final String quotedAttrsAndItems = quoteItemsInFragments(field, stringInfo);
|
||||
if (quotedAttrsAndItems == null) return false;
|
||||
|
||||
newStringText.append(quotedAttrsAndItems);
|
||||
@@ -396,7 +396,7 @@ public class PyConvertToFStringIntention extends PyBaseIntentionAction {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static String quoteItemsInFragments(@NotNull PyNewStyleStringFormatParser.Field field, char hostStringQuote) {
|
||||
private static String quoteItemsInFragments(@NotNull PyNewStyleStringFormatParser.Field field, @NotNull StringNodeInfo hostStringInfo) {
|
||||
List<String> escaped = new ArrayList<>();
|
||||
for (String part : field.getAttributesAndLookups()) {
|
||||
if (part.startsWith(".")) {
|
||||
@@ -411,11 +411,17 @@ public class PyConvertToFStringIntention extends PyBaseIntentionAction {
|
||||
escaped.add(part);
|
||||
continue;
|
||||
}
|
||||
final char quote = flipQuote(hostStringQuote);
|
||||
if (indexText.indexOf(hostStringQuote) >= 0 || indexText.indexOf(quote) >= 0) {
|
||||
return null;
|
||||
final char originalQuote = hostStringInfo.getSingleQuote();
|
||||
char targetQuote = flipQuote(originalQuote);
|
||||
// there are no escapes inside the fragment, so the lookup key cannot contain
|
||||
// the host string quote unless it's a multiline string literal
|
||||
if (indexText.indexOf(targetQuote) >= 0) {
|
||||
if (!hostStringInfo.isTripleQuoted() || indexText.indexOf(originalQuote) >= 0) {
|
||||
return null;
|
||||
}
|
||||
targetQuote = originalQuote;
|
||||
}
|
||||
escaped.add("[" + quote + indexText + quote + "]");
|
||||
escaped.add("[" + targetQuote + indexText + targetQuote + "]");
|
||||
}
|
||||
}
|
||||
return StringUtil.join(escaped, "");
|
||||
|
||||
+1
@@ -0,0 +1 @@
|
||||
'''{x["]}'''.format(x=d)
|
||||
+1
@@ -0,0 +1 @@
|
||||
f'''{d['"']}'''
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
'''
|
||||
{0["']}
|
||||
'''.format(d)
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
'''
|
||||
{0["']}
|
||||
'''.format(d)
|
||||
+1
@@ -0,0 +1 @@
|
||||
'''{x[']}'''.format(x=d)
|
||||
+1
@@ -0,0 +1 @@
|
||||
f'''{d["'"]}'''
|
||||
+17
-1
@@ -94,11 +94,27 @@ public class PyConvertToFStringIntentionTest extends PyIntentionTestCase {
|
||||
public void testFormatMethodItemAccess() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
|
||||
// PY-21245
|
||||
public void testFormatMethodIndexContainsHostAlternativeQuote() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
// PY-21245
|
||||
public void testFormatMethodIndexContainsQuoteOfMultilineHost() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
// PY-21245
|
||||
public void testFormatMethodIndexContainsAlternativeQuoteOfMultilineHost() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
// PY-21245
|
||||
public void testFormatMethodIndexContainsBothTypesOfQuotesInsideMultilineHost() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testFormatMethodIndexContainsBackslash() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user