-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCommentVisitor.java
116 lines (88 loc) · 3.99 KB
/
CommentVisitor.java
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.BlockComment;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.LineComment;
import org.eclipse.jdt.core.dom.Javadoc;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CommentVisitor extends ASTVisitor {
// http://stackoverflow.com/questions/12971991/eclipse-jdt-core-parser-not-parsing-the-comments
CompilationUnit compilationUnit;
ArrayList<CommentMap> cMapList;
private char[] source;
int startLine;
int endLine;
public CommentVisitor(CompilationUnit compilationUnit, char[] content, ArrayList<CommentMap> commentMap,
int sLine, int eLine) {
super();
this.compilationUnit = compilationUnit;
this.source = content;
startLine = sLine;
endLine = eLine;
cMapList = commentMap;
}
public boolean visit(Javadoc node) {
int startLineNumber = compilationUnit.getLineNumber(node.getStartPosition());
int endLineNumber = compilationUnit.getLineNumber(node.getStartPosition() + node.getLength());
//System.out.println("dd " + startLineNumber + " " + endLineNumber);
//System.out.println("ee " + startLine + " " + endLine);
if (startLine == startLineNumber) {
// Build the comment char by char
StringBuilder comment = new StringBuilder();
for (int i = node.getStartPosition(); i < node.getStartPosition() + node.getLength(); i++) {
comment.append(source[i]);
}
if (comment.capacity() > 0) {
String str = comment.toString().replaceAll("\\n\\s+", "\n ");
if (str != null) {
CommentMap cMap = new CommentMap(str, startLineNumber, endLineNumber, 0);
cMapList.add(cMap);
}
}
}
return true;
}
public boolean visit(LineComment node) {
int startLineNumber = compilationUnit.getLineNumber(node.getStartPosition());
int endLineNumber = compilationUnit.getLineNumber(node.getStartPosition() + node.getLength());
if (startLineNumber >= startLine && endLineNumber <= endLine) {
// Build the comment char by char
StringBuilder comment = new StringBuilder();
for (int i = node.getStartPosition(); i < node.getStartPosition() + node.getLength(); i++) {
comment.append(source[i]);
}
if (comment.capacity() > 0) {
String str = comment.toString();
if (str != null) {
CommentMap cMap = new CommentMap(str, startLineNumber, endLineNumber, 1);
cMapList.add(cMap);
}
}
}
return true;
}
public boolean visit(BlockComment node) {
int startLineNumber = compilationUnit.getLineNumber(node.getStartPosition());
int endLineNumber = compilationUnit.getLineNumber(node.getStartPosition() + node.getLength());
//System.out.println("dd " + startLineNumber + " " + endLineNumber);
//System.out.println("ee " + startLine + " " + endLine + "\n");
if ((startLineNumber >= startLine && endLineNumber <= endLine) ||
startLine - 1 == endLineNumber) {
// Build the comment char by char
StringBuilder comment = new StringBuilder();
for (int i = node.getStartPosition(); i < node.getStartPosition() + node.getLength(); i++) {
comment.append(source[i]);
}
if (comment.capacity() > 0) {
String str = comment.toString().replaceAll("\\n\\s+", "\n ");
if (str != null) {
CommentMap cMap = new CommentMap(str, startLineNumber, endLineNumber, 2);
cMapList.add(cMap);
}
}
}
return true;
}
}