Search in sources :

Example 6 with LineComment

use of org.eclipse.jdt.core.dom.LineComment in project java-to-graphviz by randomnoun.

the class CommentExtractor method getComments.

/**
 * Return a list of processed comments from the source file.
 *
 * GvStyleComment:    "// gv-style: { xxx }"
 * GvKeepNodeComment: "// gv-keepNode: xxx"
 * GvLiteralComment:  "// gv-literal:  xxx"
 * GvDigraphComment:  "// gv-graph:    xxx"
 * GvSubgraphComment: "// gv-subgraph: xxx"
 * GvComment:         "// gv.className.className.className#id: xxx { xxx }"
 *
 * @param cu
 * @param src
 * @return
 */
@SuppressWarnings("unchecked")
public List<CommentText> getComments(CompilationUnit cu, String src) {
    // @TODO better regex
    // Pattern gvPattern = Pattern.compile("^gv(\\.[a-zA-Z]+)*:");  // gv.some.class.names:
    // probably do this right at the end as gv.literal affects how we parse it
    // Pattern valPattern = Pattern.compile("(([a-zA-Z]+)\\s*([^;]*);\\s*)*"); // things;separated;by;semicolons;
    List<CommentText> comments = new ArrayList<>();
    for (Comment c : (List<Comment>) cu.getCommentList()) {
        // comment.accept(cv);
        boolean eolComment = (c instanceof LineComment);
        int start = c.getStartPosition();
        int end = start + c.getLength();
        String text = src.substring(start, end);
        int line = cu.getLineNumber(start);
        int column = cu.getColumnNumber(start);
        if (c.isBlockComment()) {
            if (text.startsWith("/*") && text.endsWith("*/")) {
                text = text.substring(2, text.length() - 2).trim();
            } else {
                throw new IllegalStateException("Block comment does not start with '/*' and end with '*/':  '" + text + "'");
            }
        }
        if (c.isLineComment()) {
            if (text.startsWith("//")) {
                text = text.substring(2).trim();
            } else {
                throw new IllegalStateException("Line comment does not start with '//': '" + text + "'");
            }
        }
        if (text.startsWith("gv-style:")) {
            String s = text.substring(9).trim();
            if (s.startsWith("{") && s.endsWith("}")) {
                s = s.substring(1, s.length() - 1).trim();
                // here be the css
                // remove inline comments
                // logger.info("maybe here ? " +  s);
                comments.add(new GvStyleComment(c, line, column, eolComment, text, s));
            } else {
                throw new IllegalStateException("gv-style does not start with '{' and end with '}':  '" + text + "'");
            }
        } else if (text.startsWith("gv-endGraph")) {
            comments.add(new GvEndGraphComment(c, line, column, eolComment));
        } else if (text.startsWith("gv-endSubgraph")) {
            comments.add(new GvEndSubgraphComment(c, line, column, eolComment));
        } else if (text.startsWith("gv-literal:")) {
            String s = text.substring(11).trim();
            comments.add(new GvLiteralComment(c, line, column, eolComment, s));
        } else if (text.startsWith("gv-keepNode:")) {
            String s = text.substring(12).trim();
            comments.add(new GvKeepNodeComment(c, line, column, eolComment, s));
        } else if (text.startsWith("gv-option:")) {
            String s = text.substring(10).trim();
            comments.add(new GvOptionComment(c, line, column, eolComment, s));
        } else {
            Matcher fgm;
            CommentText gvc = null;
            fgm = gvGraphClassPattern.matcher(text);
            if (fgm.find()) {
                gvc = getGvComment("gv-graph", c, line, column, fgm, text);
            } else {
                fgm = gvSubgraphClassPattern.matcher(text);
                if (fgm.find()) {
                    gvc = getGvComment("gv-subgraph", c, line, column, fgm, text);
                } else {
                    fgm = gvNodeClassPattern.matcher(text);
                    if (fgm.find()) {
                        gvc = getGvComment("gv", c, line, column, fgm, text);
                    } else {
                    // regular comment, ignore
                    }
                }
            }
            if (gvc != null) {
                comments.add(gvc);
            }
        }
    }
    return comments;
}
Also used : GvSubgraphComment(com.randomnoun.build.javaToGraphviz.comment.GvSubgraphComment) GvComment(com.randomnoun.build.javaToGraphviz.comment.GvComment) GvEndGraphComment(com.randomnoun.build.javaToGraphviz.comment.GvEndGraphComment) LineComment(org.eclipse.jdt.core.dom.LineComment) GvGraphComment(com.randomnoun.build.javaToGraphviz.comment.GvGraphComment) GvKeepNodeComment(com.randomnoun.build.javaToGraphviz.comment.GvKeepNodeComment) GvEndSubgraphComment(com.randomnoun.build.javaToGraphviz.comment.GvEndSubgraphComment) GvStyleComment(com.randomnoun.build.javaToGraphviz.comment.GvStyleComment) GvLiteralComment(com.randomnoun.build.javaToGraphviz.comment.GvLiteralComment) GvOptionComment(com.randomnoun.build.javaToGraphviz.comment.GvOptionComment) Comment(org.eclipse.jdt.core.dom.Comment) Matcher(java.util.regex.Matcher) CommentText(com.randomnoun.build.javaToGraphviz.comment.CommentText) ArrayList(java.util.ArrayList) GvEndSubgraphComment(com.randomnoun.build.javaToGraphviz.comment.GvEndSubgraphComment) LineComment(org.eclipse.jdt.core.dom.LineComment) GvKeepNodeComment(com.randomnoun.build.javaToGraphviz.comment.GvKeepNodeComment) GvEndGraphComment(com.randomnoun.build.javaToGraphviz.comment.GvEndGraphComment) GvStyleComment(com.randomnoun.build.javaToGraphviz.comment.GvStyleComment) GvOptionComment(com.randomnoun.build.javaToGraphviz.comment.GvOptionComment) GvLiteralComment(com.randomnoun.build.javaToGraphviz.comment.GvLiteralComment) ArrayList(java.util.ArrayList) List(java.util.List)

