Search in sources :

Example 1 with SourceLocation

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;
}
Also used : SourceLocation(org.autorefactor.jdt.internal.corext.dom.SourceLocation) LineComment(org.eclipse.jdt.core.dom.LineComment) Comment(org.eclipse.jdt.core.dom.Comment) BlockComment(org.eclipse.jdt.core.dom.BlockComment) BlockComment(org.eclipse.jdt.core.dom.BlockComment) NotImplementedException(org.autorefactor.util.NotImplementedException) Javadoc(org.eclipse.jdt.core.dom.Javadoc) ArrayList(java.util.ArrayList) List(java.util.List) LineComment(org.eclipse.jdt.core.dom.LineComment)

Example 2 with SourceLocation

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;
}
Also used : SourceLocation(org.autorefactor.jdt.internal.corext.dom.SourceLocation) Matcher(java.util.regex.Matcher) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite)

Example 3 with SourceLocation

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);
}
Also used : SourceLocation(org.autorefactor.jdt.internal.corext.dom.SourceLocation)

Example 4 with SourceLocation

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;
}
Also used : SourceLocation(org.autorefactor.jdt.internal.corext.dom.SourceLocation) Comment(org.eclipse.jdt.core.dom.Comment) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with SourceLocation

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;
}
Also used : SourceLocation(org.autorefactor.jdt.internal.corext.dom.SourceLocation) LineComment(org.eclipse.jdt.core.dom.LineComment) Comment(org.eclipse.jdt.core.dom.Comment) BlockComment(org.eclipse.jdt.core.dom.BlockComment) LineComment(org.eclipse.jdt.core.dom.LineComment) Pair(org.autorefactor.util.Pair)

Aggregations

SourceLocation (org.autorefactor.jdt.internal.corext.dom.SourceLocation)6 Comment (org.eclipse.jdt.core.dom.Comment)3 Matcher (java.util.regex.Matcher)2 ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)2 BlockComment (org.eclipse.jdt.core.dom.BlockComment)2 LineComment (org.eclipse.jdt.core.dom.LineComment)2 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 NotImplementedException (org.autorefactor.util.NotImplementedException)1 Pair (org.autorefactor.util.Pair)1 Javadoc (org.eclipse.jdt.core.dom.Javadoc)1