PY-24232 Remove escaping from "%" signs when converting printf-style formatted strings to f-strings

GitOrigin-RevId: bd19cad899cee5d93c518b76a990a51c7048442a
This commit is contained in:
Mikhail Golubev
2020-01-27 15:04:01 +00:00
committed by intellij-monorepo-bot
parent 539e56af64
commit 6b36fb041d
6 changed files with 29 additions and 10 deletions
@@ -82,15 +82,15 @@ public abstract class BaseConvertToFStringProcessor<T extends PyStringFormatPars
int offset = contentRange.getStartOffset();
for (final T chunk : extractTopLevelSubstitutionChunks()) {
// Preceding literal text
fStringText.append(stringText, offset, chunk.getStartIndex());
processLiteralChunk(stringText.substring(offset, chunk.getStartIndex()), fStringText);
if (!processSubstitutionChunk(chunk, fStringText)) {
return;
}
offset = chunk.getEndIndex();
if (!convertSubstitutionChunk(chunk, fStringText)) return;
}
if (offset < contentRange.getEndOffset()) {
fStringText.append(stringText, offset, contentRange.getEndOffset());
processLiteralChunk(stringText.substring(offset, contentRange.getEndOffset()), fStringText);
}
fStringText.append(myNodeInfo.getQuote());
@@ -115,7 +115,9 @@ public abstract class BaseConvertToFStringProcessor<T extends PyStringFormatPars
protected abstract boolean checkChunk(@NotNull T chunk);
protected abstract boolean convertSubstitutionChunk(@NotNull T chunk, @NotNull StringBuilder fStringText);
protected abstract boolean processSubstitutionChunk(@NotNull T chunk, @NotNull StringBuilder fStringText);
protected abstract void processLiteralChunk(@NotNull String chunk, @NotNull StringBuilder fStringText);
@Nullable
protected PsiElement adjustQuotesInsideInjectedExpression(@NotNull PsiElement expression) {
@@ -91,7 +91,7 @@ public class NewStyleConvertToFStringProcessor extends BaseConvertToFStringProce
}
@Override
protected boolean convertSubstitutionChunk(@NotNull Field field, @NotNull StringBuilder fStringText) {
protected boolean processSubstitutionChunk(@NotNull Field field, @NotNull StringBuilder fStringText) {
final String stringText = myPyString.getText();
@@ -128,7 +128,7 @@ public class NewStyleConvertToFStringProcessor extends BaseConvertToFStringProce
specOffset = nestedField.getFieldEnd();
// recursively format nested field
if (!convertSubstitutionChunk(nestedField, fStringText)) {
if (!processSubstitutionChunk(nestedField, fStringText)) {
return false;
}
}
@@ -142,6 +142,11 @@ public class NewStyleConvertToFStringProcessor extends BaseConvertToFStringProce
return true;
}
@Override
protected void processLiteralChunk(@NotNull String chunk, @NotNull StringBuilder fStringText) {
fStringText.append(chunk);
}
@Nullable
private String quoteItemsInFragments(@NotNull Field field) {
final List<String> escaped = new ArrayList<>();
@@ -85,7 +85,7 @@ public class OldStyleConvertToFStringProcessor extends BaseConvertToFStringProce
}
@Override
protected boolean convertSubstitutionChunk(@NotNull SubstitutionChunk subsChunk, @NotNull StringBuilder fStringText) {
protected boolean processSubstitutionChunk(@NotNull SubstitutionChunk subsChunk, @NotNull StringBuilder fStringText) {
final char conversionChar = subsChunk.getConversionType();
String widthAndPrecision = StringUtil.notNullize(subsChunk.getWidth());
@@ -133,4 +133,9 @@ public class OldStyleConvertToFStringProcessor extends BaseConvertToFStringProce
fStringText.append("}");
return true;
}
@Override
protected void processLiteralChunk(@NotNull String chunk, @NotNull StringBuilder fStringText) {
fStringText.append(chunk.replace("%%", "%"));
}
}
@@ -88,11 +88,16 @@ public class PyConvertToFStringIntentionTest extends PyIntentionTestCase {
public void testPercentOperatorExpressionContainsOriginalHostQuote() {
doNegativeTest();
}
public void testPercentOperatorExpressionContainsAlternativeHostQuote() {
doNegativeTest();
}
// PY-24232
public void testPercentOperatorRemovingEscapingFromPercentSigns() {
doTest();
}
public void testFormatMethodSimple() {
doTest();
}