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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations