Skip to content

Commit

Permalink
fixed bug for removing duplicate javadoc comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ThLeu committed Jun 13, 2018
1 parent 5c073b9 commit 2701eab
Showing 1 changed file with 37 additions and 14 deletions.
Expand Up @@ -156,13 +156,10 @@ public void concretePropertyChange(Node observedNode, ObservableProperty propert
nodeText.addToken(index + 1, eolTokenKind(), Utils.EOL);
} else if (newValue == null) {
if (oldValue instanceof JavadocComment) {
JavadocComment javadocComment = (JavadocComment) oldValue;
List<TokenTextElement> matchingTokens = nodeText.getElements().stream().filter(e -> e.isToken(JAVADOC_COMMENT)
&& ((TokenTextElement) e).getText().equals("/**" + javadocComment.getContent() + "*/")).map(e -> (TokenTextElement) e).collect(Collectors.toList());
if (matchingTokens.size() != 1) {
throw new IllegalStateException();
}
int index = nodeText.findElement(matchingTokens.get(0));
List<TokenTextElement> matchingTokens = getMatchingTokenTextElements((JavadocComment) oldValue, nodeText);

TokenTextElement matchingElement = matchingTokens.get(0);
int index = nodeText.findElement(matchingElement.and(matchingElement.matchByRange()));
nodeText.removeElement(index);
if (nodeText.getElements().get(index).isNewline()) {
nodeText.removeElement(index);
Expand All @@ -172,14 +169,11 @@ public void concretePropertyChange(Node observedNode, ObservableProperty propert
}
} else {
if (oldValue instanceof JavadocComment) {
JavadocComment oldJavadocComment = (JavadocComment) oldValue;
List<TokenTextElement> matchingTokens = nodeText.getElements().stream().filter(e -> e.isToken(JAVADOC_COMMENT)
&& ((TokenTextElement) e).getText().equals("/**" + oldJavadocComment.getContent() + "*/")).map(e -> (TokenTextElement) e).collect(Collectors.toList());
if (matchingTokens.size() != 1) {
throw new IllegalStateException();
}
List<TokenTextElement> matchingTokens = getMatchingTokenTextElements((JavadocComment) oldValue, nodeText);

JavadocComment newJavadocComment = (JavadocComment) newValue;
nodeText.replace(matchingTokens.get(0), new TokenTextElement(JAVADOC_COMMENT, "/**" + newJavadocComment.getContent() + "*/"));
TokenTextElement matchingElement = matchingTokens.get(0);
nodeText.replace(matchingElement.and(matchingElement.matchByRange()), new TokenTextElement(JAVADOC_COMMENT, "/**" + newJavadocComment.getContent() + "*/"));
} else {
throw new UnsupportedOperationException();
}
Expand All @@ -194,6 +188,35 @@ public void concretePropertyChange(Node observedNode, ObservableProperty propert
LEXICAL_DIFFERENCE_CALCULATOR.calculatePropertyChange(nodeText, observedNode, property, oldValue, newValue);
}

private List<TokenTextElement> getMatchingTokenTextElements(JavadocComment oldValue, NodeText nodeText) {
JavadocComment javadocComment = oldValue;
List<TokenTextElement> matchingTokens = nodeText.getElements().stream()
.filter(e -> e.isToken(JAVADOC_COMMENT))
.map(e -> (TokenTextElement) e)
.filter(t -> t.getText().equals("/**" + javadocComment.getContent() + "*/"))
.collect(Collectors.toList());

if (matchingTokens.size() > 1) {
// Duplicate comments found, refine the result
matchingTokens = matchingTokens.stream()
.filter(t -> isEqualRange(t.getToken().getRange(), javadocComment.getRange()))
.collect(Collectors.toList());
}

if (matchingTokens.size() != 1) {
throw new IllegalStateException("The matching JavadocComment to be removed / replaced could not be found");
}
return matchingTokens;
}

private boolean isEqualRange(Optional<Range> range1, Optional<Range> range2) {
if (range1.isPresent() && range2.isPresent()) {
return range1.get().equals(range2.get());
}

return false;
}

@Override
public void concreteListChange(NodeList changedList, AstObserver.ListChangeType type, int index, Node nodeAddedOrRemoved) {
NodeText nodeText = getOrCreateNodeText(changedList.getParentNodeForChildren());
Expand Down

0 comments on commit 2701eab

Please sign in to comment.