From 36d58fee2bf06e576a6f6906b5faf3d8675c05a8 Mon Sep 17 00:00:00 2001 From: Mickael Istria Date: Thu, 19 Sep 2024 21:45:34 +0200 Subject: [PATCH] Support conversion of Markdown Javadoc --- .../eclipse/jdt/core/dom/JavacConverter.java | 66 ++++++++++++++++--- .../jdt/core/dom/JavadocConverter.java | 16 +++-- 2 files changed, 69 insertions(+), 13 deletions(-) diff --git a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacConverter.java b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacConverter.java index 8b825c06a1e..9e7bb7f46f8 100644 --- a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacConverter.java +++ b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacConverter.java @@ -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; @@ -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); @@ -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()) { @@ -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; } @@ -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) { @@ -3465,6 +3493,28 @@ public boolean visit(Modifier modifier) { return true; } + @Override + public void endVisit(TagElement tagElement) { + if (tagElement.getStartPosition() < 0) { + OptionalInt start = ((List)tagElement.fragments()).stream() + .filter(node -> node.getStartPosition() >= 0 && node.getLength() >= 0) + .mapToInt(ASTNode::getStartPosition) + .min(); + OptionalInt end = ((List)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 excluding) { int current = in.getStartPosition(); PriorityQueue excluded = new PriorityQueue<>(Comparator.comparing(ASTNode::getStartPosition)); diff --git a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavadocConverter.java b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavadocConverter.java index 8e8f404ce4d..19a1b3c1369 100644 --- a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavadocConverter.java +++ b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavadocConverter.java @@ -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; @@ -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)); } @@ -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()); } @@ -225,7 +228,7 @@ Set 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, @@ -595,9 +598,10 @@ private List convertElementCombiningNodes(List 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 { @@ -633,6 +637,8 @@ private List convertElementCombiningNodes(List treeElements private Stream 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);