mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-19 21:11:28 +07:00
StringConcatenationInLoopsInspection: automatically create append chains
This commit is contained in:
@@ -4,6 +4,7 @@ public class Main {
|
||||
String test(String[] strings) {
|
||||
StringBuilder res = new StringBuilder();
|
||||
for (String s : strings) {
|
||||
res.append(s).append("\n");
|
||||
res.append(s);
|
||||
}
|
||||
return res.toString();
|
||||
|
||||
@@ -8,7 +8,12 @@ public class Main {
|
||||
if (/*before*/res.length() > 0) {
|
||||
res.append(", ");
|
||||
}
|
||||
res.append(s);
|
||||
if (s.contains("'")) {
|
||||
res.append('[' // bracket
|
||||
).append(s).append(']');
|
||||
} else {
|
||||
res.append(s);
|
||||
}
|
||||
}
|
||||
return res.toString();
|
||||
}
|
||||
|
||||
@@ -4,7 +4,8 @@ public class Main {
|
||||
String test(String[] strings) {
|
||||
String res = "";
|
||||
for (String s : strings) {
|
||||
res <caret>+= s;
|
||||
res <caret>+= s + "\n";
|
||||
res += s;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -7,7 +7,12 @@ public class Main {
|
||||
if (/*before*/!res/*within*/.isEmpty()) {
|
||||
res += ", ";
|
||||
}
|
||||
res = (res <caret>+ s);
|
||||
if (s.contains("'")) {
|
||||
res = (res <caret>+ '[' // bracket
|
||||
+ s + ']');
|
||||
} else {
|
||||
res = (res + s);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ package com.siyeh.ig.performance;
|
||||
|
||||
import com.intellij.codeInsight.PsiEquivalenceUtil;
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.codeInspection.util.ChangeToAppendUtil;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
@@ -439,9 +440,9 @@ public class StringConcatenationInLoopsInspection extends BaseInspection {
|
||||
}
|
||||
|
||||
private void replaceInAssignment(PsiVariable variable,
|
||||
List<PsiElement> results,
|
||||
PsiAssignmentExpression assignment,
|
||||
CommentTracker ct) {
|
||||
List<PsiElement> results,
|
||||
PsiAssignmentExpression assignment,
|
||||
CommentTracker ct) {
|
||||
PsiExpression rValue = PsiUtil.skipParenthesizedExprDown(assignment.getRExpression());
|
||||
if(assignment.getOperationTokenType().equals(JavaTokenType.EQ)) {
|
||||
if (rValue instanceof PsiPolyadicExpression &&
|
||||
@@ -451,9 +452,14 @@ public class StringConcatenationInLoopsInspection extends BaseInspection {
|
||||
if (operands.length > 1) {
|
||||
// s = s + ...;
|
||||
if (ExpressionUtils.isReferenceTo(operands[0], variable)) {
|
||||
ct.delete(concat.getTokenBeforeOperand(operands[1]), operands[0]);
|
||||
replaceAll(variable, rValue, results, ct);
|
||||
results.add(ct.replace(assignment, variable.getName() + ".append(" + ct.text(rValue) + ")"));
|
||||
StreamEx.iterate(operands[1], Objects::nonNull, PsiElement::getNextSibling).forEach(ct::markUnchanged);
|
||||
String text = rValue.getText().substring(operands[1].getStartOffsetInParent());
|
||||
PsiExpression added = JavaPsiFacade.getElementFactory(variable.getProject()).createExpressionFromText(text, assignment);
|
||||
replaceAll(variable, added, results, ct);
|
||||
StringBuilder replacement = ChangeToAppendUtil.buildAppendExpression(added, false, new StringBuilder(variable.getName()));
|
||||
if (replacement != null) {
|
||||
results.add(ct.replace(assignment, replacement.toString()));
|
||||
}
|
||||
return;
|
||||
}
|
||||
// s = ... + s;
|
||||
@@ -473,7 +479,15 @@ public class StringConcatenationInLoopsInspection extends BaseInspection {
|
||||
}
|
||||
if(assignment.getOperationTokenType().equals(JavaTokenType.PLUSEQ)) {
|
||||
// s += ...;
|
||||
results.add(ct.replace(assignment, variable.getName() + ".append(" + ((rValue == null) ? "" : ct.text(rValue)) + ")"));
|
||||
String replacement = "";
|
||||
if (rValue != null) {
|
||||
StringBuilder sb =
|
||||
ChangeToAppendUtil.buildAppendExpression(ct.markUnchanged(rValue), false, new StringBuilder(variable.getName()));
|
||||
if (sb != null) {
|
||||
replacement = sb.toString();
|
||||
}
|
||||
}
|
||||
results.add(ct.replace(assignment, replacement));
|
||||
} else if(assignment.getOperationTokenType().equals(JavaTokenType.EQ)) {
|
||||
results.add(ct.replace(assignment, variable.getName() + "=" + generateNewStringBuilder(rValue, ct)));
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.InspectionGadgetsFix;
|
||||
import com.siyeh.ig.psiutils.CommentTracker;
|
||||
import com.siyeh.ig.psiutils.ExpressionUtils;
|
||||
import com.siyeh.ig.psiutils.ParenthesesUtils;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
@@ -89,12 +90,13 @@ public class StringConcatenationInsideStringBufferAppendInspection extends BaseI
|
||||
}
|
||||
final PsiExpressionList argumentList = methodCallExpression.getArgumentList();
|
||||
final PsiExpression[] arguments = argumentList.getExpressions();
|
||||
final PsiExpression argument = arguments[0];
|
||||
CommentTracker ct = new CommentTracker();
|
||||
final PsiExpression argument = ct.markUnchanged(arguments[0]);
|
||||
final PsiExpression appendExpression = ChangeToAppendUtil.buildAppendExpression(qualifier, argument);
|
||||
if (appendExpression == null) {
|
||||
return;
|
||||
}
|
||||
methodCallExpression.replace(appendExpression);
|
||||
ct.replaceAndRestoreComments(methodCallExpression, appendExpression);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user