diff options
author | Andrzej Ratajczak <32793002+BarkingBad@users.noreply.github.com> | 2022-01-27 10:39:56 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-27 12:39:56 +0300 |
commit | 7c44db1ef0075e2b80a4723e0747bbf57c32e775 (patch) | |
tree | d20079f969fbb977603852ef4ea2234e54851f98 /kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis | |
parent | d884fa7ce3088ac9ae0ca1bbad70e698888610fa (diff) | |
download | dokka-7c44db1ef0075e2b80a4723e0747bbf57c32e775.tar.gz dokka-7c44db1ef0075e2b80a4723e0747bbf57c32e775.tar.bz2 dokka-7c44db1ef0075e2b80a4723e0747bbf57c32e775.zip |
Fix resolving DRIs of Enum Entries (#2305)
* Fix resolving DRIs of Enum Entries
* Unify DRIs for Kotlin and Java enums. Add EnumEntry linking tests
* Updates EnumEntry extras in documentable translators
* Fix tests
* Apply requested changes
* Apply requested changes
Diffstat (limited to 'kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis')
-rw-r--r-- | kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/DRIFactory.kt | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/DRIFactory.kt b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/DRIFactory.kt index 5f74c429..33c99275 100644 --- a/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/DRIFactory.kt +++ b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/DRIFactory.kt @@ -7,6 +7,7 @@ import org.jetbrains.kotlin.descriptors.impl.EnumEntrySyntheticClassDescriptor import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf import org.jetbrains.kotlin.resolve.descriptorUtil.parentsWithSelf import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull +import org.jetbrains.kotlin.utils.addToStdlib.safeAs fun DRI.Companion.from(descriptor: DeclarationDescriptor) = descriptor.parentsWithSelf.run { val parameter = firstIsInstanceOrNull<ValueParameterDescriptor>() @@ -20,7 +21,7 @@ fun DRI.Companion.from(descriptor: DeclarationDescriptor) = descriptor.parentsWi ?.joinToString(separator = ".") { it.name.asString() }, callable = callable?.let { Callable.from(it) }, target = DriTarget.from(parameter ?: descriptor), - extra = if (descriptor is EnumEntrySyntheticClassDescriptor) + extra = if (descriptor is EnumEntrySyntheticClassDescriptor || descriptor.safeAs<ClassDescriptor>()?.kind == ClassKind.ENUM_ENTRY) DRIExtraContainer().also { it[EnumEntryDRIExtra] = EnumEntryDRIExtra }.encode() else null ) @@ -31,11 +32,16 @@ fun DRI.Companion.from(psi: PsiElement) = psi.parentsWithSelf.run { val psiField = firstIsInstanceOrNull<PsiField>() val classes = filterIsInstance<PsiClass>().filterNot { it is PsiTypeParameter } .toList() // We only want exact PsiClass types, not PsiTypeParameter subtype + val additionalClasses = if (psi is PsiEnumConstant) listOfNotNull(psiField?.name) else emptyList() DRI( packageName = classes.lastOrNull()?.qualifiedName?.substringBeforeLast('.', "") ?: "", - classNames = classes.toList().takeIf { it.isNotEmpty() }?.asReversed()?.mapNotNull { it.name } - ?.joinToString("."), - callable = psiMethod?.let { Callable.from(it) } ?: psiField?.let { Callable.from(it) }, + classNames = (additionalClasses + classes.mapNotNull { it.name }).takeIf { it.isNotEmpty() } + ?.asReversed()?.joinToString("."), + // The fallback strategy test whether psi is not `PsiEnumConstant`. The reason behind this is that + // we need unified DRI for both Java and Kotlin enums, so we can link them properly and treat them alike. + // To achieve that, we append enum name to classNames list and leave the callable part set to null. For Kotlin enums + // it is by default, while for Java enums we have to explicitly test for that in this `takeUnless` condition. + callable = psiMethod?.let { Callable.from(it) } ?: psiField?.takeUnless { psi is PsiEnumConstant }?.let { Callable.from(it) }, target = DriTarget.from(psi), extra = if (psi is PsiEnumConstant) DRIExtraContainer().also { it[EnumEntryDRIExtra] = EnumEntryDRIExtra }.encode() |