Skip to content

Commit

Permalink
Resolve javadoc bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
mickaelistria authored and datho7561 committed Jun 19, 2024
1 parent 1fcbdee commit a0cdf42
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
import org.eclipse.jdt.internal.javac.dom.JavacTypeVariableBinding;
import org.eclipse.jdt.internal.javac.dom.JavacVariableBinding;

import com.sun.source.util.DocTreePath;
import com.sun.source.util.JavacTask;
import com.sun.tools.javac.api.JavacTrees;
import com.sun.tools.javac.code.Attribute;
import com.sun.tools.javac.code.Attribute.Compound;
import com.sun.tools.javac.code.Symbol;
Expand Down Expand Up @@ -515,6 +517,10 @@ IBinding resolveName(Name name) {
if( tree != null ) {
return resolveNameToJavac(name, tree);
}
DocTreePath path = this.converter.findDocTreePath(name); // TODO
if (path != null && JavacTrees.instance(this.context).getElement(path) instanceof Symbol symbol) {
return this.bindings.getBinding(symbol, null);
}
if (tree == null && (name.getFlags() & ASTNode.ORIGINAL) != 0) {
tree = this.converter.domToJavac.get(name.getParent());
if( tree instanceof JCFieldAccess jcfa) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import com.sun.source.tree.CaseTree.CaseKind;
import com.sun.source.tree.ModuleTree.ModuleKind;
import com.sun.source.tree.Tree.Kind;
import com.sun.source.util.DocTreePath;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.code.BoundKind;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.parser.Tokens.Comment;
Expand Down Expand Up @@ -135,6 +137,7 @@ class JavacConverter {
final Map<ASTNode, JCTree> domToJavac = new HashMap<>();
final String rawText;
final Set<JCDiagnostic> javadocDiagnostics = new HashSet<>();
private final List<JavadocConverter> javadocConverters = new ArrayList<>();

public JavacConverter(AST ast, JCCompilationUnit javacCompilationUnit, Context context, String rawText) {
this.ast = ast;
Expand Down Expand Up @@ -2882,7 +2885,8 @@ private Name convert(com.sun.tools.javac.util.Name javac, String selected) {
public org.eclipse.jdt.core.dom.Comment convert(Comment javac, JCTree context) {
if (javac.getStyle() == CommentStyle.JAVADOC && context != null) {
var docCommentTree = this.javacCompilationUnit.docComments.getCommentTree(context);
JavadocConverter javadocConverter = new JavadocConverter(this, docCommentTree);
JavadocConverter javadocConverter = new JavadocConverter(this, docCommentTree, TreePath.getPath(this.javacCompilationUnit, context));
this.javadocConverters.add(javadocConverter);
Javadoc javadoc = javadocConverter.convertJavadoc();
this.javadocDiagnostics.addAll(javadocConverter.getDiagnostics());
return javadoc;
Expand Down Expand Up @@ -3055,5 +3059,13 @@ private static List<ASTNode> childrenOf(ASTNode node) {
.toList();
}

public DocTreePath findDocTreePath(ASTNode node) {
return this.javadocConverters.stream()
.map(javadocConverter -> javadocConverter.converted.get(node))
.filter(Objects::nonNull)
.findFirst()
.orElse(null);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
Expand All @@ -23,6 +25,10 @@

import org.eclipse.core.runtime.ILog;

import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.Tree;
import com.sun.source.util.DocTreePath;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.parser.UnicodeReader;
import com.sun.tools.javac.tree.DCTree;
import com.sun.tools.javac.tree.DCTree.DCAuthor;
Expand Down Expand Up @@ -61,6 +67,9 @@ class JavadocConverter {
private final DCDocComment docComment;
private final int initialOffset;
private final int endOffset;
private final TreePath contextTreePath;

public final Map<ASTNode, DocTreePath> converted = new HashMap<>();

final private Set<JCDiagnostic> diagnostics = new HashSet<>();

Expand All @@ -75,10 +84,11 @@ class JavadocConverter {
}
}

JavadocConverter(JavacConverter javacConverter, DCDocComment docComment) {
JavadocConverter(JavacConverter javacConverter, DCDocComment docComment, TreePath contextTreePath) {
this.javacConverter = javacConverter;
this.ast = javacConverter.ast;
this.docComment = docComment;
this.contextTreePath = contextTreePath;

int startPos = -1;
if (UNICODE_READER_CLASS_OFFSET_FIELD != null) {
Expand Down Expand Up @@ -107,6 +117,7 @@ private void commonSettings(ASTNode res, DCTree javac) {
length++;
}
res.setSourceRange(startPosition, length);
this.converted.put(res, DocTreePath.getPath(this.contextTreePath, this.docComment, javac));
}
}

Expand Down Expand Up @@ -379,6 +390,7 @@ private Stream<? extends IDocElement> convertElement(DCTree javac) {
} else if (!signature.contains("#")) {
Name res = this.ast.newName(signature);
res.setSourceRange(this.docComment.getSourcePosition(javac.getStartPosition()), signature.length());
this.converted.put(res, DocTreePath.getPath(this.contextTreePath, this.docComment, reference));
return Stream.of(res);
}
} else if (javac instanceof DCStartElement || javac instanceof DCEndElement || javac instanceof DCEntity) {
Expand Down

0 comments on commit a0cdf42

Please sign in to comment.