CommentTracker#replace(AndRestoreComments): mark replacement element as unchanged by default

This commit is contained in:
Tagir Valeev
2018-06-29 12:00:29 +07:00
parent d8566eaf48
commit 6de1320d04
16 changed files with 25 additions and 32 deletions
@@ -51,8 +51,7 @@ public class ClassGetClassInspection extends AbstractBaseJavaLocalInspectionTool
if (call == null) return;
PsiExpression qualifier = call.getMethodExpression().getQualifierExpression();
if (qualifier == null) return;
CommentTracker ct = new CommentTracker();
ct.replaceAndRestoreComments(call, ct.markUnchanged(qualifier));
new CommentTracker().replaceAndRestoreComments(call, qualifier);
}
}
@@ -243,13 +243,13 @@ public class InlineStreamMapAction extends PsiElementBaseIntentionAction {
}
ct.replace(e, replacement);
}
ct.replace(nextParameters[0], ct.markUnchanged(prevParameters[0]));
ct.replace(nextParameters[0], prevParameters[0]);
ExpressionUtils.bindReferenceTo(nextRef, newName);
PsiExpression prevQualifier = mapCall.getMethodExpression().getQualifierExpression();
if(prevQualifier == null) {
ct.deleteAndRestoreComments(nextQualifier);
} else {
ct.replaceAndRestoreComments(nextQualifier, ct.markUnchanged(prevQualifier));
ct.replaceAndRestoreComments(nextQualifier, prevQualifier);
}
CodeStyleManager.getInstance(project).reformat(lambda);
}
@@ -131,7 +131,7 @@ public class ObviousNullCheckInspection extends AbstractBaseJavaLocalInspectionT
}
ct.deleteAndRestoreComments(parent);
} else {
ct.replaceAndRestoreComments(call, ct.markUnchanged(startElement));
ct.replaceAndRestoreComments(call, startElement);
}
}
}
@@ -353,8 +353,7 @@ public class RedundantStreamOptionalCallInspection extends AbstractBaseJavaLocal
if (call == null) return;
PsiExpression qualifier = call.getMethodExpression().getQualifierExpression();
if (qualifier == null) return;
CommentTracker ct = new CommentTracker();
ct.replaceAndRestoreComments(call, ct.markUnchanged(qualifier));
new CommentTracker().replaceAndRestoreComments(call, qualifier);
}
}
@@ -1266,7 +1266,7 @@ public class SimplifyStreamApiCallChainsInspection extends AbstractBaseJavaLocal
ct.replace(arg, replacement);
}
ExpressionUtils.bindCallTo(qualifier, name);
return ct.replaceAndRestoreComments(call, ct.markUnchanged(qualifier));
return ct.replaceAndRestoreComments(call, qualifier);
}
static CallHandler<CallChainSimplification> handler() {
@@ -307,7 +307,7 @@ public class Java8MapApiInspection extends AbstractBaseJavaLocalInspectionTool {
LambdaCanBeMethodReferenceInspection.replaceLambdaWithMethodReference((PsiLambdaExpression)newArg);
}
if(PsiTreeUtil.isAncestor(conditional, result, true)) {
result = ct.replaceAndRestoreComments(conditional, ct.markUnchanged(result));
result = ct.replaceAndRestoreComments(conditional, result);
} else {
ct.deleteAndRestoreComments(conditional);
}
@@ -28,7 +28,6 @@ import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.Processor;
import com.intellij.util.containers.ContainerUtil;
import java.util.HashSet;
import com.siyeh.ig.callMatcher.CallMatcher;
import com.siyeh.ig.psiutils.*;
import one.util.streamex.StreamEx;
@@ -249,7 +248,7 @@ public class JoiningMigration extends BaseStreamApiMigration {
PsiMethodCallExpression nextCall = ExpressionUtils.getCallForQualifier(call);
PsiExpression qualifier = call.getMethodExpression().getQualifierExpression();
if (nextCall != null && qualifier != null) {
ct.replace(nextCall, ct.markUnchanged(qualifier));
ct.replace(nextCall, qualifier);
}
}
}
@@ -96,7 +96,6 @@ public class ConditionalCanBePushedInsideExpressionInspection extends BaseInspec
ParenthesesUtils.removeParentheses((PsiExpression)replacedConditionalExpression, false);
CommentTracker commentTracker = new CommentTracker();
commentTracker.markUnchanged(conditionalExpression.getCondition());
commentTracker.markUnchanged(thenExpression);
commentTracker.replaceAndRestoreComments(conditionalExpression, thenExpression);
}
}
@@ -104,7 +104,7 @@ public class ForLoopReplaceableByWhileInspection extends BaseInspection {
final PsiExpression whileCondition = whileStatement.getCondition();
if (forCondition != null) {
assert whileCondition != null;
commentTracker.replace(whileCondition, commentTracker.markUnchanged(forCondition));
commentTracker.replace(whileCondition, forCondition);
}
final PsiBlockStatement blockStatement = (PsiBlockStatement)whileStatement.getBody();
if (blockStatement == null) {
@@ -24,7 +24,6 @@ import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.PsiReplacementUtil;
import com.siyeh.ig.psiutils.*;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -89,8 +88,7 @@ public class ManualArrayCopyInspection extends BaseInspection {
if (Boolean.TRUE.equals(DfaUtil.evaluateCondition(ifStatement.getCondition()))) {
PsiStatement copyStatement = ControlFlowUtils.stripBraces(ifStatement.getThenBranch());
assert copyStatement != null;
CommentTracker ct = new CommentTracker();
ct.replaceAndRestoreComments(ifStatement, ct.markUnchanged(copyStatement));
new CommentTracker().replaceAndRestoreComments(ifStatement, copyStatement);
}
}
@@ -20,7 +20,7 @@ import java.util.function.Predicate;
*
* @author Tagir Valeev
*/
public class CommentTracker {
public final class CommentTracker {
private final Set<PsiElement> ignoredParents = new HashSet<>();
private List<PsiComment> comments = new ArrayList<>();
@@ -167,10 +167,11 @@ public class CommentTracker {
* Replaces given PsiElement collecting all the comments inside it.
*
* @param element element to replace
* @param replacement replacement element
* @param replacement replacement element. It's also marked as unchanged (see {@link #markUnchanged(PsiElement)})
* @return the element which was actually inserted in the tree (either {@code replacement} or its copy)
*/
public @NotNull PsiElement replace(@NotNull PsiElement element, @NotNull PsiElement replacement) {
markUnchanged(replacement);
grabComments(element);
return element.replace(replacement);
}
@@ -201,7 +202,7 @@ public class CommentTracker {
* <p>After calling this method the tracker cannot be used anymore.</p>
*
* @param element element to replace
* @param replacement replacement element
* @param replacement replacement element. It's also marked as unchanged (see {@link #markUnchanged(PsiElement)})
* @return the element which was actually inserted in the tree (either {@code replacement} or its copy)
*/
public @NotNull PsiElement replaceAndRestoreComments(@NotNull PsiElement element, @NotNull PsiElement replacement) {
@@ -94,7 +94,7 @@ public class SimplifiableIfStatementInspection extends AbstractBaseJavaLocalInsp
if (!PsiTreeUtil.isAncestor(ifStatement, model.myElseBranch, true)) {
commentTracker.delete(model.myElseBranch);
}
PsiElement result = commentTracker.replaceAndRestoreComments(ifStatement, commentTracker.markUnchanged(model.myThenBranch));
PsiElement result = commentTracker.replaceAndRestoreComments(ifStatement, model.myThenBranch);
tryJoinDeclaration(result);
}
@@ -203,7 +203,7 @@ public class TrivialFunctionalExpressionUsageInspection extends AbstractBaseJava
inlineCallArguments(callExpression, element, ct);
// body could be invalidated after inlining
expression = LambdaUtil.extractSingleExpressionFromBody(element.getBody());
ct.replaceAndRestoreComments(callExpression, ct.markUnchanged(expression));
ct.replaceAndRestoreComments(callExpression, expression);
}
private static void replaceCodeBlock(PsiLambdaExpression element) {
@@ -233,7 +233,7 @@ public class TrivialFunctionalExpressionUsageInspection extends AbstractBaseJava
}
final PsiExpression returnValue = statement == null ? null : statement.getReturnValue();
if (returnValue != null) {
ct.replaceAndRestoreComments(callExpression, ct.markUnchanged(returnValue));
ct.replaceAndRestoreComments(callExpression, returnValue);
}
else {
ct.deleteAndRestoreComments(callExpression);
@@ -154,7 +154,7 @@ public class ListRemoveInLoopInspection extends AbstractBaseJavaLocalInspectionT
if (Boolean.TRUE.equals(DfaUtil.evaluateCondition(condition))) {
PsiStatement nakedSubListClear = ControlFlowUtils.stripBraces(ifStatement.getThenBranch());
assert nakedSubListClear != null;
ct.replaceAndRestoreComments(ifStatement, ct.markUnchanged(nakedSubListClear));
ct.replaceAndRestoreComments(ifStatement, nakedSubListClear);
}
}
@@ -175,7 +175,7 @@ public class RedundantCollectionOperationInspection extends AbstractBaseJavaLoca
PsiPolyadicExpression conjunction = (PsiPolyadicExpression)parent;
PsiExpression[] conjuncts = conjunction.getOperands();
if (conjuncts.length == 2) {
ct.replaceAndRestoreComments(parent, ct.markUnchanged(conjuncts[0]));
ct.replaceAndRestoreComments(parent, conjuncts[0]);
} else {
PsiExpression lastConjunct = conjuncts[conjuncts.length-1];
PsiJavaToken token = conjunction.getTokenBeforeOperand(lastConjunct);
@@ -189,7 +189,7 @@ public class RedundantCollectionOperationInspection extends AbstractBaseJavaLoca
PsiIfStatement ifStatement = (PsiIfStatement)parent;
PsiExpressionStatement thenBody = tryCast(ControlFlowUtils.stripBraces(ifStatement.getThenBranch()), PsiExpressionStatement.class);
if (thenBody == null) return;
ct.replaceAndRestoreComments(ifStatement, ct.markUnchanged(thenBody));
ct.replaceAndRestoreComments(ifStatement, thenBody);
}
}
@@ -352,8 +352,7 @@ public class RedundantCollectionOperationInspection extends AbstractBaseJavaLoca
PsiExpression singletonArg = ArrayUtil.getFirstElement(singleton.getArgumentList().getExpressions());
if (singletonArg == null) return;
ExpressionUtils.bindCallTo(call, "contains");
CommentTracker ct = new CommentTracker();
ct.replaceAndRestoreComments(arg, ct.markUnchanged(singletonArg));
new CommentTracker().replaceAndRestoreComments(arg, singletonArg);
}
public static RedundantCollectionOperationHandler handler(PsiMethodCallExpression call) {
@@ -426,7 +425,7 @@ public class RedundantCollectionOperationInspection extends AbstractBaseJavaLoca
typeElement.replace(JavaPsiFacade.getElementFactory(project).createTypeElement(elementType.createArrayType()));
}
}
ct.replaceAndRestoreComments(call, ct.markUnchanged(args[0]));
ct.replaceAndRestoreComments(call, args[0]);
}
static RedundantAsListForIterationHandler handler(PsiMethodCallExpression call) {
@@ -480,9 +479,8 @@ public class RedundantCollectionOperationInspection extends AbstractBaseJavaLoca
if (myCollectionsSort) {
PsiMethodCallExpression outerCall = PsiTreeUtil.getParentOfType(call, PsiMethodCallExpression.class);
if (outerCall == null) return;
new CommentTracker().replaceAndRestoreComments(call, array);
CommentTracker ct = new CommentTracker();
ct.replaceAndRestoreComments(call, ct.markUnchanged(array));
ct = new CommentTracker();
ct.replaceAndRestoreComments(outerCall, sortMethod + ct.text(outerCall.getArgumentList()));
}
else {
@@ -239,7 +239,7 @@ public class RedundantStringOperationInspection extends AbstractBaseJavaLocalIns
CommentTracker ct = new CommentTracker();
switch (myFixType) {
case REPLACE_WITH_QUALIFIER: {
PsiExpression result = (PsiExpression)ct.replaceAndRestoreComments(call, ct.markUnchanged(qualifier));
PsiExpression result = (PsiExpression)ct.replaceAndRestoreComments(call, qualifier);
if (result.getParent() instanceof PsiExpressionStatement) {
extractSideEffects(result, (PsiExpressionStatement)result.getParent());
}
@@ -251,7 +251,7 @@ public class RedundantStringOperationInspection extends AbstractBaseJavaLocalIns
for (PsiExpression arg : call.getArgumentList().getExpressions()) {
list.add(ct.markUnchanged(arg));
}
ct.replaceAndRestoreComments(call, ct.markUnchanged(qualifier));
ct.replaceAndRestoreComments(call, qualifier);
break;
}
}