Search in sources :

Example 1 with LineComment

use of org.eclipse.jdt.core.dom.LineComment in project AutoRefactor by JnRouvignac.

the class ASTCommentRewriter method replaceLineCommentAfterJavaElement.

private void replaceLineCommentAfterJavaElement(List<TextEdit> commentEdits, LineComment lineComment, List<LineComment> lineComments, int i, String source, TreeSet<Integer> lineStarts) {
    if (i - 1 < 0) {
        throw new NotImplementedException(lineComment, "for a line comment situated after the java elements that it documents," + " and this line comment is not the last line comment to add to the javadoc.");
    }
    final LineComment previousLineComment = lineComments.get(i - 1);
    final int position = getEndPosition(previousLineComment);
    final String indent = getIndentForJavadoc(previousLineComment, source, lineStarts).substring(source);
    final StringBuilder newJavadoc = new StringBuilder().append(lineSeparator).append(indent).append(" *");
    appendCommentTextReplaceEndsOfBlockComment(newJavadoc, lineComment, source);
    newJavadoc.append(lineSeparator).append(indent).append(" */");
    commentEdits.add(new InsertEdit(position, newJavadoc.toString()));
    deleteLineCommentAfterNode(commentEdits, source, lineComment);
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) NotImplementedException(org.autorefactor.util.NotImplementedException) LineComment(org.eclipse.jdt.core.dom.LineComment)

Example 2 with LineComment

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

use of org.eclipse.jdt.core.dom.LineComment in project AutoRefactor by JnRouvignac.

the class ASTCommentRewriter method addSingleLineCommentToJavadocEdits.

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

Example 4 with LineComment

use of org.eclipse.jdt.core.dom.LineComment in project AutoRefactor by JnRouvignac.

the class ASTCommentRewriter method replaceLineCommentAfterJavaElement.

private void replaceLineCommentAfterJavaElement(final List<TextEdit> commentEdits, final LineComment lineComment, final List<LineComment> lineComments, final int i, final String source, final TreeSet<Integer> lineStarts) {
    if (i - 1 < 0) {
        throw new NotImplementedException(lineComment, // $NON-NLS-1$
        "for a line comment situated after the java elements that it documents," + // $NON-NLS-1$
        " and this line comment is not the last line comment to add to the javadoc.");
    }
    LineComment previousLineComment = lineComments.get(i - 1);
    int position = SourceLocation.getEndPosition(previousLineComment);
    String indent = getIndentForJavadoc(previousLineComment, source, lineStarts).substring(source);
    // $NON-NLS-1$
    StringBuilder newJavadoc = new StringBuilder(lineSeparator).append(indent).append(" *");
    appendCommentTextReplaceEndsOfBlockComment(newJavadoc, lineComment, source);
    // $NON-NLS-1$
    newJavadoc.append(lineSeparator).append(indent).append(" */");
    commentEdits.add(new InsertEdit(position, newJavadoc.toString()));
    deleteLineCommentAfterNode(commentEdits, source, lineComment);
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) NotImplementedException(org.autorefactor.util.NotImplementedException) LineComment(org.eclipse.jdt.core.dom.LineComment)

Example 5 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)

Aggregations

LineComment (org.eclipse.jdt.core.dom.LineComment)9 BlockComment (org.eclipse.jdt.core.dom.BlockComment)5 Comment (org.eclipse.jdt.core.dom.Comment)5 NotImplementedException (org.autorefactor.util.NotImplementedException)4 InsertEdit (org.eclipse.text.edits.InsertEdit)4 SourceLocation (org.autorefactor.refactoring.SourceLocation)3 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 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ASTNode (org.eclipse.jdt.core.dom.ASTNode)1 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)1