Search in sources :

Example 6 with SourceLocation

use of org.autorefactor.refactoring.SourceLocation in project AutoRefactor by JnRouvignac.

the class RemoveSemiColonRefactoring method getNonCommentsStrings.

private Map<String, SourceLocation> getNonCommentsStrings(ASTNode node, int start, int end) {
    final List<Comment> comments = filterCommentsInRange(start, end, node.getRoot());
    final String source = ctx.getSource(node);
    final LinkedHashMap<String, SourceLocation> results = new LinkedHashMap<String, SourceLocation>();
    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 = getEndPosition(comment);
        }
    }
    return results;
}
Also used : SourceLocation(org.autorefactor.refactoring.SourceLocation) Comment(org.eclipse.jdt.core.dom.Comment) LinkedHashMap(java.util.LinkedHashMap)

Example 7 with SourceLocation

use of org.autorefactor.refactoring.SourceLocation in project AutoRefactor by JnRouvignac.

the class CommentsRefactoring method visit.

@Override
public boolean visit(CompilationUnit node) {
    this.astRoot = node;
    for (Comment comment : getCommentList(astRoot)) {
        comments.add(Pair.of(new SourceLocation(comment), comment));
    }
    for (Comment comment : getCommentList(astRoot)) {
        if (comment.isBlockComment()) {
            final BlockComment bc = (BlockComment) comment;
            bc.accept(this);
        } else if (comment.isLineComment()) {
            final LineComment lc = (LineComment) comment;
            lc.accept(this);
        } else if (comment.isDocComment()) {
            final Javadoc jc = (Javadoc) comment;
            jc.accept(this);
        } else {
            throw new NotImplementedException(comment);
        }
    }
    return VISIT_SUBTREE;
}
Also used : SourceLocation(org.autorefactor.refactoring.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) LineComment(org.eclipse.jdt.core.dom.LineComment)

Example 8 with SourceLocation

use of org.autorefactor.refactoring.SourceLocation in project AutoRefactor by JnRouvignac.

the class CommentsRefactoring method betterCommentExist.

private boolean betterCommentExist(Comment comment, ASTNode nodeWhereToAddJavadoc) {
    if (hasJavadoc(nodeWhereToAddJavadoc)) {
        return true;
    }
    final SourceLocation nodeLoc = new SourceLocation(nodeWhereToAddJavadoc);
    SourceLocation bestLoc = new SourceLocation(comment);
    Comment bestComment = comment;
    for (Iterator<Pair<SourceLocation, Comment>> iter = this.comments.iterator(); iter.hasNext(); ) {
        final Pair<SourceLocation, Comment> pair = iter.next();
        final SourceLocation newLoc = pair.getFirst();
        final 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) {
            if (!(newComment instanceof LineComment)) {
                // new comment is a BlockComment or a Javadoc
                bestLoc = newLoc;
                bestComment = newComment;
                continue;
            } else if (!(bestComment instanceof LineComment)) {
                // new comment is a line comment and best comment is not
                bestLoc = newLoc;
                bestComment = newComment;
                continue;
            }
        }
    }
    return bestComment != null && bestComment != comment;
}
Also used : SourceLocation(org.autorefactor.refactoring.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)

Example 9 with SourceLocation

use of org.autorefactor.refactoring.SourceLocation in project AutoRefactor by JnRouvignac.

the class ASTCommentRewriter method replaceLineCommentBeforeJavaElement.

