Example usage for org.eclipse.jdt.core.dom LineComment getStartPosition

List of usage examples for org.eclipse.jdt.core.dom LineComment getStartPosition

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom LineComment getStartPosition.

Prototype

public final int getStartPosition() 

Source Link

Document

Returns the character index into the original source file indicating where the source fragment corresponding to this node begins.

Usage

From source file:ASTParser.ParseJavaFile.java

public boolean visit(LineComment node) {
    int start = node.getStartPosition();
    int end = start + node.getLength();
    String comment = source.substring(start, end);
    allComments.append(comment);//from  w  w w.ja va  2 s  . co  m
    allComments.append("\r\n");
    return true;
}

From source file:boa.datagen.util.Java7Visitor.java

License:Apache License

@Override
public boolean visit(LineComment node) {
    boa.types.Ast.Comment.Builder b = boa.types.Ast.Comment.newBuilder();
    buildPosition(node);//ww  w .ja  v  a 2 s  .  co  m
    b.setPosition(pos.build());
    b.setKind(boa.types.Ast.Comment.CommentKind.LINE);
    b.setValue(src.substring(node.getStartPosition(), node.getStartPosition() + node.getLength()));
    comments.add(b.build());
    return false;
}

From source file:br.uff.ic.gems.resources.ast.Visitor.java

@Override
public boolean visit(LineComment node) {
    int beginLine = cu.getLineNumber(node.getStartPosition());
    int endLine = cu.getLineNumber(node.getStartPosition() + node.getLength());
    int beginColumn = cu.getColumnNumber(node.getStartPosition());
    int endColumn = cu.getColumnNumber(node.getStartPosition() + node.getLength());

    languageConstructs.add(/*  w  w w  .j  a  v a2  s. c  om*/
            new LanguageConstruct(node.getClass().getSimpleName(), beginLine, endLine, beginColumn, endColumn));

    return true;
}

From source file:com.github.lbroudoux.dsl.eip.parser.camel.CamelJavaFileParser.java

License:Apache License

@Override
public boolean visit(LineComment node) {
    commentMap.put(routeCU.getLineNumber(node.getStartPosition()), getCommentContent(node));
    return true;/*from w w w  . jav a 2  s.c o m*/
}

From source file:edu.buffalo.cse.Sapphire.JavaModelListener.java

License:Open Source License

/**
 * This function deals with the changes of comments since comments can't 
 * be obtained via ASTNodes, it has to be done by accepting ASTVisitor.
 * /*from w  w  w  .j  av  a 2 s .  c  om*/
 * @author Chern Yee Chua
 */

