Skip to content

Commit

Permalink
Fix missing annotations in GFM and unresolved static imports (#1845)
Browse files Browse the repository at this point in the history
* Fix missing unresolved links in GFM

* Fix missing links to elements imported as static
  • Loading branch information
MarcinAman authored Apr 14, 2021
1 parent acd9234 commit 38270a3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package org.jetbrains.dokka.base.translators.psi

import com.intellij.lang.jvm.JvmModifier
import com.intellij.lang.jvm.annotation.JvmAnnotationAttribute
import com.intellij.lang.jvm.annotation.JvmAnnotationAttributeValue
import com.intellij.lang.jvm.annotation.JvmAnnotationEnumFieldValue
import com.intellij.lang.jvm.types.JvmReferenceType
import com.intellij.openapi.vfs.VirtualFileManager
import com.intellij.psi.*
Expand Down Expand Up @@ -572,13 +574,22 @@ class DefaultPsiToDocumentableTranslator(
filter { it !is KtLightAbstractAnnotation }.mapNotNull { it.toAnnotation() }

private fun JvmAnnotationAttribute.toValue(): AnnotationParameterValue = when (this) {
is PsiNameValuePair -> value?.toValue() ?: StringValue("")
is PsiNameValuePair -> value?.toValue() ?: attributeValue?.toValue() ?: StringValue("")
else -> StringValue(this.attributeName)
}.let { annotationValue ->
if (annotationValue is StringValue) annotationValue.copy(unquotedValue(annotationValue.value))
else annotationValue
}

/**
* This is a workaround for static imports from JDK like RetentionPolicy
* For some reason they are not represented in the same way than using normal import
*/
private fun JvmAnnotationAttributeValue.toValue(): AnnotationParameterValue? = when (this) {
is JvmAnnotationEnumFieldValue -> (field as? PsiElement)?.let { EnumValue(fieldName ?: "", DRI.from(it)) }
else -> null
}

private fun PsiAnnotationMemberValue.toValue(): AnnotationParameterValue? = when (this) {
is PsiAnnotation -> toAnnotation()?.let { AnnotationValue(it) }
is PsiArrayInitializerMemberValue -> ArrayValue(initializers.mapNotNull { it.toValue() })
Expand Down
38 changes: 38 additions & 0 deletions plugins/base/src/test/kotlin/model/JavaTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.junit.jupiter.api.Test
import utils.AbstractModelTest
import utils.assertNotNull
import utils.name
import kotlin.test.assertEquals
import org.jetbrains.dokka.links.Callable as DRICallable

class JavaTest : AbstractModelTest("/src/main/kotlin/java/Test.java", "java") {
Expand Down Expand Up @@ -342,4 +343,41 @@ class JavaTest : AbstractModelTest("/src/main/kotlin/java/Test.java", "java") {
}
}
}

@Test
fun `retention should work with static import`() {
inlineModelTest(
"""
|import java.lang.annotation.Retention;
|import java.lang.annotation.RetentionPolicy;
|import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
|@Retention(RUNTIME)
|public @interface JsonClass {
|};
""", configuration = configuration
) {
with((this / "java" / "JsonClass").cast<DAnnotation>()) {
val annotation = extra[Annotations]?.directAnnotations?.entries
?.firstOrNull()?.value //First sourceset
?.firstOrNull()

val expectedDri = DRI("java.lang.annotation", "Retention", null, PointingToDeclaration)
val expectedParams = "value" to EnumValue(
"RUNTIME",
DRI(
"java.lang.annotation",
"RetentionPolicy",
DRICallable("RUNTIME", null, emptyList()),
PointingToDeclaration
)
)

assertEquals(expectedDri, annotation?.dri)
assertEquals(expectedParams.first, annotation?.params?.entries?.first()?.key)
assertEquals(expectedParams.second, annotation?.params?.entries?.first()?.value)
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ open class CommonmarkRenderer(
templateCommand(ResolveLinkGfmCommand(node.address)) {
buildText(node.children, pageContext, sourceSetRestriction)
}
} else Unit
} else buildText(node.children, pageContext, sourceSetRestriction)
}

override fun StringBuilder.buildNewLine() {
Expand Down

0 comments on commit 38270a3

Please sign in to comment.