Java: make quick fix available to create text block from any string literal (IDEA-287070)

GitOrigin-RevId: c6393e03f710177d526f07e121a768a7eb5e0ad2
This commit is contained in:
Bas Leijdekkers
2024-01-12 10:40:53 +01:00
committed by intellij-monorepo-bot
parent 76e07d4b62
commit 019144e115
8 changed files with 32 additions and 10 deletions

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.psi;
import org.jetbrains.annotations.NotNull;
@@ -56,6 +56,7 @@ public final class BasicLiteralUtil {
while (indent < line.length() && Character.isWhitespace(line.charAt(indent))) indent++;
if (indent == line.length() && (i < lines.length - 1 || ignoreLastLine)) {
if (!preserveContent) lines[i] = "";
if (lines.length == 1) prefix = indent;
}
else if (indent < prefix) prefix = indent;
}

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2024 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.codeInsight.daemon.impl.analysis.HighlightingFeature;
@@ -79,18 +79,17 @@ public final class TextBlockMigrationInspection extends AbstractBaseJavaLocalIns
@Override
public void visitLiteralExpression(@NotNull PsiLiteralExpression expression) {
if (PsiUtil.skipParenthesizedExprUp(expression.getParent()) instanceof PsiPolyadicExpression) return;
boolean quickFixOnly = isOnTheFly && InspectionProjectProfileManager.isInformationLevel(getShortName(), expression);
if (!mySuggestLiteralReplacement && !quickFixOnly) return;
PsiLiteralExpression literal = getLiteralExpression(expression);
if (literal == null) return;
String text = literal.getText();
int newLineIdx = getNewLineIndex(text, 0);
if (newLineIdx != -1 && getNewLineIndex(text, newLineIdx + 1) != -1) {
if (mySuggestLiteralReplacement && newLineIdx != -1 && getNewLineIndex(text, newLineIdx + 1) != -1) {
boolean quickFixOnly = isOnTheFly && InspectionProjectProfileManager.isInformationLevel(getShortName(), expression);
holder.registerProblem(expression, quickFixOnly ? null : new TextRange(newLineIdx, newLineIdx + 2),
JavaBundle.message("inspection.text.block.migration.string.message"),
new ReplaceWithTextBlockFix());
}
else if (isOnTheFly && getQuoteIndex(text) != -1) {
else if (isOnTheFly) {
holder.registerProblem(expression,
JavaBundle.message("inspection.text.block.migration.string.message"),
ProblemHighlightType.INFORMATION, new ReplaceWithTextBlockFix());

View File

@@ -7,7 +7,8 @@ Reports <code>String</code> concatenations that can be simplified by replacing t
<li>Text blocks are not concatenated.</li>
</ul>
<p>
Use the <b>Apply to single string literals</b> option to suggest the fix for single literals containing line breaks.
Use the <b>Report single string literals</b> option to highlight single literals containing line breaks.
The quick-fix will still be available even when this option is disabled.
<p>
<p><b>Example:</b></p>
<pre><code>

View File

@@ -0,0 +1,10 @@
// "Replace with text block" "true-preview"
class TextBlockMigration {
void empty() {
String empty = """
""";
}
}

View File

@@ -0,0 +1,11 @@
// "Replace with text block" "true-preview"
class TextBlockMigration {
void literalWithNewLine() {
String foo = """
foo
bar""";
}
}

View File

@@ -1,4 +1,4 @@
// "Fix all 'Text block can be used' problems in file" "false"
// "Replace with text block" "true-preview"
class TextBlockMigration {

View File

@@ -1,4 +1,4 @@
// "Fix all 'Text block can be used' problems in file" "false"
// "Replace with text block" "true-preview"
class TextBlockMigration {

View File

@@ -743,7 +743,7 @@ inspection.text.block.backward.migration.name=Text block can be replaced with re
inspection.text.block.migration.string.message=String can be replaced with text block
inspection.text.block.migration.concatenation.message=Concatenation can be replaced with text block
inspection.text.block.migration.name=Text block can be used
inspection.text.block.migration.suggest.literal.replacement=Apply to single string literals
inspection.text.block.migration.suggest.literal.replacement=Report single string literals
inspection.string.template.migration.string.message=String can be replaced with template
inspection.string.template.migration.concatenation.message=Concatenation can be replaced with string template
inspection.string.template.migration.name=String template can be used