private String getSpaceAtStart(String source, final LineComment lineComment) { final char firstChar = source.charAt(lineComment.getStartPosition() + "//".length()); return !Character.isWhitespace(firstChar) ? " " : ""; }
public boolean visit(LineComment node) { int start = node.getStartPosition(); int end = start + node.getLength(); // source is a string representing your source code String comment = source.substring(start, end); System.out.println(comment); return true; }
private void replaceEndsOfBlockCommentFromCommentText( List<TextEdit> commentEdits, LineComment lineComment, String source) { final Matcher matcher = endsOfBlockCommentMatcher(lineComment, source, lineComment.getStartPosition()); while (matcher.find()) { commentEdits.add(new ReplaceEdit(matcher.start(), matcher.end() - matcher.start(), "* /")); } }
private void appendCommentTextReplaceEndsOfBlockComment(StringBuilder sb, LineComment lineComment, String source) { 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)); }
private int[] getOffsets() { if (this.offsets == null) { if (this.commentList != null) { int nComments= this.commentList.size(); // count the number of line comments int count= 0; for (int i= 0; i < nComments; i++) { Object curr= this.commentList.get(i); if (curr instanceof LineComment) { count++; } } // fill the offset table this.offsets= new int[count]; for (int i= 0, k= 0; i < nComments; i++) { Object curr= this.commentList.get(i); if (curr instanceof LineComment) { LineComment comment= (LineComment) curr; this.offsets[k++]= comment.getStartPosition() + comment.getLength(); } } } else { this.offsets= Util.EMPTY_INT_ARRAY; } } return this.offsets; }
private int[] getOffsets() { if (this.offsets == null) { if (this.commentList != null) { int nComments= this.commentList.size(); // count the number of line comments int count= 0; for (int i= 0; i < nComments; i++) { Object curr= this.commentList.get(i); if (curr instanceof LineComment) { count++; } } // fill the offset table this.offsets= new int[count]; for (int i= 0, k= 0; i < nComments; i++) { Object curr= this.commentList.get(i); if (curr instanceof LineComment) { LineComment comment= (LineComment) curr; this.offsets[k++]= comment.getStartPosition() + comment.getLength(); } } } else { this.offsets= Util.EMPTY_INT_ARRAY; } } return this.offsets; }
private int[] getOffsets() { if (this.offsets == null) { if (this.commentList != null) { int nComments= this.commentList.size(); // count the number of line comments int count= 0; for (int i= 0; i < nComments; i++) { Object curr= this.commentList.get(i); if (curr instanceof LineComment) { count++; } } // fill the offset table this.offsets= new int[count]; for (int i= 0, k= 0; i < nComments; i++) { Object curr= this.commentList.get(i); if (curr instanceof LineComment) { LineComment comment= (LineComment) curr; this.offsets[k++]= comment.getStartPosition() + comment.getLength(); } } } else { this.offsets= Util.EMPTY_INT_ARRAY; } } return this.offsets; }
private int[] getOffsets() { if (this.offsets == null) { if (this.commentList != null) { int nComments= this.commentList.size(); // count the number of line comments int count= 0; for (int i= 0; i < nComments; i++) { Object curr= this.commentList.get(i); if (curr instanceof LineComment) { count++; } } // fill the offset table this.offsets= new int[count]; for (int i= 0, k= 0; i < nComments; i++) { Object curr= this.commentList.get(i); if (curr instanceof LineComment) { LineComment comment= (LineComment) curr; this.offsets[k++]= comment.getStartPosition() + comment.getLength(); } } } else { this.offsets= Util.EMPTY_INT_ARRAY; } } return this.offsets; }
@Override public boolean visit(LineComment node) { _compilationUnit.addComment(new CSLineComment(node.getStartPosition(), getText(node.getStartPosition(), node .getLength()))); return false; }
private void addMultiLineCommentsToJavadocEdits(List<TextEdit> commentEdits, ASTNode node, List<LineComment> lineComments, String source, TreeSet<Integer> lineStarts) { for (int i = 0; i < lineComments.size(); i++) { final LineComment lineComment = lineComments.get(i); if (lineComment.getStartPosition() <= node.getStartPosition()) { replaceLineCommentBeforeJavaElement( commentEdits, lineComment, lineComments, i, source, lineStarts); } else { replaceLineCommentAfterJavaElement( commentEdits, lineComment, lineComments, i, source, lineStarts); } } }
private void deleteLineCommentAfterNode(List<TextEdit> commentEdits, String source, LineComment lineComment) { final int commentStart = lineComment.getStartPosition(); final int commentLength = lineComment.getLength(); final int nbWhiteSpaces = nbTrailingSpaces(source, commentStart); commentEdits.add(new DeleteEdit(commentStart - nbWhiteSpaces, nbWhiteSpaces + commentLength)); }
ASTNode clone0(AST target) { LineComment result = new LineComment(target); result.setSourceRange(getStartPosition(), getLength()); return result; }
@Override ASTNode clone0(AST target) { LineComment result = new LineComment(target); result.setSourceRange(getStartPosition(), getLength()); return result; }
ASTNode clone0(AST target) { LineComment result = new LineComment(target); result.setSourceRange(getStartPosition(), getLength()); return result; }
ASTNode clone0(AST target) { LineComment result = new LineComment(target); result.setSourceRange(getStartPosition(), getLength()); return result; }
ASTNode clone0(AST target) { LineComment result = new LineComment(target); result.setSourceRange(getStartPosition(), getLength()); return result; }
ASTNode clone0(AST target) { LineComment result = new LineComment(target); result.setSourceRange(getStartPosition(), getLength()); return result; }
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) + " */")); } }
private boolean isSameLineNumber(LineComment node, ASTNode previousNode) { final CompilationUnit cu = (CompilationUnit) previousNode.getRoot(); final int lineNb1 = cu.getLineNumber(node.getStartPosition()); final int lineNb2 = cu.getLineNumber(previousNode.getStartPosition()); return lineNb1 == lineNb2; }
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(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); } }