Skip to content

Commit

Permalink
Fix #2024 Now supports removal of block and single line comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Kissling committed Jan 29, 2019
1 parent 3a1de78 commit 48339d6
Showing 1 changed file with 27 additions and 9 deletions.
Expand Up @@ -156,8 +156,8 @@ public void concretePropertyChange(Node observedNode, ObservableProperty propert
nodeText.addChild(index, (Comment) newValue); nodeText.addChild(index, (Comment) newValue);
nodeText.addToken(index + 1, eolTokenKind(), Utils.EOL); nodeText.addToken(index + 1, eolTokenKind(), Utils.EOL);
} else if (newValue == null) { } else if (newValue == null) {
if (oldValue instanceof JavadocComment) { if (oldValue instanceof Comment) {
List<TokenTextElement> matchingTokens = getMatchingTokenTextElements((JavadocComment) oldValue, nodeText); List<TokenTextElement> matchingTokens = getMatchingTokenTextElements((Comment) oldValue, nodeText);


TokenTextElement matchingElement = matchingTokens.get(0); TokenTextElement matchingElement = matchingTokens.get(0);
int index = nodeText.findElement(matchingElement.and(matchingElement.matchByRange())); int index = nodeText.findElement(matchingElement.and(matchingElement.matchByRange()));
Expand All @@ -175,7 +175,9 @@ public void concretePropertyChange(Node observedNode, ObservableProperty propert
JavadocComment newJavadocComment = (JavadocComment) newValue; JavadocComment newJavadocComment = (JavadocComment) newValue;
TokenTextElement matchingElement = matchingTokens.get(0); TokenTextElement matchingElement = matchingTokens.get(0);
nodeText.replace(matchingElement.and(matchingElement.matchByRange()), new TokenTextElement(JAVADOC_COMMENT, "/**" + newJavadocComment.getContent() + "*/")); nodeText.replace(matchingElement.and(matchingElement.matchByRange()), new TokenTextElement(JAVADOC_COMMENT, "/**" + newJavadocComment.getContent() + "*/"));
} else { }

else {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
} }
Expand All @@ -189,27 +191,43 @@ public void concretePropertyChange(Node observedNode, ObservableProperty propert
LEXICAL_DIFFERENCE_CALCULATOR.calculatePropertyChange(nodeText, observedNode, property, oldValue, newValue); LEXICAL_DIFFERENCE_CALCULATOR.calculatePropertyChange(nodeText, observedNode, property, oldValue, newValue);
} }


private List<TokenTextElement> getMatchingTokenTextElements(JavadocComment oldValue, NodeText nodeText) { private List<TokenTextElement> getMatchingTokenTextElements(Comment oldValue, NodeText nodeText) {
JavadocComment javadocComment = oldValue; List<TokenTextElement> matchingTokens;
List<TokenTextElement> matchingTokens = nodeText.getElements().stream()
if (oldValue instanceof JavadocComment) {
matchingTokens = nodeText.getElements().stream()
.filter(e -> e.isToken(JAVADOC_COMMENT)) .filter(e -> e.isToken(JAVADOC_COMMENT))
.map(e -> (TokenTextElement) e) .map(e -> (TokenTextElement) e)
.filter(t -> t.getText().equals("/**" + javadocComment.getContent() + "*/")) .filter(t -> t.getText().equals("/**" + oldValue.getContent() + "*/"))
.collect(Collectors.toList());
} else if (oldValue instanceof BlockComment) {
matchingTokens = nodeText.getElements().stream()
.filter(e -> e.isToken(MULTI_LINE_COMMENT))
.map(e -> (TokenTextElement) e)
.filter(t -> t.getText().equals("/*" + oldValue.getContent() + "*/"))
.collect(Collectors.toList()); .collect(Collectors.toList());
} else {
matchingTokens = nodeText.getElements().stream()
.filter(e -> e.isToken(SINGLE_LINE_COMMENT))
.map(e -> (TokenTextElement) e)
.filter(t -> t.getText().equals("//" + oldValue.getContent() + System.lineSeparator()))
.collect(Collectors.toList());
}


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


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



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

0 comments on commit 48339d6

Please sign in to comment.