Example 7 with LineComment

use of org.eclipse.jdt.core.dom.LineComment 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)

Example 8 with LineComment

use of org.eclipse.jdt.core.dom.LineComment in project xtext-xtend by eclipse.

the class JavaASTFlattener method visit.

@Override
public boolean visit(final Block node) {
    this.appendToBuffer("{");
    this.increaseIndent();
    boolean _isEmpty = node.statements().isEmpty();
    boolean _not = (!_isEmpty);
    if (_not) {
        final Procedure2<ASTNode, Integer> _function = (ASTNode child, Integer counter) -> {
            this.appendLineWrapToBuffer();
            child.accept(this);
        };
        IterableExtensions.<ASTNode>forEach(node.statements(), _function);
    }
    ASTNode _root = node.getRoot();
    if ((_root instanceof CompilationUnit)) {
        ASTNode _root_1 = node.getRoot();
        final CompilationUnit cu = ((CompilationUnit) _root_1);
        final Function1<Comment, Boolean> _function_1 = (Comment it) -> {
            int _startPosition = it.getStartPosition();
            int _startPosition_1 = node.getStartPosition();
            int _length = node.getLength();
            int _plus = (_startPosition_1 + _length);
            return Boolean.valueOf((_startPosition < _plus));
        };
        final Consumer<Comment> _function_2 = (Comment it) -> {
            if ((!(it instanceof LineComment))) {
                this.appendLineWrapToBuffer();
            }
            it.accept(this);
            this.assignedComments.add(it);
        };
        IterableExtensions.<Comment>filter(this.unAssignedComments(cu), _function_1).forEach(_function_2);
    }
    this.decreaseIndent();
    this.appendLineWrapToBuffer();
    this.appendToBuffer("}");
    return false;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) Comment(org.eclipse.jdt.core.dom.Comment) BlockComment(org.eclipse.jdt.core.dom.BlockComment) LineComment(org.eclipse.jdt.core.dom.LineComment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) LineComment(org.eclipse.jdt.core.dom.LineComment)

Example 9 with LineComment

use of org.eclipse.jdt.core.dom.LineComment 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 10 with LineComment

use of org.eclipse.jdt.core.dom.LineComment 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)

Aggregations

LineComment (org.eclipse.jdt.core.dom.LineComment)11 Comment (org.eclipse.jdt.core.dom.Comment)6 BlockComment (org.eclipse.jdt.core.dom.BlockComment)5 NotImplementedException (org.autorefactor.util.NotImplementedException)4 InsertEdit (org.eclipse.text.edits.InsertEdit)4 ArrayList (java.util.ArrayList)3 SourceLocation (org.autorefactor.refactoring.SourceLocation)3 GvComment (com.randomnoun.build.javaToGraphviz.comment.GvComment)2 GvGraphComment (com.randomnoun.build.javaToGraphviz.comment.GvGraphComment)2 GvSubgraphComment (com.randomnoun.build.javaToGraphviz.comment.GvSubgraphComment)2 List (java.util.List)2 Matcher (java.util.regex.Matcher)2 SourceLocation (org.autorefactor.jdt.internal.corext.dom.SourceLocation)2 Pair (org.autorefactor.util.Pair)2 Javadoc (org.eclipse.jdt.core.dom.Javadoc)2 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)2 CommentText (com.randomnoun.build.javaToGraphviz.comment.CommentText)1 GvEndGraphComment (com.randomnoun.build.javaToGraphviz.comment.GvEndGraphComment)1 GvEndSubgraphComment (com.randomnoun.build.javaToGraphviz.comment.GvEndSubgraphComment)1 GvKeepNodeComment (com.randomnoun.build.javaToGraphviz.comment.GvKeepNodeComment)1