@SuppressWarnings("unchecked")
public static void parse(final String strA) {
    ASTParser parserA = ASTParser.newParser(AST.JLS8);
    parserA.setSource(strA.toCharArray());
    parserA.setKind(ASTParser.K_COMPILATION_UNIT);
    final CompilationUnit cuA = (CompilationUnit) parserA.createAST(null);

    for (Comment comment : (List<Comment>) cuA.getCommentList()) {
        comment.accept(new ASTVisitor() {

            public boolean visit(LineComment node) {
                int start = node.getStartPosition();
                int end = start + node.getLength();
                String comment = strA.substring(start, end);
                int lineNumber;

                if (isTempLarger) {
                    lineNumber = astRootTemp.getLineNumber(node.getStartPosition());
                } else {
                    lineNumber = astRoot.getLineNumber(node.getStartPosition());
                }

                if (isTempLarger) {
                    if (!commentListTemp.contains(lineNumber + " # " + "[LINE_COMMENT] " + comment)) {
                        commentListTemp.add(lineNumber + " # " + "[LINE_COMMENT] " + comment);
                        commentListTempContent.add("[LINE_COMMENT] " + comment);
                    }
                } else {
                    if (!commentList.contains(lineNumber + " # " + "[LINE_COMMENT] " + comment)) {
                        commentList.add(lineNumber + " # " + "[LINE_COMMENT] " + comment);
                        commentListContent.add("[LINE_COMMENT] " + comment);
                    }
                }

                return true;
            }

            public boolean visit(BlockComment node) {
                int start = node.getStartPosition();
                int end = start + node.getLength();
                String comment = strA.substring(start, end);
                int lineNumber;

                if (isTempLarger) {
                    lineNumber = astRootTemp.getLineNumber(node.getStartPosition());
                } else {
                    lineNumber = astRoot.getLineNumber(node.getStartPosition());
                }

                if (isTempLarger) {
                    if (!commentListTemp.contains(lineNumber + " # " + "[BLOCK_COMMENT] \n" + comment)) {
                        commentListTemp.add(lineNumber + " # " + "[BLOCK_COMMENT] \n" + comment);
                        commentListTempContent.add("[BLOCK_COMMENT] \n" + comment);
                    }
                } else {
                    if (!commentList.contains(lineNumber + " # " + "[BLOCK_COMMENT] \n" + comment)) {
                        commentList.add(lineNumber + " # " + "[BLOCK_COMMENT] \n" + comment);
                        commentListContent.add("[BLOCK_COMMENT] \n" + comment);
                    }
                }
                return true;
            }

            public boolean visit(Javadoc node) {

                int start = node.getStartPosition();
                int end = start + node.getLength();
                String comment = strA.substring(start, end);
                int lineNumber;

                if (isTempLarger) {
                    lineNumber = astRootTemp.getLineNumber(node.getStartPosition());
                } else {
                    lineNumber = astRoot.getLineNumber(node.getStartPosition());
                }

                if (isTempLarger) {
                    if (!commentListTemp.contains(lineNumber + " # " + "[JAVADOC] \n" + comment)) {
                        commentListTemp.add(lineNumber + " # " + "[JAVADOC] \n" + comment);
                        commentListTempContent.add("[JAVADOC] \n" + comment);
                    }
                } else {
                    if (!commentList.contains(lineNumber + " # " + "[JAVADOC] \n" + comment)) {
                        commentList.add(lineNumber + " # " + "[JAVADOC] \n" + comment);
                        commentListContent.add("[JAVADOC] \n" + comment);
                    }
                }
                return true;
            }
        });
    }
}

From source file:edu.uci.ics.sourcerer.extractor.ast.ReferenceExtractorVisitor.java

License:Open Source License

/**
 * This method writes://from w ww  .  j a  v a2  s  .c o m
 *<ul>
 *  <li>For any line comment:
 *  <ul>
 *    <li>Comment to <code>ICommentWriter</code>.</li>
 *  </ul></li>
 *</ul>
 */
@Override
public boolean visit(LineComment node) {
    // Write the comment entity
    commentWriter.writeLineComment(compilationUnitPath, node.getStartPosition(), node.getLength());
    return true;
}

From source file:ko2.ic.sample.ast.Visitor.java

License:Open Source License

@Override
public boolean visit(LineComment node) {
    int startLineNumber = compilationUnit.getLineNumber(node.getStartPosition()) - 1;
    String lineComment = source[startLineNumber].trim();
    System.out.println(String.format("%d = %s", startLineNumber + 1, lineComment));
    return super.visit(node);
}

From source file:org.autorefactor.refactoring.ASTCommentRewriter.java

License:Open Source License

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//w  w  w  . ja  va2  s .  c om
                .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);
    }
}

From source file:org.autorefactor.refactoring.ASTCommentRewriter.java

License:Open Source License

private void appendCommentTextReplaceEndsOfBlockComment(StringBuilder sb, LineComment lineComment,
        String source) {//  ww  w. ja  va 2  s .c  o  m
    final int commentStart = lineComment.getStartPosition();
    int nextStart = commentStart + "//".length();
    final Matcher matcher = endsOfBlockCommentMatcher(lineComment, source, nextStart);
    while (matcher.find()) {
        sb.append(source, nextStart, matcher.start());
        sb.append("* /");
        nextStart = matcher.end();
    }
    if (source.charAt(nextStart) == '/') {
        sb.append(' ');
    }
    sb.append(source, nextStart, getEndPosition(lineComment));
}

From source file:org.autorefactor.refactoring.ASTCommentRewriter.java

License:Open Source License

private String getSpaceAtStart(String source, final LineComment lineComment) {
    final char firstChar = source.charAt(lineComment.getStartPosition() + "//".length());
    return !Character.isWhitespace(firstChar) ? " " : "";
}