Skip to content

Commit

Permalink
[Javac] Fix Javadoc Link
Browse files Browse the repository at this point in the history
  • Loading branch information
mickaelistria committed May 10, 2024
1 parent 47222c9 commit 06dd74c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 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.PriorityQueue;
import java.util.function.BiConsumer;
import java.util.function.Predicate;

import javax.lang.model.type.TypeKind;
Expand All @@ -35,8 +36,6 @@
import org.eclipse.jdt.core.dom.ModuleModifier.ModuleModifierKeyword;
import org.eclipse.jdt.core.dom.PrefixExpression.Operator;
import org.eclipse.jdt.core.dom.PrimitiveType.Code;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.lookup.ExtraCompilerModifiers;
import org.eclipse.jdt.internal.javac.JavacProblemConverter;

import com.sun.source.tree.CaseTree.CaseKind;
Expand Down Expand Up @@ -64,7 +63,6 @@
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
import com.sun.tools.javac.tree.JCTree.JCConditional;
import com.sun.tools.javac.tree.JCTree.JCConstantCaseLabel;
import com.sun.tools.javac.tree.JCTree.JCContinue;
import com.sun.tools.javac.tree.JCTree.JCDirective;
import com.sun.tools.javac.tree.JCTree.JCDoWhileLoop;
Expand Down Expand Up @@ -348,30 +346,27 @@ void commonSettings(ASTNode res, JCTree javac) {
}
}

public interface CommonSettingsOperator {
public void op(ASTNode res, JCTree javac);
}
private Name toName(JCTree expression) {
return toName(expression, (a,b) -> commonSettings(a,b));
return toName(expression, this::commonSettings);
}
Name toName(JCTree expression, CommonSettingsOperator commonSettings ) {
Name toName(JCTree expression, BiConsumer<ASTNode, JCTree> extraSettings ) {
if (expression instanceof JCIdent ident) {
Name res = convertName(ident.getName());
commonSettings.op(res, ident);
extraSettings.accept(res, ident);
return res;
}
if (expression instanceof JCFieldAccess fieldAccess) {
Name qualifier = toName(fieldAccess.getExpression());
SimpleName n = (SimpleName)convertName(fieldAccess.getIdentifier());
QualifiedName res = this.ast.newQualifiedName(qualifier, n);
commonSettings.op(res, fieldAccess);
extraSettings.accept(res, fieldAccess);
return res;
}
if (expression instanceof JCAnnotatedType jcat) {
return toName(jcat.underlyingType, commonSettings);
return toName(jcat.underlyingType, extraSettings);
}
if (expression instanceof JCTypeApply jcta) {
return toName(jcta.clazz, commonSettings);
return toName(jcta.clazz, extraSettings);
}
throw new UnsupportedOperationException("toName for " + expression + " (" + expression.getClass().getName() + ")");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@ private void commonSettings(ASTNode res, DCTree javac) {
}
//this.domToJavac.put(res, javac);
}
private void commonSettings(ASTNode res, JCTree javac) {
int start = this.docComment.getSourcePosition(javac.getStartPosition());
int length = javac.toString().length();
res.setSourceRange(start, Math.max(0,length));
}

Javadoc convertJavadoc() {
Javadoc res = this.ast.newJavadoc();
Expand Down Expand Up @@ -185,7 +180,11 @@ private Optional<TagElement> convertInlineTag(DCTree javac) {
}

private Name toName(JCTree expression) {
Name n = this.javacConverter.toName(expression, (a,b) -> commonSettings(a,b));
Name n = this.javacConverter.toName(expression, (dom, javac) -> {
int start = this.docComment.getSourcePosition(javac.getStartPosition());
int length = javac.toString().length();
dom.setSourceRange(start, Math.max(0,length));
});
// We need to clean all the sub-names
if( n instanceof QualifiedName qn ) {
SimpleName sn = qn.getName();
Expand Down Expand Up @@ -239,14 +238,25 @@ private IDocElement convertElement(DCTree javac) {
} else {
MemberRef res = this.ast.newMemberRef();
commonSettings(res, javac);
Name qualifierExpressionName = toName(reference.qualifierExpression);
qualifierExpressionName.setSourceRange(this.docComment.getSourcePosition(reference.pos), Math.max(0, reference.qualifierExpression.toString().length()));
if (reference.memberName != null) {
SimpleName name = this.ast.newSimpleName(reference.memberName.toString());
name.setSourceRange(this.docComment.getSourcePosition(javac.getStartPosition()), Math.max(0, reference.memberName.toString().length()));
res.setName(name);
res.setQualifier(qualifierExpressionName);
} else {
if (qualifierExpressionName instanceof SimpleName simpleQualifier) {
res.setName(simpleQualifier);
} else if (qualifierExpressionName instanceof QualifiedName qName) {
Name qualifier = qName.getQualifier();
qualifier.setParent(null, MemberRef.QUALIFIER_PROPERTY);
res.setQualifier(qualifier);
SimpleName simpleName = qName.getName();
simpleName.setParent(null, MemberRef.NAME_PROPERTY);
res.setName(simpleName);
}
}
Name n = toName(reference.qualifierExpression);
n.setSourceRange(this.docComment.getSourcePosition(reference.pos), Math.max(0, reference.qualifierExpression.toString().length()));
res.setQualifier(n);
return res;
}
} else if (javac instanceof DCStartElement || javac instanceof DCEndElement || javac instanceof DCEntity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private static org.eclipse.jface.text.Position getPositionUsingScanner(JCDiagnos
javacScanner.nextToken();
}
Token toHighlight = javacScanner.prevToken();
return new org.eclipse.jface.text.Position(toHighlight.pos, toHighlight.endPos - toHighlight.pos - 1);
return new org.eclipse.jface.text.Position(toHighlight.pos, Math.max(0, toHighlight.endPos - toHighlight.pos - 1));
} catch (IOException ex) {
ILog.get().error(ex.getMessage(), ex);
}
Expand Down

0 comments on commit 06dd74c

Please sign in to comment.