[javadoc] Add javadoc for type parameters

#IDEA-326131 Fixed

GitOrigin-RevId: 940612c4c8a335e1607c5db596e10fccb575ebea
This commit is contained in:
Louis Vignier
2024-04-26 19:56:18 +00:00
committed by intellij-monorepo-bot
parent a26924949a
commit f2593c7a3d
8 changed files with 72 additions and 1 deletions
@@ -544,7 +544,10 @@ public class JavaDocInfoGenerator {
}
public boolean generateDocInfoCore(StringBuilder buffer, boolean generatePrologue) {
if (myElement instanceof PsiClass cls) {
if (myElement instanceof PsiTypeParameter parameter) {
generateTypeParameterJavaDoc(buffer, parameter, generatePrologue);
}
else if (myElement instanceof PsiClass cls) {
generateClassJavaDoc(buffer, cls, generatePrologue);
}
else if (myElement instanceof PsiMethod method) {
@@ -834,6 +837,33 @@ public class JavaDocInfoGenerator {
buffer.append(DocumentationMarkup.SECTIONS_END);
}
private void generateTypeParameterJavaDoc(StringBuilder buffer, PsiTypeParameter parameter, boolean generatePrologue) {
if (parameter instanceof PsiAnonymousClass) return;
if (generatePrologue) generatePrologue(buffer);
if (!isRendered()) {
buffer.append(DocumentationMarkup.DEFINITION_START);
generateTypeParameterSignature(buffer, parameter, SignaturePlace.Javadoc);
buffer.append(DocumentationMarkup.DEFINITION_END);
}
if (parameter.getOwner() instanceof PsiJavaDocumentedElement documentedElement) {
final PsiDocComment docComment = getDocComment(documentedElement);
PsiDocTag[] localTags = docComment != null ? docComment.getTags() : PsiDocTag.EMPTY_ARRAY;
PsiDocTag tag = getTagByName(localTags, "<" + parameter.getName() + ">");
if (tag != null) {
buffer.append("<p>");
final PsiElement[] elements = Arrays.stream(tag.getChildren())
.skip(1)
.filter(e -> e.getNode().getElementType() != JavaDocTokenType.DOC_COMMENT_LEADING_ASTERISKS &&
e.getNode().getElementType() != JavaDocElementType.DOC_PARAMETER_REF)
.toArray(PsiElement[]::new);
generateValue(buffer, elements, ourEmptyElementsProvider);
}
}
}
private void generateRecordParametersSection(StringBuilder buffer, PsiClass recordClass, PsiDocComment comment) {
if (!recordClass.isRecord() || comment == null) return;
PsiDocTag[] localTags = comment.findTagsByName("param");
@@ -899,6 +929,19 @@ public class JavaDocInfoGenerator {
return false;
}
private void generateTypeParameterSignature(StringBuilder buffer, PsiTypeParameter parameter, SignaturePlace place) {
boolean generateLink = place == SignaturePlace.Javadoc;
appendPlainText(buffer, generateOneTypeParameterPresentableName(parameter));
buffer.append('\n');
PsiClassType[] refs = parameter.getExtendsListTypes();
if (refs.length > 0) {
generateRefList(buffer, parameter, generateLink, refs, "extends");
}
}
private void generateRefList(StringBuilder buffer, PsiClass aClass, boolean generateLink, PsiClassType[] refs, String keyword) {
appendStyledSpan(buffer, getHighlightingManager().getKeywordAttributes(), keyword);
buffer.append(" ");
@@ -0,0 +1,2 @@
<html><head><base href="placeholder"></head><body><div class="definition"><pre>&lt;<span style="color:#20999d;">T</span>&gt;
<span style="color:#000080;font-weight:bold;">extends</span> <a href="psi_element://java.lang.Runnable"><code><span style="color:#0000ff;">Runnable</span></code></a></pre></div><p> param description</p></body></html>
@@ -0,0 +1,6 @@
/**
* @param <T> param description
*/
class MethodTypeParam<<caret>T extends Runnable, K extends String> {
}
@@ -0,0 +1,2 @@
<html><head><base href="placeholder"></head><body><div class="definition"><pre>&lt;<span style="color:#20999d;">T</span>&gt;
</pre></div><p> type param</p></body></html>
@@ -0,0 +1,7 @@
class MethodTypeParam2 {
/**
* @param <T> type param
*/
<<caret>T> void foo(T t) {
}
}
@@ -0,0 +1,2 @@
<html><head><base href="placeholder"></head><body><div class="definition"><pre>&lt;<span style="color:#20999d;">T</span>&gt;
</pre></div><p> parameter</p></body></html>
@@ -0,0 +1,6 @@
class C {
/**
* @param <T> parameter
*/
<T>void m(<caret>T i) {}
}
@@ -77,11 +77,13 @@ public class JavaDocInfoGeneratorTest extends JavaCodeInsightTestCase {
public void testEscapingStringValue() { doTestMethod(); }
public void testIdeadev2326() { doTestMethod(); }
public void testMethodTypeParameter() { doTestMethod(); }
public void testMethodTypeParameter2() { doTestAtCaret(); }
public void testInheritedDocInThrows() { doTestMethod(); }
public void testInheritedDocInThrows1() { doTestMethod(); }
public void testEscapeValues() { doTestClass(); }
public void testClassTypeParameter() { doTestClass(); }
public void testClassTypeParameter1() { doTestClass(); }
public void testClassTypeParameter2() { doTestAtCaret(); }
public void testUnicodeEscapes() { doTestClass(); }
public void testEnumValueOf() { doTestMethod(); }
public void testMethodFormatting() { doTestMethod(); }
@@ -119,6 +121,7 @@ public class JavaDocInfoGeneratorTest extends JavaCodeInsightTestCase {
public void testDocumentationForUncheckedExceptionsInSupers() { doTestAtCaret(); }
public void testDocumentationForGetterByField() { doTestAtCaret(); }
public void testParamInJavadoc() { doTestAtCaret(); }
public void testParamInMethod() { doTestAtCaret(); }
public void testExternalLinksInJavadoc() { doTestAtCaret(); }
public void testLiteralInsideCode() { useJava8(); doTestClass(); }
public void testSuperJavadocExactResolve() { doTestAtCaret(); }