From 2fcedcd7bbd97fcd752b4165bb65a13df52aff72 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Thu, 25 May 2023 13:47:20 +0200 Subject: [PATCH] Revert "Drop `replaceIncludingComments`" This reverts commit 133c85da4f86a3d3965384acf4cdc24b3f50677b. --- .../bugpatterns/MemberOrdering.java | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MemberOrdering.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MemberOrdering.java index 52f72682bc9..b51c64eff93 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MemberOrdering.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MemberOrdering.java @@ -108,7 +108,7 @@ private static SuggestedFix swapMembersIncludingComments( getComments(classTree, correct, state).map(Tokens.Comment::getText), Stream.of(state.getSourceForNode(correct))) .collect(joining("\n")); - fix.merge(SuggestedFixes.replaceIncludingComments(state.getPath(), replacement, state)); + fix.merge(replaceIncludingComments(classTree, original, replacement, state)); } } return fix.build(); @@ -131,6 +131,43 @@ private static Stream getComments( .flatMap(List::stream); } + // XXX: From this point the code is just a fancy copy of `SuggestFixes.replaceIncludingComments` - + // if we cannot use existing solutions for this functionality, this one needs a big refactor. + + private static SuggestedFix replaceIncludingComments( + ClassTree classTree, Tree member, String replacement, VisitorState state) { + Optional previousMember = getPreviousMember(member, classTree); + ImmutableList tokens = + getTokensBeforeMember(classTree, member, state).collect(toImmutableList()); + + if (tokens.isEmpty()) { + return SuggestedFix.replace(member, replacement); + } + if (tokens.get(0).comments().isEmpty()) { + return SuggestedFix.replace(tokens.get(0).pos(), state.getEndPosition(member), replacement); + } + ImmutableList comments = + ImmutableList.sortedCopyOf( + Comparator.comparingInt(c -> c.getSourcePos(0)).reversed(), + tokens.get(0).comments()); + @Var int startPos = ASTHelpers.getStartPosition(member); + // This can happen for desugared expressions like `int a, b;`. + if (startPos < getStartTokenization(classTree, state, previousMember)) { + return SuggestedFix.emptyFix(); + } + // Delete backwards for comments which are not separated from our target by a blank line. + CharSequence sourceCode = state.getSourceCode(); + for (Tokens.Comment comment : comments) { + int endOfCommentPos = comment.getSourcePos(comment.getText().length() - 1); + CharSequence stringBetweenComments = sourceCode.subSequence(endOfCommentPos, startPos); + if (stringBetweenComments.chars().filter(c -> c == '\n').count() > 1) { + break; + } + startPos = comment.getSourcePos(0); + } + return SuggestedFix.replace(startPos, state.getEndPosition(member), replacement); + } + private static Optional getPreviousMember(Tree tree, ClassTree classTree) { @Var Tree previousMember = null; for (Tree member : classTree.getMembers()) {