StreamToLoopInspection: preserve comments; CommentTracker: now possible to delete element first and only after that register unchanged parts.

This commit is contained in:
Tagir Valeev
2016-12-23 18:00:01 +07:00
parent cd45987619
commit c9c574e428
20 changed files with 131 additions and 112 deletions

View File

@@ -97,7 +97,7 @@ abstract class FunctionHelper {
*/
void rename(String oldName, String newName, StreamToLoopReplacementContext context) {}
void registerUsedNames(Consumer<String> consumer) {}
void registerReusedElements(Consumer<PsiElement> consumer) {}
@Nullable
String getParameterName(int index) {
@@ -276,16 +276,6 @@ abstract class FunctionHelper {
return (PsiExpression)lambda.getBody();
}
static void processUsedNames(PsiElement start, Consumer<String> action) {
start.accept(new JavaRecursiveElementVisitor() {
@Override
public void visitVariable(PsiVariable variable) {
super.visitVariable(variable);
action.accept(variable.getName());
}
});
}
private static class MethodReferenceFunctionHelper extends FunctionHelper {
private final String myType;
private final String myQualifierType;
@@ -334,8 +324,8 @@ abstract class FunctionHelper {
}
@Override
void registerUsedNames(Consumer<String> consumer) {
processUsedNames(myMethodRef, consumer);
void registerReusedElements(Consumer<PsiElement> consumer) {
consumer.accept(myMethodRef);
}
@Override
@@ -452,8 +442,8 @@ abstract class FunctionHelper {
}
@Override
void registerUsedNames(Consumer<String> consumer) {
processUsedNames(myExpression, consumer);
void registerReusedElements(Consumer<PsiElement> consumer) {
consumer.accept(myExpression);
}
@Override
@@ -491,8 +481,8 @@ abstract class FunctionHelper {
}
@Override
void registerUsedNames(Consumer<String> consumer) {
processUsedNames(myReference, consumer);
void registerReusedElements(Consumer<PsiElement> consumer) {
consumer.accept(myReference);
}
}
@@ -563,8 +553,8 @@ abstract class FunctionHelper {
}
@Override
void registerUsedNames(Consumer<String> consumer) {
processUsedNames(myBody, consumer);
void registerReusedElements(Consumer<PsiElement> consumer) {
consumer.accept(myBody);
}
String getParameterName(int index) {

View File

@@ -16,6 +16,7 @@
package com.intellij.codeInspection.streamToLoop;
import com.intellij.codeInspection.streamToLoop.StreamToLoopInspection.StreamToLoopReplacementContext;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.PsiMethodCallExpression;
import com.intellij.psi.PsiType;
@@ -50,7 +51,7 @@ abstract class Operation {
return null;
}
public void registerUsedNames(Consumer<String> usedNameConsumer) {}
public void registerReusedElements(Consumer<PsiElement> consumer) {}
public void suggestNames(StreamVariable inVar, StreamVariable outVar) {}
@@ -100,8 +101,8 @@ abstract class Operation {
}
@Override
public void registerUsedNames(Consumer<String> usedNameConsumer) {
myFn.registerUsedNames(usedNameConsumer);
public void registerReusedElements(Consumer<PsiElement> consumer) {
myFn.registerReusedElements(consumer);
}
@Override
@@ -209,8 +210,8 @@ abstract class Operation {
}
@Override
public void registerUsedNames(Consumer<String> usedNameConsumer) {
myRecords.forEach(or -> or.myOperation.registerUsedNames(usedNameConsumer));
public void registerReusedElements(Consumer<PsiElement> consumer) {
myRecords.forEach(or -> or.myOperation.registerReusedElements(consumer));
}
@Override
@@ -268,8 +269,8 @@ abstract class Operation {
}
@Override
public void registerUsedNames(Consumer<String> usedNameConsumer) {
FunctionHelper.processUsedNames(myExpression, usedNameConsumer);
public void registerReusedElements(Consumer<PsiElement> consumer) {
consumer.accept(myExpression);
}
@Override
@@ -292,8 +293,8 @@ abstract class Operation {
}
@Override
public void registerUsedNames(Consumer<String> usedNameConsumer) {
FunctionHelper.processUsedNames(myLimit, usedNameConsumer);
public void registerReusedElements(Consumer<PsiElement> consumer) {
consumer.accept(myLimit);
}
@Override

View File

@@ -29,7 +29,6 @@ import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.function.Consumer;
import static com.intellij.codeInspection.streamToLoop.FunctionHelper.processUsedNames;
import static com.intellij.codeInspection.streamToLoop.FunctionHelper.replaceVarReference;
/**
@@ -77,7 +76,7 @@ abstract class SourceOperation extends Operation {
}
}
}
return new ExplicitSource(args);
return new ExplicitSource(call);
}
if (name.equals("generate") && args.length == 1 && method.getModifierList().hasExplicitModifier(
PsiModifier.STATIC) && className.startsWith("java.util.stream.")) {
@@ -113,8 +112,8 @@ abstract class SourceOperation extends Operation {
}
@Override
public void registerUsedNames(Consumer<String> usedNameConsumer) {
processUsedNames(myQualifier, usedNameConsumer);
public void registerReusedElements(Consumer<PsiElement> consumer) {
consumer.accept(myQualifier);
}
@Override
@@ -138,31 +137,34 @@ abstract class SourceOperation extends Operation {
}
static class ExplicitSource extends SourceOperation {
private PsiExpression[] myArgList;
private PsiMethodCallExpression myCall;
ExplicitSource(PsiExpression[] argList) {
myArgList = argList;
ExplicitSource(PsiMethodCallExpression call) {
myCall = call;
}
@Override
void rename(String oldName, String newName, StreamToLoopReplacementContext context) {
Arrays.asList(myArgList).replaceAll(arg -> replaceVarReference(arg, oldName, newName, context));
myCall = (PsiMethodCallExpression)replaceVarReference(myCall, oldName, newName, context);
}
@Override
public void registerUsedNames(Consumer<String> usedNameConsumer) {
for(PsiExpression arg : myArgList) {
processUsedNames(arg, usedNameConsumer);
}
public void registerReusedElements(Consumer<PsiElement> consumer) {
consumer.accept(myCall.getArgumentList());
}
@Override
public String wrap(StreamVariable outVar, String code, StreamToLoopReplacementContext context) {
String type = outVar.getType();
String args = StreamEx.of(myArgList).map(PsiExpression::getText).joining(", ");
// TODO: remove type argument if redundant
String collection =
TypeConversionUtil.isPrimitive(type) ? "new " + type + "[] {" + args + "}" : "java.util.Arrays.<" + type + ">asList(" + args + ")";
String collection;
PsiExpressionList argList = myCall.getArgumentList();
if (TypeConversionUtil.isPrimitive(type)) {
collection = StreamEx.of(argList.getChildren()).remove(child -> child.textMatches("(") || child.textMatches(")"))
.map(PsiElement::getText).joining("", "new " + type + "[] {", "}");
}
else {
collection = "java.util.Arrays.<" + type + ">asList" + argList.getText();
}
return context.getLoopLabel() +
"for(" + outVar.getDeclaration() + ": " + collection + ") {" + code + "}\n";
}
@@ -194,10 +196,10 @@ abstract class SourceOperation extends Operation {
}
@Override
public void registerUsedNames(Consumer<String> usedNameConsumer) {
myFn.registerUsedNames(usedNameConsumer);
public void registerReusedElements(Consumer<PsiElement> consumer) {
myFn.registerReusedElements(consumer);
if(myLimit != null) {
processUsedNames(myLimit, usedNameConsumer);
consumer.accept(myLimit);
}
}
@@ -232,9 +234,9 @@ abstract class SourceOperation extends Operation {
}
@Override
public void registerUsedNames(Consumer<String> usedNameConsumer) {
processUsedNames(myInitializer, usedNameConsumer);
myFn.registerUsedNames(usedNameConsumer);
public void registerReusedElements(Consumer<PsiElement> consumer) {
consumer.accept(myInitializer);
myFn.registerReusedElements(consumer);
}
@Override
@@ -269,9 +271,9 @@ abstract class SourceOperation extends Operation {
}
@Override
public void registerUsedNames(Consumer<String> usedNameConsumer) {
processUsedNames(myOrigin, usedNameConsumer);
processUsedNames(myBound, usedNameConsumer);
public void registerReusedElements(Consumer<PsiElement> consumer) {
consumer.accept(myOrigin);
consumer.accept(myBound);
}
@Override

View File

@@ -235,28 +235,29 @@ public class StreamToLoopInspection extends BaseJavaBatchLocalInspectionTool {
if(!(element instanceof PsiMethodCallExpression)) return;
PsiMethodCallExpression terminalCall = (PsiMethodCallExpression)element;
if(!isSupportedCodeLocation(terminalCall)) return;
PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
terminalCall = ensureCodeBlock(terminalCall, factory);
if (terminalCall == null) return;
PsiType resultType = terminalCall.getType();
if (resultType == null) return;
List<OperationRecord> operations = extractOperations(StreamVariable.STUB, terminalCall);
TerminalOperation terminal = getTerminal(operations);
if (terminal == null) return;
allOperations(operations).forEach(or -> or.myOperation.suggestNames(or.myInVar, or.myOutVar));
PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
terminalCall = ensureCodeBlock(terminalCall, factory);
if(terminalCall == null) return;
PsiStatement statement = PsiTreeUtil.getParentOfType(terminalCall, PsiStatement.class);
LOG.assertTrue(statement != null);
CommentTracker ct = new CommentTracker();
PsiExpression temporaryStreamPlaceholder =
(PsiExpression)terminalCall
.replace(factory.createExpressionFromText("((" + resultType.getCanonicalText() + ")$streamReplacement$)", terminalCall));
(PsiExpression)ct.replace(terminalCall, "((" + resultType.getCanonicalText() + ")$streamReplacement$)");
try {
StreamToLoopReplacementContext context =
new StreamToLoopReplacementContext(statement, operations, temporaryStreamPlaceholder);
new StreamToLoopReplacementContext(statement, operations, temporaryStreamPlaceholder, ct);
registerVariables(operations, context);
String replacement = "";
for (OperationRecord or : StreamEx.ofReversed(operations)) {
replacement = or.myOperation.wrap(or.myInVar, or.myOutVar, replacement, context);
}
ct.insertCommentsBefore(statement);
for (String declaration : context.getDeclarations()) {
addStatement(project, statement, factory.createStatementFromText(declaration, statement));
}
@@ -264,7 +265,6 @@ public class StreamToLoopInspection extends BaseJavaBatchLocalInspectionTool {
.getCodeBlock().getStatements()) {
addStatement(project, statement, addedStatement);
}
PsiElement result = context.makeFinalReplacement();
if(result != null) {
normalize(project, result);
@@ -333,7 +333,7 @@ public class StreamToLoopInspection extends BaseJavaBatchLocalInspectionTool {
}
private static void registerVariables(List<OperationRecord> operations, StreamToLoopReplacementContext context) {
allOperations(operations).map(or -> or.myOperation).forEach(op -> op.registerUsedNames(context::addUsedVar));
allOperations(operations).map(or -> or.myOperation).forEach(op -> op.registerReusedElements(context::registerReusedElement));
allOperations(operations).map(or -> or.myInVar).distinct().forEach(var -> var.register(context));
}
}
@@ -362,17 +362,22 @@ public class StreamToLoopInspection extends BaseJavaBatchLocalInspectionTool {
private final Set<String> myUsedNames;
private final Set<String> myUsedLabels;
private final List<String> myDeclarations = new ArrayList<>();
private final CommentTracker myCommentTracker;
private PsiElement myPlaceholder;
private final PsiElementFactory myFactory;
private String myLabel;
private String myFinisher;
StreamToLoopReplacementContext(PsiStatement statement, List<OperationRecord> records, @NotNull PsiExpression placeholder) {
StreamToLoopReplacementContext(PsiStatement statement,
List<OperationRecord> records,
@NotNull PsiExpression placeholder,
CommentTracker ct) {
myStatement = statement;
myFactory = JavaPsiFacade.getElementFactory(myStatement.getProject());
myHasNestedLoops = records.stream().anyMatch(or -> or.myOperation instanceof FlatMapOperation);
myPlaceholder = placeholder;
mySuffix = myHasNestedLoops ? "Outer" : "";
myCommentTracker = ct;
myUsedNames = new HashSet<>();
myUsedLabels = StreamEx.iterate(statement, Objects::nonNull, PsiElement::getParent).select(PsiLabeledStatement.class)
.map(PsiLabeledStatement::getName).toSet();
@@ -384,12 +389,20 @@ public class StreamToLoopInspection extends BaseJavaBatchLocalInspectionTool {
myPlaceholder = null;
myStatement = parentContext.myStatement;
myFactory = parentContext.myFactory;
myCommentTracker = parentContext.myCommentTracker;
myHasNestedLoops = records.stream().anyMatch(or -> or.myOperation instanceof FlatMapOperation);
mySuffix = "Inner";
}
public void addUsedVar(String name) {
myUsedNames.add(name);
public void registerReusedElement(PsiElement element) {
element.accept(new JavaRecursiveElementVisitor() {
@Override
public void visitVariable(PsiVariable variable) {
super.visitVariable(variable);
myUsedNames.add(variable.getName());
}
});
myCommentTracker.markUnchanged(element);
}
@Nullable

View File

@@ -333,9 +333,9 @@ abstract class TerminalOperation extends Operation {
}
@Override
public void registerUsedNames(Consumer<String> usedNameConsumer) {
FunctionHelper.processUsedNames(myIdentity, usedNameConsumer);
myUpdater.registerUsedNames(usedNameConsumer);
public void registerReusedElements(Consumer<PsiElement> consumer) {
consumer.accept(myIdentity);
myUpdater.registerReusedElements(consumer);
}
@Override
@@ -356,8 +356,8 @@ abstract class TerminalOperation extends Operation {
}
@Override
public void registerUsedNames(Consumer<String> usedNameConsumer) {
myUpdater.registerUsedNames(usedNameConsumer);
public void registerReusedElements(Consumer<PsiElement> consumer) {
myUpdater.registerReusedElements(consumer);
}
@Override
@@ -397,9 +397,9 @@ abstract class TerminalOperation extends Operation {
}
@Override
public void registerUsedNames(Consumer<String> usedNameConsumer) {
mySupplier.registerUsedNames(usedNameConsumer);
myAccumulator.registerUsedNames(usedNameConsumer);
public void registerReusedElements(Consumer<PsiElement> consumer) {
mySupplier.registerReusedElements(consumer);
myAccumulator.registerReusedElements(consumer);
}
@Override
@@ -518,8 +518,8 @@ abstract class TerminalOperation extends Operation {
}
@Override
public void registerUsedNames(Consumer<String> usedNameConsumer) {
myFn.registerUsedNames(usedNameConsumer);
public void registerReusedElements(Consumer<PsiElement> consumer) {
myFn.registerReusedElements(consumer);
}
@Override
@@ -548,7 +548,7 @@ abstract class TerminalOperation extends Operation {
// Non-trivial finishers are not supported
default void transform(StreamToLoopReplacementContext context, String item) {}
default void suggestNames(StreamVariable inVar, StreamVariable outVar) {}
default void registerUsedNames(Consumer<String> usedNameConsumer) {}
default void registerReusedElements(Consumer<PsiElement> consumer) {}
String getSupplier();
String getAccumulator(String acc, String item);
@@ -581,8 +581,8 @@ abstract class TerminalOperation extends Operation {
}
@Override
public void registerUsedNames(Consumer<String> usedNameConsumer) {
mySupplier.registerUsedNames(usedNameConsumer);
public void registerReusedElements(Consumer<PsiElement> consumer) {
mySupplier.registerReusedElements(consumer);
}
@Override
@@ -693,9 +693,9 @@ abstract class TerminalOperation extends Operation {
}
@Override
public void registerUsedNames(Consumer<String> usedNameConsumer) {
public void registerReusedElements(Consumer<PsiElement> consumer) {
if(myComparator != null) {
myComparator.registerUsedNames(usedNameConsumer);
myComparator.registerReusedElements(consumer);
}
}
@@ -761,11 +761,11 @@ abstract class TerminalOperation extends Operation {
}
@Override
public void registerUsedNames(Consumer<String> usedNameConsumer) {
super.registerUsedNames(usedNameConsumer);
myKeyExtractor.registerUsedNames(usedNameConsumer);
myValueExtractor.registerUsedNames(usedNameConsumer);
if(myMerger != null) FunctionHelper.processUsedNames(myMerger, usedNameConsumer);
public void registerReusedElements(Consumer<PsiElement> consumer) {
super.registerReusedElements(consumer);
myKeyExtractor.registerReusedElements(consumer);
myValueExtractor.registerReusedElements(consumer);
if(myMerger != null) consumer.accept(myMerger);
}
@Override
@@ -829,10 +829,10 @@ abstract class TerminalOperation extends Operation {
}
@Override
public void registerUsedNames(Consumer<String> usedNameConsumer) {
super.registerUsedNames(usedNameConsumer);
myKeyExtractor.registerUsedNames(usedNameConsumer);
myCollector.registerUsedNames(usedNameConsumer);
public void registerReusedElements(Consumer<PsiElement> consumer) {
super.registerReusedElements(consumer);
myKeyExtractor.registerReusedElements(consumer);
myCollector.registerReusedElements(consumer);
}
@Override
@@ -868,9 +868,9 @@ abstract class TerminalOperation extends Operation {
}
@Override
public void registerUsedNames(Consumer<String> usedNameConsumer) {
myPredicate.registerUsedNames(usedNameConsumer);
myCollector.registerUsedNames(usedNameConsumer);
public void registerReusedElements(Consumer<PsiElement> consumer) {
myPredicate.registerReusedElements(consumer);
myCollector.registerReusedElements(consumer);
}
@Override
@@ -905,9 +905,9 @@ abstract class TerminalOperation extends Operation {
}
@Override
public void registerUsedNames(Consumer<String> usedNameConsumer) {
myMapper.registerUsedNames(usedNameConsumer);
myDownstream.registerUsedNames(usedNameConsumer);
public void registerReusedElements(Consumer<PsiElement> consumer) {
myMapper.registerReusedElements(consumer);
myDownstream.registerReusedElements(consumer);
}
@Override
@@ -1002,8 +1002,8 @@ abstract class TerminalOperation extends Operation {
}
@Override
public void registerUsedNames(Consumer<String> usedNameConsumer) {
myFn.registerUsedNames(usedNameConsumer);
public void registerReusedElements(Consumer<PsiElement> consumer) {
myFn.registerReusedElements(consumer);
}
@Override

View File

@@ -6,7 +6,7 @@ import java.util.stream.IntStream;
public class Main {
private static IntSummaryStatistics test() {
IntSummaryStatistics stat = new IntSummaryStatistics();
for (int i : new int[]{1, 2, 3}) {
for (int i : new int[]{1,/*two*/2,/*three*/3}) {
stat.accept(i);
}
return stat;

View File

@@ -7,9 +7,10 @@ import java.util.List;
public class Main {
public static List<String> test() {
/*limit*/
List<String> list = new ArrayList<>();
long limit = 20;
for (String x = ""; ; x = x + "a") {
for (String x = ""; ; x = x /* add "a" */ + "a") {
if (limit-- == 0) break;
list.add(x);
}

View File

@@ -11,6 +11,7 @@ public class Main {
for (Map.Entry<String, List<String>> e : strings.entrySet()) {
if (!e.getKey().isEmpty()) {
long count = e.getValue().stream().filter(new Predicate<String>() {
// we're inside anonymous class
@Override
public boolean test(String s) {
return e.getKey().equals(s);

View File

@@ -6,12 +6,13 @@ import java.util.stream.Collectors;
public class Main {
public static void test(List<String> strings) {
// and collect
Map<Boolean, Map<String, Integer>> map = new HashMap<>();
map.put(false, new HashMap<>());
map.put(true, new HashMap<>());
for (String string : strings) {
String s = string.trim();
if (map.get(s.length() > 2).put(((UnaryOperator<String>) x -> x).apply(s), s.length()) != null) {
String s = string/*trimming*/.trim();
if (map.get(s.length() /*too big!*/ > 2).put(((UnaryOperator<String>) /* cast is necessary here */ x -> x).apply(s), s.length()) != null) {
throw new IllegalStateException("Duplicate key");
}
}

View File

@@ -5,9 +5,10 @@ import java.util.function.LongSupplier;
public class Main {
private static void test(List<String> list) {
// and filter!
long count1 = 0;
for (String s : list) {
if (!s.isEmpty()) {
if (!s/* comment */.isEmpty()) {
count1++;
}
}

View File

@@ -6,7 +6,7 @@ import java.util.stream.Stream;
public class Main {
private static IntSummaryStatistics test() {
IntSummaryStatistics stat = new IntSummaryStatistics();
for (Integer i : new Integer[]{1, 2, 3}) {
for (Integer i : new Integer[] /*create array*/{1, 2, 3}) {
int i1 = i;
stat.accept(i1);
}

View File

@@ -8,7 +8,7 @@ import java.util.stream.Stream;
public class Main {
private static IntSummaryStatistics test() {
IntSummaryStatistics stat = new IntSummaryStatistics();
for (Supplier<Integer> sup : Arrays.<Supplier<Integer>>asList(() -> 1, () -> 2, () -> 3)) {
for (Supplier<Integer> sup : Arrays.<Supplier<Integer>>asList(() -> 1, /*between*/ () /*supply 2*/ -> 2, () -> 3)) {
int i = sup.get();
stat.accept(i);
}

View File

@@ -5,7 +5,7 @@ import java.util.stream.IntStream;
public class Main {
private static IntSummaryStatistics test() {
return IntStream.of(1,2,3).summaryStatist<caret>ics();
return IntStream.of(1,/*two*/2,/*three*/3).summaryStatist<caret>ics();
}
public static void main(String[] args) {

View File

@@ -6,7 +6,7 @@ import java.util.List;
public class Main {
public static List<String> test() {
return Stream.iterate("", x -> x + "a").limit(20).co<caret>llect(Collectors.toList());
return Stream.iterate("", x -> x /* add "a" */ + "a").limit(/*limit*/20).co<caret>llect(Collectors.toList());
}
public static void main(String[] args) {

View File

@@ -8,6 +8,7 @@ public class Main {
private static long test(Map<String, List<String>> strings) {
return strings.entrySet().stream().filter(s -> !s.getKey().isEmpty()).mapToLong(e -> e.getValue().stream().filter(new Predicate<String>() {
// we're inside anonymous class
@Override
public boolean test(String s) {
return e.getKey().equals(s);

View File

@@ -8,8 +8,10 @@ import java.util.stream.Collectors;
public class Main {
public static void test(List<String> strings) {
System.out.println(strings.stream().map(x -> x.trim()).colle<caret>ct(Collectors.partitioningBy(s -> s.length() > 2,
Collectors.toMap(s -> ((UnaryOperator<String>) x -> x).apply(s), String::length))));
System.out.println(strings.stream().map(x -> x/*trimming*/.trim()) // and collect
.colle<caret>ct(Collectors.partitioningBy(s -> s.length() /*too big!*/ > 2,
Collectors.toMap(s -> ((UnaryOperator<String>) /* cast is necessary here */ x -> x).apply(s),
String::length))));
}
public static void main(String[] args) {

View File

@@ -5,7 +5,8 @@ import java.util.function.LongSupplier;
public class Main {
private static void test(List<String> list) {
long count = list.stream().filter(s -> !s.isEmpty()).co<caret>unt();
long count = list.stream(). // and filter!
filter(s -> !s/* comment */.isEmpty()).co<caret>unt();
if(count > 10) {
LongSupplier sup = () -> count*2;
long result = sup.get();

View File

@@ -5,7 +5,7 @@ import java.util.stream.Stream;
public class Main {
private static IntSummaryStatistics test() {
return Stream.of(new Integer[] {1,2,3}).mapToInt(i -> i).summaryStatisti<caret>cs();
return Stream.of(new Integer[] /*create array*/{1,2,3}).mapToInt(i -> i).summaryStatisti<caret>cs();
}
public static void main(String[] args) {

View File

@@ -6,7 +6,7 @@ import java.util.stream.Stream;
public class Main {
private static IntSummaryStatistics test() {
return Stream.<Supplier<Integer>>of(() -> 1, () -> 2, () -> 3).mapToInt(sup -> sup.get()).summar<caret>yStatistics();
return Stream.<Supplier<Integer>>of(() -> 1, /*between*/ () /*supply 2*/ -> 2, () -> 3).mapToInt(sup -> sup.get()).summar<caret>yStatistics();
}
public static void main(String[] args) {

View File

@@ -205,9 +205,10 @@ public class CommentTracker {
PsiElement parent = anchor.getParent();
PsiElementFactory factory = JavaPsiFacade.getElementFactory(anchor.getProject());
for(PsiComment comment : comments) {
if (shouldIgnore(comment)) continue;
PsiElement added = parent.addBefore(factory.createCommentFromText(comment.getText(), anchor), anchor);
PsiElement prevSibling = added.getPrevSibling();
if(prevSibling instanceof PsiWhiteSpace) {
if (prevSibling instanceof PsiWhiteSpace) {
PsiWhiteSpace whiteSpaceBefore = (PsiWhiteSpace)prevSibling;
PsiElement prev = anchor.getPrevSibling();
if (prev instanceof PsiWhiteSpace) {
@@ -222,10 +223,14 @@ public class CommentTracker {
comments = null;
}
private boolean shouldIgnore(PsiComment comment) {
return ignoredParents.stream().anyMatch(p -> PsiTreeUtil.isAncestor(p, comment, false));
}
private void grabComments(PsiElement element) {
checkState();
for(PsiComment comment : PsiTreeUtil.collectElementsOfType(element, PsiComment.class)) {
if(ignoredParents.stream().noneMatch(parent -> PsiTreeUtil.isAncestor(parent, comment, false))) {
if (!shouldIgnore(comment)) {
comments.add(comment);
}
}