Java: fix SIOOBE in "Escape trailing whitespace characters"

GitOrigin-RevId: 1f975c570b0192a94b651e219b517f742b3b0ba0
This commit is contained in:
Bas Leijdekkers
2024-01-28 15:39:38 +00:00
committed by intellij-monorepo-bot
parent f7a2d9ee0e
commit 41b6cfab0b
7 changed files with 34 additions and 5 deletions
@@ -200,13 +200,13 @@ public final class TrailingWhitespacesInTextBlockInspection extends AbstractBase
if (suffix.equals("\\{")) return result.append(line).toString();
line = line.substring(0, line.length() - suffix.length());
}
String transformed = lineTransformation.apply(line);
String transformed = line.isEmpty() ? line : lineTransformation.apply(line);
if (transformed == null) return null;
if (last && hasUnescapedLastQuote(transformed)) {
result.append(transformed, 0, transformed.length() - 1).append("\\\"");
}
else if (transformed.isEmpty()) {
result.append((line.length() < indent) ? line : " ".repeat(indent));
result.append((line.length() < indent) ? line : line.substring(0, indent));
} else {
result.append(transformed);
}
@@ -0,0 +1,9 @@
class EmptyLine {
void x(int a) {
System.out.println(STR."""
a\na "\{a}
\s""");
}
}
@@ -0,0 +1,9 @@
class EmptyLine {
void x(int a) {
System.out.println(STR."""
a\na "\{a}
<warning descr="Trailing whitespace characters inside text block"><caret> </warning>""");
}
}
@@ -1,12 +1,12 @@
class Foo {
void test() {
String colors = """
red
green
@@ -28,5 +28,10 @@ class Foo {
\u005C \"""";
String text14 = """
\u005C\u005C\u005C\u005C\"""";
String text15 = STR."""
"scary"\{}
"scary"\{}
"scary" \{}
""";
}
}
@@ -28,5 +28,10 @@ class Foo {
<error descr="Illegal escape character in string literal">\u005C</error> "<warning descr="Trailing whitespace characters inside text block"> </warning>""";
String text14 = """
\u005C\u005C\u005C\u005C"<warning descr="Trailing whitespace characters inside text block"> </warning>""";
String text15 = STR."""
"scary"\{}<warning descr="Trailing whitespace characters inside text block"> </warning>
"scary"\{}
"scary" \{}
""";
}
}
@@ -12,6 +12,7 @@ public class TrailingWhitespacesInTextBlockInspectionTest extends LightJavaInspe
public void testBadTextBlock() { doTest(); }
public void testBlankLine() { doTestAll(); }
public void testEmptyLine() { doTestEscape(); }
public void testEmptyLastLine() { doTestAll(); }
public void testOneLineBlock() { doTestAll(); }
public void testReplaceAtTheEnd() { doTestEscape(); }