[codeInsight] IDEA-113640 Provide intention to combine System.out.println(String.format(...)) into System.out.printf

This patch removes StringUtil.isEmpty("%n") from RedundantStringFormatCallInspection#joinWithNewlineToken as it is always true. In addition to that it also includes:
- inlining the getMethodNameRange method since the createProblemDescriptor method accepts a nullable parameter
- moving RedundantStringFormatCallInspection#joinWithNewlineToken inside of StringFormatToPrintfQuickFix to reduce its visibility scope

Signed-off-by: Nikita Eshkeev <nikita.eshkeev@jetbrains.com>

GitOrigin-RevId: e70f0b6be7eb158910016ac2bb31bb5667186c91
This commit is contained in:
Nikita Eshkeev
2020-05-04 23:13:31 +00:00
committed by intellij-monorepo-bot
parent 374bb1b021
commit 68f1da981c
2 changed files with 34 additions and 41 deletions
@@ -3,15 +3,16 @@ package com.intellij.psi.util;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.JavaTokenType;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiJavaToken;
import com.intellij.psi.PsiLiteralExpression;
import com.intellij.psi.tree.IElementType;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Objects;
public class PsiLiteralUtil {
@NonNls public static final String HEX_PREFIX = "0x";
@NonNls public static final String BIN_PREFIX = "0b";
@@ -73,9 +73,7 @@ public final class RedundantStringFormatCallInspection extends LocalInspectionTo
final PsiExpression formatValue = args.getExpressions()[0];
if (containsNewlineToken(formatValue)) return null;
final TextRange textRange = getMethodNameRange(call);
if (textRange == null) return null;
return myManager.createProblemDescriptor(call, textRange,
return myManager.createProblemDescriptor(call, getMethodNameRange(call),
InspectionGadgetsBundle.message("redundant.call.problem.descriptor"),
ProblemHighlightType.GENERIC_ERROR_OR_WARNING, myIsOnTheFly,
new ReplaceWithPrintFix());
@@ -84,9 +82,7 @@ public final class RedundantStringFormatCallInspection extends LocalInspectionTo
@Nullable
private ProblemDescriptor getRedundantStringFormatProblem(@NotNull final PsiMethodCallExpression call) {
if (isStringFormatCallRedundant(call)) {
final TextRange textRange = getMethodNameRange(call);
if (textRange == null) return null;
return myManager.createProblemDescriptor(call, textRange,
return myManager.createProblemDescriptor(call, getMethodNameRange(call),
InspectionGadgetsBundle.message("redundant.call.problem.descriptor"),
ProblemHighlightType.GENERIC_ERROR_OR_WARNING, myIsOnTheFly,
new RemoveRedundantStringFormatFix());
@@ -97,9 +93,7 @@ public final class RedundantStringFormatCallInspection extends LocalInspectionTo
if (!PRINTSTREAM_PRINT.test(printlnCall)) return null;
}
final TextRange textRange = getMethodNameRange(call);
if (textRange == null) return null;
return myManager.createProblemDescriptor(call, textRange,
return myManager.createProblemDescriptor(call, getMethodNameRange(call),
InspectionGadgetsBundle.message("redundant.call.problem.descriptor"),
ProblemHighlightType.GENERIC_ERROR_OR_WARNING, myIsOnTheFly,
new StringFormatToPrintfQuickFix(isPrintlnCall));
@@ -290,35 +284,33 @@ public final class RedundantStringFormatCallInspection extends LocalInspectionTo
}
}
@Contract(value = "null -> null; !null -> !null", pure = true)
private static PsiLiteralExpression joinWithNewlineToken(@Nullable final PsiLiteralExpression expression) {
if (expression == null) return null;
final Object value = expression.getValue();
if (value == null) return expression;
final StringBuilder newExpression = new StringBuilder();
final String leftText = value.toString();
if (expression.isTextBlock()) {
final String indent = StringUtil.repeat(" ", PsiLiteralUtil.getTextBlockIndent(expression));
newExpression.append("\"\"\"").append('\n').append(indent);
newExpression.append(leftText.replaceAll("\n", "\n" + indent));
newExpression.append("%n");
newExpression.append("\"\"\"");
}
else {
newExpression.append('"');
newExpression.append(StringUtil.escapeStringCharacters(leftText));
newExpression.append("%n");
newExpression.append('"');
}
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(expression.getProject());
return (PsiLiteralExpression)factory.createExpressionFromText(newExpression.toString(), null);
}
}
}
@Contract(value = "null -> null; !null -> !null", pure = true)
private static PsiLiteralExpression joinWithNewlineToken(@Nullable final PsiLiteralExpression expression) {
if (expression == null) return null;
if (StringUtil.isEmpty("%n")) return expression;
final Object value = expression.getValue();
if (value == null) return expression;
final StringBuilder newExpression = new StringBuilder();
final String leftText = value.toString();
if (expression.isTextBlock()) {
final String indent = StringUtil.repeat(" ", PsiLiteralUtil.getTextBlockIndent(expression));
newExpression.append("\"\"\"").append('\n').append(indent);
newExpression.append(leftText.replaceAll("\n", "\n" + indent));
newExpression.append("%n");
newExpression.append("\"\"\"");
}
else {
newExpression.append('"');
newExpression.append(StringUtil.escapeStringCharacters(leftText));
newExpression.append("%n");
newExpression.append('"');
}
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(expression.getProject());
return (PsiLiteralExpression)factory.createExpressionFromText(newExpression.toString(), null);
}
}