Skip to content

Commit

Permalink
feat: Add JavaDoc comments JdtVisitor
Browse files Browse the repository at this point in the history
  • Loading branch information
pouryafard75 committed Sep 4, 2024
1 parent 7183a2b commit e054db3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@
/* Created by pourya on 2024-08-28*/
class JdtCommentVisitor extends JdtVisitor {
final TreeContext context;
final char[] source;

public JdtCommentVisitor(IScanner scanner, TreeContext context) {
super(scanner);
this.source = scanner.getSource();
this.context = context;
}

Expand All @@ -45,11 +43,17 @@ public boolean visit(LineComment node) {
return visitComment(node);
}

public boolean visit(Javadoc node) {
if (node.getParent() == null) //Then it is not a normal java doc, it is a comment
return visitComment(node);
return true;
}

public boolean visitComment(Comment node) {
int start = node.getStartPosition();
int end = start + node.getLength();
Tree parent = findMostInnerEnclosingParent(context.getRoot(), start, end);
Tree t = context.createTree(nodeAsSymbol(node), new String(source, start, end - start));
Tree t = context.createTree(nodeAsSymbol(node), new String(scanner.getSource(), start, end - start));
t.setPos(start);
t.setLength(node.getLength());
insertChildProperly(parent, t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class JdtVisitor extends AbstractJdtVisitor {
private static final Type ARRAY_INITIALIZER = nodeAsSymbol(ASTNode.ARRAY_INITIALIZER);
private static final Type SIMPLE_NAME = nodeAsSymbol(ASTNode.SIMPLE_NAME);

private IScanner scanner;
protected IScanner scanner;

public JdtVisitor(IScanner scanner) {
super();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,43 @@ public void testComments() throws IOException {
+ " LineComment: //run(); [66,74]";
assertEquals(expected, ct.getRoot().toTreeString());
}

@Test
public void testComments2() throws IOException {
String input = "/**\n"
+ " * test\n"
+ " */\n"
+ "public class X {\n"
+ " void A(boolean b\n"
+ " ) {\n"
+ " /**\n"
+ " * test2 \n"
+ " */\n"
+ " sleep();\n"
+ " }\n"
+ "}\n";
String expected = "CompilationUnit [0,145]\n"
+ " TypeDeclaration [0,144]\n"
+ " Javadoc [0,31]\n"
+ " TagElement [15,19]\n"
+ " TextElement: test [15,19]\n"
+ " Modifier: public [32,38]\n"
+ " TYPE_DECLARATION_KIND: class [39,44]\n"
+ " SimpleName: X [45,46]\n"
+ " MethodDeclaration [53,142]\n"
+ " PrimitiveType: void [53,57]\n"
+ " SimpleName: A [58,59]\n"
+ " SingleVariableDeclaration [60,69]\n"
+ " PrimitiveType: boolean [60,67]\n"
+ " SimpleName: b [68,69]\n"
+ " Block [76,142]\n"
+ " Javadoc: /**\n"
+ " * test2 \n"
+ " */ [86,119]\n"
+ " ExpressionStatement [128,136]\n"
+ " MethodInvocation [128,135]\n"
+ " SimpleName: sleep [128,133]";
TreeContext ct = new JdtTreeGenerator().generateFrom().string(input);
assertEquals(expected, ct.getRoot().toTreeString());
}
}

0 comments on commit e054db3

Please sign in to comment.