use of org.autorefactor.jdt.internal.corext.dom.SourceLocation in project AutoRefactor by JnRouvignac.
the class CommentsCleanUp method visit.
@Override
public boolean visit(final CompilationUnit visited) {
comments.clear();
this.astRoot = visited;
for (Comment comment : (List<Comment>) astRoot.getCommentList()) {
comments.add(Pair.of(new SourceLocation(comment), comment));
}
for (Comment comment : (List<Comment>) astRoot.getCommentList()) {
if (comment.isBlockComment()) {
BlockComment bc = (BlockComment) comment;
bc.accept(this);
} else if (comment.isLineComment()) {
LineComment lc = (LineComment) comment;
lc.accept(this);
} else if (comment.isDocComment()) {
Javadoc jc = (Javadoc) comment;
jc.accept(this);
} else {
throw new NotImplementedException(comment);
}
}
return true;
}
use of org.autorefactor.jdt.internal.corext.dom.SourceLocation in project AutoRefactor by JnRouvignac.
the class RemoveEmptyLinesCleanUp method maybeRemoveEmptyLines.
private boolean maybeRemoveEmptyLines(final String source, final int endOfLineIndex, final int newLineIndex) {
if (endOfLineIndex < newLineIndex) {
Matcher matcher = NEWLINE_PATTERN.matcher(source).region(endOfLineIndex, newLineIndex);
boolean isEqualToNewline = matcher.matches();
if (!isEqualToNewline && matcher.find() && matcher.end() < newLineIndex) {
SourceLocation toRemove = SourceLocation.fromPositions(matcher.end(), newLineIndex);
ASTRewrite rewrite = cuRewrite.getASTRewrite();
rewrite.remove(toRemove);
return true;
}
}
return false;
}
use of org.autorefactor.jdt.internal.corext.dom.SourceLocation in project AutoRefactor by JnRouvignac.
the class ObsoleteRemoveSemiColonCleanUp method putResult.
private void putResult(final String source, final int start, final int end, final Map<String, SourceLocation> results) {
SourceLocation sourceLoc = SourceLocation.fromPositions(start, end);
String s = sourceLoc.substring(source);
results.put(s, sourceLoc);
}
use of org.autorefactor.jdt.internal.corext.dom.SourceLocation in project AutoRefactor by JnRouvignac.
the class ObsoleteRemoveSemiColonCleanUp method getNonCommentsStrings.
private Map<String, SourceLocation> getNonCommentsStrings(final ASTNode visited, final int start, final int end) {
List<Comment> comments = filterCommentsInRange(start, end, visited.getRoot());
String source = cuRewrite.getSource(visited);
Map<String, SourceLocation> results = new LinkedHashMap<>();
if (comments.isEmpty()) {
putResult(source, start, end, results);
} else {
int nextStart = start;
for (Comment comment : comments) {
if (nextStart < comment.getStartPosition()) {
putResult(source, nextStart, comment.getStartPosition(), results);
}
nextStart = SourceLocation.getEndPosition(comment);
}
}
return results;
}
use of org.autorefactor.jdt.internal.corext.dom.SourceLocation in project AutoRefactor by JnRouvignac.
the class CommentsCleanUp method betterCommentExist.
private boolean betterCommentExist(final Comment comment, final ASTNode nodeWhereToAddJavadoc) {
if (hasJavadoc(nodeWhereToAddJavadoc)) {
return true;
}
SourceLocation nodeLoc = new SourceLocation(nodeWhereToAddJavadoc);
SourceLocation bestLoc = new SourceLocation(comment);
Comment bestComment = comment;
for (Iterator<Pair<SourceLocation, Comment>> iter = this.comments.iterator(); iter.hasNext(); ) {
Pair<SourceLocation, Comment> pair = iter.next();
SourceLocation newLoc = pair.getFirst();
Comment newComment = pair.getSecond();
if (newLoc.compareTo(bestLoc) < 0) {
// Since comments are visited in ascending order,
// we can forget this comment.
iter.remove();
continue;
}
if (nodeLoc.compareTo(newLoc) < 0) {
break;
}
if (bestLoc.compareTo(newLoc) < 0 && (!(newComment instanceof LineComment) || !(bestComment instanceof LineComment))) {
// New comment is a BlockComment or a Javadoc
bestLoc = newLoc;
bestComment = newComment;
continue;
}
}
return bestComment != null && bestComment != comment;
}
Aggregations