Skip to content

Commit

Permalink
Support conversion of Markdown Javadoc
Browse files Browse the repository at this point in the history
  • Loading branch information
mickaelistria committed Sep 20, 2024
1 parent a20aacb commit 36d58fe
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.PriorityQueue;
import java.util.Set;
import java.util.function.BiConsumer;
Expand Down Expand Up @@ -1220,7 +1221,7 @@ private FieldDeclaration convertFieldDeclaration(JCVariableDecl javac, ASTNode p

private void setJavadocForNode(JCTree javac, ASTNode node) {
Comment c = this.javacCompilationUnit.docComments.getComment(javac);
if( c != null && c.getStyle() == Comment.CommentStyle.JAVADOC_BLOCK) {
if(c != null && (c.getStyle() == Comment.CommentStyle.JAVADOC_BLOCK || c.getStyle() == CommentStyle.JAVADOC_LINE)) {
Javadoc javadoc = (Javadoc)convert(c, javac);
if (node instanceof BodyDeclaration bodyDeclaration) {
bodyDeclaration.setJavadoc(javadoc);
Expand Down Expand Up @@ -3371,14 +3372,17 @@ private Name convertName(com.sun.tools.javac.util.Name javac) {


public org.eclipse.jdt.core.dom.Comment convert(Comment javac, JCTree context) {
if (javac.getStyle() == CommentStyle.JAVADOC_BLOCK && context != null) {
if ((javac.getStyle() == CommentStyle.JAVADOC_BLOCK || javac.getStyle() == CommentStyle.JAVADOC_LINE) && context != null) {
var docCommentTree = this.javacCompilationUnit.docComments.getCommentTree(context);
if (docCommentTree instanceof DCDocComment dcDocComment) {
JavadocConverter javadocConverter = new JavadocConverter(this, dcDocComment, TreePath.getPath(this.javacCompilationUnit, context), this.buildJavadoc);
this.javadocConverters.add(javadocConverter);
Javadoc javadoc = javadocConverter.convertJavadoc();
this.javadocDiagnostics.addAll(javadocConverter.getDiagnostics());
return javadoc;
JavadocConverter javadocConverter = new JavadocConverter(this, dcDocComment, TreePath.getPath(this.javacCompilationUnit, context), this.buildJavadoc);
this.javadocConverters.add(javadocConverter);
Javadoc javadoc = javadocConverter.convertJavadoc();
if (this.ast.apiLevel() >= AST.JLS23) {
javadoc.setMarkdown(javac.getStyle() == CommentStyle.JAVADOC_LINE);
}
this.javadocDiagnostics.addAll(javadocConverter.getDiagnostics());
return javadoc;
}
}
org.eclipse.jdt.core.dom.Comment jdt = switch (javac.getStyle()) {
Expand All @@ -3393,11 +3397,14 @@ public org.eclipse.jdt.core.dom.Comment convert(Comment javac, JCTree context) {
}

public org.eclipse.jdt.core.dom.Comment convert(Comment javac, int pos, int endPos) {
if (javac.getStyle() == CommentStyle.JAVADOC_BLOCK) {
if (javac.getStyle() == CommentStyle.JAVADOC_BLOCK || javac.getStyle() == CommentStyle.JAVADOC_LINE) {
var parser = new com.sun.tools.javac.parser.DocCommentParser(ParserFactory.instance(this.context), Log.instance(this.context).currentSource(), javac);
JavadocConverter javadocConverter = new JavadocConverter(this, parser.parse(), pos, endPos, this.buildJavadoc);
this.javadocConverters.add(javadocConverter);
Javadoc javadoc = javadocConverter.convertJavadoc();
if (this.ast.apiLevel() >= AST.JLS23) {
javadoc.setMarkdown(javac.getStyle() == CommentStyle.JAVADOC_LINE);
}
this.javadocDiagnostics.addAll(javadocConverter.getDiagnostics());
return javadoc;
}
Expand Down Expand Up @@ -3426,6 +3433,27 @@ class FixPositions extends ASTVisitor {
this.contents = s;
}

public void postVisit(ASTNode node) {
int start = node.getStartPosition();
int end = start + node.getLength();
if (start >= 0 && end >= start && node.getParent() != null) {
int parentStart = node.getParent().getStartPosition();
int parentEnd = node.getParent().getStartPosition() + node.getParent().getLength();
boolean changed = false;
if (node.getParent().getStartPosition() < 0 || node.getParent().getStartPosition() > start) {
parentStart = start;
changed = true;
}
if (node.getParent().getStartPosition() < 0 || node.getLength() < 0 || parentEnd < end) {
parentEnd = end;
changed = true;
}
if (changed) {
node.getParent().setSourceRange(parentStart, parentEnd - parentStart);
}
}
}

@Override
public boolean visit(QualifiedName node) {
if (node.getStartPosition() < 0) {
Expand Down Expand Up @@ -3465,6 +3493,28 @@ public boolean visit(Modifier modifier) {
return true;
}

@Override
public void endVisit(TagElement tagElement) {
if (tagElement.getStartPosition() < 0) {
OptionalInt start = ((List<ASTNode>)tagElement.fragments()).stream()
.filter(node -> node.getStartPosition() >= 0 && node.getLength() >= 0)
.mapToInt(ASTNode::getStartPosition)
.min();
OptionalInt end = ((List<ASTNode>)tagElement.fragments()).stream()
.filter(node -> node.getStartPosition() >= 0 && node.getLength() >= 0)
.mapToInt(node -> node.getStartPosition() + node.getLength())
.min();
if (start.isPresent() && end.isPresent()) {
if (JavadocConverter.isInline(tagElement)) {
// include some extra wrapping chars ( `{...}` or `[...]`)
tagElement.setSourceRange(start.getAsInt() - 1, end.getAsInt() + 1);
} else {
tagElement.setSourceRange(start.getAsInt(), end.getAsInt());
}
}
}
}

private int findPositionOfText(String text, ASTNode in, List<ASTNode> excluding) {
int current = in.getStartPosition();
PriorityQueue<ASTNode> excluded = new PriorityQueue<>(Comparator.comparing(ASTNode::getStartPosition));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.sun.tools.javac.tree.DCTree.DCLink;
import com.sun.tools.javac.tree.DCTree.DCLiteral;
import com.sun.tools.javac.tree.DCTree.DCParam;
import com.sun.tools.javac.tree.DCTree.DCRawText;
import com.sun.tools.javac.tree.DCTree.DCReference;
import com.sun.tools.javac.tree.DCTree.DCReturn;
import com.sun.tools.javac.tree.DCTree.DCSee;
Expand Down Expand Up @@ -138,7 +139,9 @@ private void commonSettings(ASTNode res, DCTree javac) {
// if (res instanceof TextElement) {
// length++;
// }
res.setSourceRange(startPosition, length);
if (startPosition >= 0 && length >= 0) {
res.setSourceRange(startPosition, length);
}
if (this.contextTreePath != null) {
this.converted.put(res, DocTreePath.getPath(this.contextTreePath, this.docComment, javac));
}
Expand Down Expand Up @@ -177,7 +180,7 @@ Javadoc convertJavadoc() {
if(docElement instanceof ASTNode astn) {
host.setSourceRange(astn.getStartPosition(), astn.getLength());
}
} else if (docElement instanceof ASTNode extraNode){
} else if (docElement instanceof ASTNode extraNode && extraNode.getStartPosition() >= 0 && extraNode.getLength() >= 0){
host.setSourceRange(host.getStartPosition(), extraNode.getStartPosition() + extraNode.getLength() - host.getStartPosition());
}

Expand Down Expand Up @@ -225,7 +228,7 @@ Set<JCDiagnostic> getDiagnostics() {
return diagnostics;
}

private boolean isInline(TagElement tag) {
static boolean isInline(TagElement tag) {
return tag.getTagName() == null || switch (tag.getTagName()) {
case TagElement.TAG_CODE,
TagElement.TAG_DOCROOT,
Expand Down Expand Up @@ -595,9 +598,10 @@ private List<IDocElement> convertElementCombiningNodes(List<DCTree> treeElements
boolean shouldCombine = false;
boolean lineBreakBefore = false;
DCTree oneTree = treeElements.get(i);
if( oneTree instanceof DCText || oneTree instanceof DCStartElement || oneTree instanceof DCEndElement || oneTree instanceof DCEntity) {
if( oneTree instanceof DCText || oneTree instanceof DCStartElement || oneTree instanceof DCEndElement || oneTree instanceof DCEntity || oneTree instanceof DCRawText) {
shouldCombine = true;
if( oneTree instanceof DCText dct && dct.text.startsWith("\n")) {
if((oneTree instanceof DCText dct && dct.text.startsWith("\n"))
|| (oneTree instanceof DCRawText raw && raw.getContent().endsWith("\n"))) {
lineBreakBefore = true;
}
} else {
Expand Down Expand Up @@ -633,6 +637,8 @@ private List<IDocElement> convertElementCombiningNodes(List<DCTree> treeElements
private Stream<? extends IDocElement> convertElement(DCTree javac) {
if (javac instanceof DCText text) {
return splitLines(text, false).map(this::toTextElement);
} else if (javac instanceof DCRawText rawText) {
return splitLines(rawText.getContent(), rawText.getStartPosition(), rawText.getEndPosition(), false).map(this::toTextElement);
} else if (javac instanceof DCIdentifier identifier) {
Name res = this.ast.newName(identifier.getName().toString());
commonSettings(res, javac);
Expand Down

0 comments on commit 36d58fe

Please sign in to comment.