Skip to content

Commit

Permalink
Some more Javadoc fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mickaelistria committed Jun 20, 2024
1 parent 86cc949 commit bc044a4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -517,9 +517,16 @@ 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);
DocTreePath path = this.converter.findDocTreePath(name);
if (path != null) {
if (JavacTrees.instance(this.context).getElement(path) instanceof Symbol symbol) {
return this.bindings.getBinding(symbol, null);
}
// try parent
path = path.getParentPath();
if (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());
Expand Down Expand Up @@ -840,4 +847,24 @@ IAnnotationBinding resolveAnnotation(Annotation annotation) {
}
return null;
}

@Override
IBinding resolveReference(MethodRef ref) {
resolve();
DocTreePath path = this.converter.findDocTreePath(ref);
if (path != null && JavacTrees.instance(this.context).getElement(path) instanceof Symbol symbol) {
return this.bindings.getBinding(symbol, null);
}
return null;
}

@Override
IBinding resolveReference(MemberRef ref) {
resolve();
DocTreePath path = this.converter.findDocTreePath(ref);
if (path != null && JavacTrees.instance(this.context).getElement(path) instanceof Symbol symbol) {
return this.bindings.getBinding(symbol, null);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2435,9 +2435,16 @@ private IfStatement convertIfStatement(JCIf javac) {
return res;
}

/**
* ⚠️ node position in JCTree must be absolute
* @param javac
* @return
*/
Type convertToType(JCTree javac) {
if (javac instanceof JCIdent ident) {
SimpleType res = this.ast.newSimpleType(convertName(ident.name));
Name name = convertName(ident.name);
name.setSourceRange(ident.getStartPosition(), ident.name.length());
SimpleType res = this.ast.newSimpleType(name);
commonSettings(res, ident);
return res;
}
Expand All @@ -2455,17 +2462,23 @@ Type convertToType(JCTree javac) {
// case of not translatable name, eg because of generics
// TODO find a better check instead of relying on exception
Type qualifierType = convertToType(qualified.getExpression());
SimpleName simpleName = (SimpleName)convertName(qualified.getIdentifier());
int simpleNameStart = this.rawText.indexOf(simpleName.getIdentifier(), qualifierType.getStartPosition() + qualifierType.getLength());
simpleName.setSourceRange(simpleNameStart, simpleName.getIdentifier().length());
if(qualifierType instanceof SimpleType simpleType && (ast.apiLevel() < AST.JLS8 || simpleType.annotations().isEmpty())) {
simpleType.delete();
Name parentName = simpleType.getName();
parentName.setParent(null, null);
QualifiedName name = this.ast.newQualifiedName(simpleType.getName(), (SimpleName)convertName(qualified.getIdentifier()));
QualifiedName name = this.ast.newQualifiedName(simpleType.getName(), simpleName);
commonSettings(name, javac);
int length = name.getName().getStartPosition() + name.getName().getLength() - name.getStartPosition();
name.setSourceRange(name.getStartPosition(), length);
SimpleType res = this.ast.newSimpleType(name);
commonSettings(res, javac);
res.setSourceRange(name.getStartPosition(), length);
return res;
} else {
QualifiedType res = this.ast.newQualifiedType(qualifierType, (SimpleName)convertName(qualified.getIdentifier()));
QualifiedType res = this.ast.newQualifiedType(qualifierType, simpleName);
commonSettings(res, qualified);
return res;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@

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;
import com.sun.tools.javac.tree.DCTree.DCBlockTag;
import com.sun.tools.javac.tree.DCTree.DCDeprecated;
import com.sun.tools.javac.tree.DCTree.DCComment;
import com.sun.tools.javac.tree.DCTree.DCDeprecated;
import com.sun.tools.javac.tree.DCTree.DCDocComment;
import com.sun.tools.javac.tree.DCTree.DCEndElement;
import com.sun.tools.javac.tree.DCTree.DCEntity;
Expand All @@ -58,6 +56,7 @@
import com.sun.tools.javac.tree.DCTree.DCValue;
import com.sun.tools.javac.tree.DCTree.DCVersion;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.TreeScanner;
import com.sun.tools.javac.util.JCDiagnostic;

class JavadocConverter {
Expand Down Expand Up @@ -353,6 +352,7 @@ private Stream<? extends IDocElement> convertElement(DCTree javac) {
name.setSourceRange(currentOffset, Math.max(0, reference.memberName.toString().length()));
currentOffset += name.getLength();
res.setName(name);
this.converted.put(name, DocTreePath.getPath(this.contextTreePath, this.docComment, reference));
currentOffset++; // (
final int paramListOffset = currentOffset;
List<Region> params = new ArrayList<>();
Expand All @@ -379,6 +379,7 @@ private Stream<? extends IDocElement> convertElement(DCTree javac) {
commonSettings(res, javac);
SimpleName name = this.ast.newSimpleName(reference.memberName.toString());
name.setSourceRange(this.docComment.getSourcePosition(javac.getStartPosition()), Math.max(0, reference.memberName.toString().length()));
this.converted.put(res, DocTreePath.getPath(this.contextTreePath, this.docComment, reference));
res.setName(name);
if (reference.qualifierExpression != null) {
Name qualifierExpressionName = toName(reference.qualifierExpression, res.getStartPosition());
Expand All @@ -390,7 +391,12 @@ 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));
res.accept(new ASTVisitor() {
@Override
public void preVisit(ASTNode node) {
JavadocConverter.this.converted.put(node, DocTreePath.getPath(JavadocConverter.this.contextTreePath, JavadocConverter.this.docComment, reference));
}
});
return Stream.of(res);
}
} else if (javac instanceof DCStartElement || javac instanceof DCEndElement || javac instanceof DCEntity) {
Expand Down Expand Up @@ -437,16 +443,25 @@ private MethodRefParameter toMethodRefParam(JCTree type, Region range, int param
res.setSourceRange(
range != null ? range.startOffset : paramListOffset + type.getStartPosition(),
range != null ? range.length : type.toString().length());
// Make positons absolute
var fixPositions = new TreeScanner() {
@Override
public void scan(JCTree tree) {
tree.setPos(tree.pos + paramListOffset);
super.scan(tree);
}
};
fixPositions.scan(type);
Type jdtType = this.javacConverter.convertToType(type);
res.setType(jdtType);
jdtType.accept(new ASTVisitor(true) {
// some lengths may be missing
jdtType.accept(new ASTVisitor() {
@Override
public void preVisit(ASTNode node) {
if (node.getStartPosition() <= 0 && node.getParent() != null) {
node.setSourceRange(node.getParent().getStartPosition(), node.getLength() != 0 ? node.getLength() : node.toString().length());
} else {
node.setSourceRange(node.getStartPosition() + paramListOffset, node.getLength() != 0 ? node.getLength() : node.toString().length());
if (node.getLength() == 0 && node.getStartPosition() >= 0) {
node.setSourceRange(node.getStartPosition(), node.toString().length());
}
super.preVisit(node);
}
});
if (jdtType.getStartPosition() + jdtType.getLength() < res.getStartPosition() + res.getLength()) {
Expand Down

0 comments on commit bc044a4

Please sign in to comment.