Skip to content

Commit

Permalink
Javadoc work, and ensure parents fully surround children including ja…
Browse files Browse the repository at this point in the history
…vadoc

Signed-off-by: Rob Stryker <[email protected]>
  • Loading branch information
Rob Stryker committed Apr 2, 2024
1 parent e1389cc commit 95a2ae0
Showing 1 changed file with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,19 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws java.io.
JavacConverter converter = new JavacConverter(ast, javacCompilationUnit, context, rawText);
converter.populateCompilationUnit(res, javacCompilationUnit);
attachComments(res, context, fileObject, converter, compilerOptions);
ASTVisitor v = new ASTVisitor() {
public void postVisit(ASTNode node) {
if( node.getParent() != null ) {
if( node.getStartPosition() < node.getParent().getStartPosition()) {
int parentEnd = node.getParent().getStartPosition() + node.getParent().getLength();
if( node.getStartPosition() >= 0 ) {
node.getParent().setSourceRange(node.getStartPosition(), parentEnd - node.getStartPosition());
}
}
}
}
};
res.accept(v);
ast.setBindingResolver(new JavacBindingResolver(javac, javaProject, context, converter));
//
ast.setOriginalModificationCount(ast.modificationCount()); // "un-dirty" AST so Rewrite can process it
Expand Down Expand Up @@ -217,7 +230,7 @@ protected Comment processComment(int pos, int endPos, CommentStyle style) {
return res;
}
}

//
/**
* Currently re-scans the doc to build the list of comments and then
* attach them to the already built AST.
Expand Down Expand Up @@ -261,15 +274,15 @@ private void attachComments(CompilationUnit res, Context context, FileObject fil
Arrays.stream(res.optionalCommentTable)
.filter(Javadoc.class::isInstance)
.map(Javadoc.class::cast)
.forEach(doc -> attachToSibling(doc, res));
.forEach(doc -> attachToSibling(res.getAST(), doc, res));
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}

private void attachToSibling(Javadoc javadoc, CompilationUnit unit) {
FindNextJavadocableSibling finder = new FindNextJavadocableSibling(javadoc.getStartPosition() + javadoc.getLength());
private void attachToSibling(AST ast, Javadoc javadoc, CompilationUnit unit) {
FindNextJavadocableSibling finder = new FindNextJavadocableSibling(javadoc.getStartPosition(), javadoc.getLength());
unit.accept(finder);
if (finder.nextNode != null) {
int endOffset = finder.nextNode.getStartPosition() + finder.nextNode.getLength();
Expand All @@ -283,6 +296,13 @@ private void attachToSibling(Javadoc javadoc, CompilationUnit unit) {
fieldDecl.setJavadoc(javadoc);
finder.nextNode.setSourceRange(javadoc.getStartPosition(), endOffset - javadoc.getStartPosition());
}
} else if (finder.nextNode instanceof PackageDeclaration pd) {
if( ast.apiLevel != AST.JLS2_INTERNAL) {
if( pd.getJavadoc() == null ) {
pd.setJavadoc(javadoc);
finder.nextNode.setSourceRange(javadoc.getStartPosition(), endOffset - javadoc.getStartPosition());
}
}
} else if (finder.nextNode instanceof BodyDeclaration methodDecl) {
if( methodDecl.getJavadoc() == null ) {
methodDecl.setJavadoc(javadoc);
Expand Down

0 comments on commit 95a2ae0

Please sign in to comment.