PY-73911: fix "the inserted app is double-quoted" issue

GitOrigin-RevId: e22cadbea69abbfaf312b67bce3a82b9190912b7
This commit is contained in:
Vladimir Lezhnev
2024-07-16 18:51:13 +02:00
committed by intellij-monorepo-bot
parent d969c2efbc
commit 3bcabcf4a0
2 changed files with 24 additions and 4 deletions

View File

@@ -33,11 +33,18 @@ public abstract class PyElementGenerator extends PyAstElementGenerator {
* @param destination where the literal is destined to; used to determine the encoding.
* @param unescaped the string
* @param preferUTF8 try to use UTF8 (would use ascii if false)
* @param preferDoubleQuotes try to use double/single quotes
* @return a newly created literal
*/
protected abstract PyStringLiteralExpression createStringLiteralFromString(@Nullable PsiFile destination,
@NotNull String unescaped,
boolean preferUTF8, boolean preferDoubleQuotes);
public abstract PyStringLiteralExpression createStringLiteralFromString(@Nullable PsiFile destination, String unescaped,
boolean preferUTF8);
public abstract PyStringLiteralExpression createStringLiteralFromString(@NotNull String unescaped, boolean preferDoubleQuotes);
public abstract PyStringLiteralExpression createStringLiteralFromString(@NotNull String unescaped);
public abstract PyStringLiteralExpression createStringLiteral(@NotNull StringLiteralExpression oldElement, @NotNull String unescaped);
@@ -50,7 +57,7 @@ public abstract class PyElementGenerator extends PyAstElementGenerator {
@NotNull
public abstract PyExpression createExpressionFromText(@NotNull LanguageLevel languageLevel, @NotNull String text) throws IncorrectOperationException;
@NotNull
public abstract PyPattern createPatternFromText(@NotNull LanguageLevel languageLevel, @NotNull String text) throws IncorrectOperationException;

View File

@@ -80,10 +80,11 @@ public final class PyElementGeneratorImpl extends PyElementGenerator {
@Override
public PyStringLiteralExpression createStringLiteralFromString(@Nullable PsiFile destination,
protected PyStringLiteralExpression createStringLiteralFromString(@Nullable PsiFile destination,
@NotNull String unescaped,
final boolean preferUTF8) {
boolean useDouble = !unescaped.contains("\"");
final boolean preferUTF8,
boolean preferDoubleQuotes) {
boolean useDouble = (!unescaped.contains("\"") && preferDoubleQuotes) || unescaped.contains("'");
boolean useMulti = unescaped.matches(".*(\r|\n).*");
String quotes;
if (useMulti) {
@@ -139,6 +140,18 @@ public final class PyElementGeneratorImpl extends PyElementGenerator {
return createStringLiteralAlreadyEscaped(buf.toString());
}
@Override
public PyStringLiteralExpression createStringLiteralFromString(@Nullable PsiFile destination,
@NotNull String unescaped,
final boolean preferUTF8) {
return createStringLiteralFromString(destination, unescaped, preferUTF8, true);
}
@Override
public PyStringLiteralExpression createStringLiteralFromString(@NotNull String unescaped, boolean preferDoubleQuotes) {
return createStringLiteralFromString(null, unescaped, true, preferDoubleQuotes);
}
@Override
public PyListLiteralExpression createListLiteral() {
final PsiFile dummyFile = createDummyFile(LanguageLevel.getDefault(), "[]");