This repository was archived by the owner on Mar 8, 2020. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathCommentVisitor.java
More file actions
72 lines (50 loc) · 2.23 KB
/
Copy pathCommentVisitor.java
File metadata and controls
72 lines (50 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package bblfsh;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.BlockComment;
import org.eclipse.jdt.core.dom.Comment;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.Javadoc;
import org.eclipse.jdt.core.dom.LineComment;
public class CommentVisitor extends ASTVisitor {
CompilationUnit compilationUnit;
private String[] source;
private String commentText;
public CommentVisitor(CompilationUnit compilationUnit, String[] source) {
super();
this.compilationUnit = compilationUnit;
this.source = source;
}
public String getCommentText() {
return commentText;
}
public boolean visit(LineComment node) {
int startLineNumber = compilationUnit.getLineNumber(node.getStartPosition()) - 1;
int startLineColumn = compilationUnit.getColumnNumber(node.getStartPosition());
commentText = source[startLineNumber].substring(startLineColumn).trim();
return true;
}
public boolean visit(Javadoc node) {
return visitBlock(node);
}
public boolean visit(BlockComment node) {
return visitBlock(node);
}
private boolean visitBlock(Comment node) {
int startLineNumber = compilationUnit.getLineNumber(node.getStartPosition()) - 1;
int startLineColumn = compilationUnit.getColumnNumber(node.getStartPosition());
int endLineNumber = compilationUnit.getLineNumber(node.getStartPosition() + node.getLength()) - 1;
int endLineColumn = compilationUnit.getColumnNumber(node.getStartPosition() + node.getLength());
StringBuffer blockComment = new StringBuffer();
for (int lineCount = startLineNumber; lineCount <= endLineNumber; lineCount++) {
int startCol = lineCount == startLineNumber ? startLineColumn : 0;
int endCol = lineCount == endLineNumber ? endLineColumn : source[lineCount].length();
String blockCommentLine = source[lineCount].substring(startCol, endCol).trim();
blockComment.append(blockCommentLine);
if (lineCount != endLineNumber) {
blockComment.append("\n");
}
}
commentText = blockComment.toString();
return true;
}
}