IDEA-383426 [java]: normalize strings with leading newlines when replacing with text block

GitOrigin-RevId: a31195746d6d797f217d13c7351179fa12adb6ae
This commit is contained in:
Bas Leijdekkers
2025-12-19 13:53:53 +00:00
committed by intellij-monorepo-bot
parent f335e07320
commit e4150e0d4e
3 changed files with 35 additions and 9 deletions
@@ -1,4 +1,4 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInspection;
import com.intellij.codeInspection.options.OptPane;
@@ -115,17 +115,15 @@ public final class TextBlockMigrationInspection extends AbstractBaseJavaLocalIns
if (expression == null) return;
Document document = expression.getContainingFile().getViewProvider().getDocument();
if (document == null) return;
PsiLiteralExpression literalExpression = tryCast(expression, PsiLiteralExpression.class);
if (literalExpression != null) {
replaceWithTextBlock(new PsiExpression[]{literalExpression}, literalExpression);
return;
if (expression instanceof PsiLiteralExpression literalExpression) {
replaceWithTextBlock(literalExpression, new PsiExpression[]{literalExpression});
}
else if (expression instanceof PsiPolyadicExpression polyadicExpression && ExpressionUtils.hasStringType(polyadicExpression)) {
replaceWithTextBlock(polyadicExpression, polyadicExpression.getOperands());
}
PsiPolyadicExpression polyadicExpression = tryCast(expression, PsiPolyadicExpression.class);
if (polyadicExpression == null || !ExpressionUtils.hasStringType(polyadicExpression)) return;
replaceWithTextBlock(polyadicExpression.getOperands(), polyadicExpression);
}
private static void replaceWithTextBlock(PsiExpression @NotNull [] operands, @NotNull PsiExpression toReplace) {
private static void replaceWithTextBlock(@NotNull PsiExpression toReplace, PsiExpression @NotNull [] operands) {
String[] lines = getContentLines(operands);
if (lines == null) return;
CommentTracker tracker = new CommentTracker();
@@ -139,6 +137,12 @@ public final class TextBlockMigrationInspection extends AbstractBaseJavaLocalIns
// append \ + newline at the end of the last line, so we can use closing """ to indent
lines[lines.length - 1] += "\\\n";
}
for (int i = 0; i < lines.length - 1; i++) {
String line = lines[i];
if (line.endsWith("\\\n") && lines[i + 1].equals("\n")) {
lines[i] = line.substring(0, line.length() - 2); // normalize strings with leading newlines
}
}
String content = StringUtil.join(lines);
if (content.endsWith(" ")) {
content = content.substring(0, content.length() - 1) + "\\s";
@@ -0,0 +1,12 @@
// "Replace with text block" "true-preview"
class LeadingNewlines {
public static void main(String[] args) {
String text = """
+---------------------------- ------------------------------------------------------------+
| Unable to authenticate the microservice. Administrative functions will not be available |
+-----------------------------------------------------------------------------------------+""";
}
}
@@ -0,0 +1,10 @@
// "Replace with text block" "true-preview"
class LeadingNewlines {
public static void main(String[] args) {
String text = "\n<caret>+---------------------------- ------------------------------------------------------------+" +
"\n| Unable to authenticate the microservice. Administrative functions will not be available |" +
"\n+-----------------------------------------------------------------------------------------+";
}
}