From 8635d354aa7738104c0e230a82c4b41726a41ff6 Mon Sep 17 00:00:00 2001 From: Artemiy Sartakov Date: Fri, 31 Jan 2020 12:55:54 +0700 Subject: [PATCH] JavaTextBlockMigrationPropertyTest: fixed escape sequences parsing 1. expect '\040' sequence not to be replaced with space if it is escaped 2. handle text block content that ends with backslashes correctly GitOrigin-RevId: e2bdcdd21e1e043992d62626df725a85981f26d7 --- .../TextBlockBackwardMigrationInspection.java | 10 +++---- .../JavaTextBlockMigrationPropertyTest.java | 30 ++++++++++++++++++- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/java/java-impl/src/com/intellij/codeInspection/TextBlockBackwardMigrationInspection.java b/java/java-impl/src/com/intellij/codeInspection/TextBlockBackwardMigrationInspection.java index 20f1ad607f61..c5e0bd2fb52e 100644 --- a/java/java-impl/src/com/intellij/codeInspection/TextBlockBackwardMigrationInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/TextBlockBackwardMigrationInspection.java @@ -88,10 +88,14 @@ public class TextBlockBackwardMigrationInspection extends AbstractBaseJavaLocalI while (i < length) { int nSlashes = 0; int next = i; - while ((next = PsiLiteralUtil.parseBackSlash(text, next)) != -1) { + while (next < length && (next = PsiLiteralUtil.parseBackSlash(text, next)) != -1) { nSlashes++; i = next; } + if (i >= length) { + result.append(StringUtil.repeatSymbol('\\', nSlashes)); + break; + } next = parseQuote(i, text, nSlashes, result); if (next != -1) { i = next; @@ -109,10 +113,6 @@ public class TextBlockBackwardMigrationInspection extends AbstractBaseJavaLocalI } private static int parseEscapedChar(int i, @NotNull String text, int nSlashes, @NotNull StringBuilder result) { - if (i > text.length()) { - result.append(StringUtil.repeatSymbol('\\', nSlashes)); - return i; - } int next = parseEscapedSpace(i, text, nSlashes, result); if (next != -1) return next; next = parseEscapedLineBreak(i, text, nSlashes, result); diff --git a/java/java-tests/testSrc/com/intellij/java/propertyBased/JavaTextBlockMigrationPropertyTest.java b/java/java-tests/testSrc/com/intellij/java/propertyBased/JavaTextBlockMigrationPropertyTest.java index 9405276e83c8..d36e76c32f45 100644 --- a/java/java-tests/testSrc/com/intellij/java/propertyBased/JavaTextBlockMigrationPropertyTest.java +++ b/java/java-tests/testSrc/com/intellij/java/propertyBased/JavaTextBlockMigrationPropertyTest.java @@ -100,7 +100,7 @@ public class JavaTextBlockMigrationPropertyTest extends LightJavaCodeInsightFixt if (injected != null && !injected.isEmpty()) continue; String expected = getConcatenationText(operands); if (expected == null || countNewLines(expected) < 2) continue; - expected = expected.replaceAll("\\\\040", " "); + expected = replaceUnescapedSpaces(expected); Computable replaceAction = () -> { PsiElementFactory factory = JavaPsiFacade.getInstance(getProject()).getElementFactory(); @@ -122,6 +122,34 @@ public class JavaTextBlockMigrationPropertyTest extends LightJavaCodeInsightFixt } } + @NotNull + private static String replaceUnescapedSpaces(@NotNull String text) { + if (!text.contains("040")) return text; + StringBuilder result = new StringBuilder(); + int i = 0; + int length = text.length(); + while (i < length) { + int nSlashes = 0; + int next; + while (i < length && (next = PsiLiteralUtil.parseBackSlash(text, i)) != -1) { + nSlashes++; + i = next; + } + if (i >= length) { + result.append(StringUtil.repeatSymbol('\\', nSlashes)); + break; + } + if (nSlashes % 2 != 0 && StringUtil.startsWith(text, i, "040")) { + result.append(StringUtil.repeatSymbol('\\', nSlashes - 1)).append(" "); + i += 3; + continue; + } + result.append(StringUtil.repeatSymbol('\\', nSlashes)).append(text.charAt(i)); + i++; + } + return result.toString(); + } + @Nullable private static String getConcatenationText(PsiExpression @NotNull [] operands) { String[] lines = new String[operands.length];