private void replaceLineCommentBeforeJavaElement(List<TextEdit> commentEdits, LineComment lineComment, List<LineComment> lineComments, int i, String source, TreeSet<Integer> lineStarts) {
    final int replaceLength = "//".length();
    final boolean isFirst = i == 0;
    String replacementText;
    final SourceLocation indentLoc = getIndentForJavadoc(lineComment, source, lineStarts);
    if (isFirst) {
        // TODO JNR how to obey configured indentation?
        replacementText = "/**" + lineSeparator + indentLoc.substring(source) + " *";
    } else {
        replacementText = " *";
    }
    final boolean commentStartsWithSlash = source.charAt(lineComment.getStartPosition() + replaceLength) == '/';
    if (commentStartsWithSlash) {
        replacementText += " ";
    }
    commentEdits.add(new ReplaceEdit(lineComment.getStartPosition(), replaceLength, replacementText));
    replaceEndsOfBlockCommentFromCommentText(commentEdits, lineComment, source);
    final boolean isLast = i == lineComments.size() - 1;
    if (isLast) {
        // TODO JNR how to obey configured indentation?
        final int position = getEndPosition(lineComment);
        commentEdits.add(new InsertEdit(position, lineSeparator + indentLoc.substring(source) + " */"));
    }
}
Also used : SourceLocation(org.autorefactor.refactoring.SourceLocation) InsertEdit(org.eclipse.text.edits.InsertEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit)

Example 10 with SourceLocation

use of org.autorefactor.refactoring.SourceLocation in project AutoRefactor by JnRouvignac.

the class ASTCommentRewriter method addSingleLineCommentToJavadocEdits.

private void addSingleLineCommentToJavadocEdits(List<TextEdit> commentEdits, ASTNode nextNode, List<LineComment> lineComments, String source, TreeSet<Integer> lineStarts) {
    final int nodeStart = nextNode.getStartPosition();
    final LineComment lineComment = lineComments.get(0);
    // TODO JNR how to obey configured indentation?
    // TODO JNR how to obey configured line length?
    final int commentStart = lineComment.getStartPosition();
    if (commentStart < nodeStart) {
        // assume comment is situated exactly before target node for javadoc
        final String spaceAtStart = getSpaceAtStart(source, lineComment);
        commentEdits.add(new ReplaceEdit(commentStart, "//".length(), "/**" + spaceAtStart));
        commentEdits.add(new InsertEdit(getEndPosition(lineComment), getSpaceAtEnd(source, lineComment) + "*/"));
        replaceEndsOfBlockCommentFromCommentText(commentEdits, lineComment, source);
    } else {
        // assume comment is situated exactly after target node for javadoc
        final StringBuilder newJavadoc = new StringBuilder().append("/**").append(getSpaceAtStart(source, lineComment));
        appendCommentTextReplaceEndsOfBlockComment(newJavadoc, lineComment, source);
        SourceLocation indent = getIndent(nextNode, lineStarts);
        newJavadoc.append(getSpaceAtEnd(source, lineComment)).append("*/").append(lineSeparator).append(source, indent.getStartPosition(), indent.getEndPosition());
        commentEdits.add(new InsertEdit(nodeStart, newJavadoc.toString()));
        deleteLineCommentAfterNode(commentEdits, source, lineComment);
    }
}
Also used : SourceLocation(org.autorefactor.refactoring.SourceLocation) InsertEdit(org.eclipse.text.edits.InsertEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) LineComment(org.eclipse.jdt.core.dom.LineComment)

Aggregations

SourceLocation (org.autorefactor.refactoring.SourceLocation)10 Matcher (java.util.regex.Matcher)3 Comment (org.eclipse.jdt.core.dom.Comment)3 LineComment (org.eclipse.jdt.core.dom.LineComment)3 BlockComment (org.eclipse.jdt.core.dom.BlockComment)2 InsertEdit (org.eclipse.text.edits.InsertEdit)2 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)2 LinkedHashMap (java.util.LinkedHashMap)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 NotImplementedException (org.autorefactor.util.NotImplementedException)1 Pair (org.autorefactor.util.Pair)1 Javadoc (org.eclipse.jdt.core.dom.Javadoc)1 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)1 TextEdit (org.eclipse.text.edits.TextEdit)1 TextEditVisitor (org.eclipse.text.edits.TextEditVisitor)1