aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/src/main/kotlin/translators/psi
diff options
context:
space:
mode:
authorVadim Mishenev <vad-mishenev@yandex.ru>2022-08-18 18:43:20 +0300
committerGitHub <noreply@github.com>2022-08-18 18:43:20 +0300
commitdf8d9879b818799c83ff731b3a78e7d2b96fd8e5 (patch)
treea935c4fcd42750cf7727d9970c2c575db0f9400e /plugins/base/src/main/kotlin/translators/psi
parente9e95f6bdefa2ea4bd2cc1a79ed642ff57b25e48 (diff)
downloaddokka-df8d9879b818799c83ff731b3a78e7d2b96fd8e5.tar.gz
dokka-df8d9879b818799c83ff731b3a78e7d2b96fd8e5.tar.bz2
dokka-df8d9879b818799c83ff731b3a78e7d2b96fd8e5.zip
Fix generic types caching (#2619)
Diffstat (limited to 'plugins/base/src/main/kotlin/translators/psi')
-rw-r--r--plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt57
1 files changed, 38 insertions, 19 deletions
diff --git a/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt b/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt
index d98559e5..1fbe06f7 100644
--- a/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt
+++ b/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt
@@ -502,11 +502,21 @@ class DefaultPsiToDocumentableTranslator(
} ?: PropertyContainer.empty()
private fun getBound(type: PsiType): Bound {
- fun bound() = when (type) {
+ //We would like to cache most of the bounds since it is not common to annotate them,
+ //but if this is the case, we treat them as 'one of'
+ fun PsiType.cacheBoundIfHasNoAnnotation(f: (List<Annotations.Annotation>) -> Bound): Bound {
+ val annotations = this.annotations.toList().toListOfAnnotations()
+ return if (annotations.isNotEmpty()) f(annotations)
+ else cachedBounds.getOrPut(canonicalText) {
+ f(annotations)
+ }
+ }
+
+ return when (type) {
is PsiClassReferenceType ->
type.resolve()?.let { resolved ->
when {
- resolved.qualifiedName == "java.lang.Object" -> JavaObject(type.annotations())
+ resolved.qualifiedName == "java.lang.Object" -> type.cacheBoundIfHasNoAnnotation { annotations -> JavaObject(annotations.annotations()) }
resolved is PsiTypeParameter -> {
TypeParameter(
dri = DRI.from(resolved),
@@ -514,6 +524,7 @@ class DefaultPsiToDocumentableTranslator(
extra = type.annotations()
)
}
+
Regex("kotlin\\.jvm\\.functions\\.Function.*").matches(resolved.qualifiedName ?: "") ||
Regex("java\\.util\\.function\\.Function.*").matches(
resolved.qualifiedName ?: ""
@@ -522,33 +533,41 @@ class DefaultPsiToDocumentableTranslator(
type.parameters.map { getProjection(it) },
extra = type.annotations()
)
- else -> GenericTypeConstructor(
- DRI.from(resolved),
- type.parameters.map { getProjection(it) },
- extra = type.annotations()
- )
+
+ else -> {
+ // cache types that have no annotation and no type parameter
+ // since we cache only by name and type parameters depend on context
+ val typeParameters = type.parameters.map { getProjection(it) }
+ if (typeParameters.isEmpty())
+ type.cacheBoundIfHasNoAnnotation { annotations ->
+ GenericTypeConstructor(
+ DRI.from(resolved),
+ typeParameters,
+ extra = annotations.annotations()
+ )
+ }
+ else
+ GenericTypeConstructor(
+ DRI.from(resolved),
+ typeParameters,
+ extra = type.annotations()
+ )
+ }
}
} ?: UnresolvedBound(type.presentableText, type.annotations())
+
is PsiArrayType -> GenericTypeConstructor(
DRI("kotlin", "Array"),
listOf(getProjection(type.componentType)),
extra = type.annotations()
)
+
is PsiPrimitiveType -> if (type.name == "void") Void
- else PrimitiveJavaType(type.name, type.annotations())
- is PsiImmediateClassType -> JavaObject(type.annotations())
+ else type.cacheBoundIfHasNoAnnotation { annotations -> PrimitiveJavaType(type.name, annotations.annotations()) }
+ is PsiImmediateClassType ->
+ type.cacheBoundIfHasNoAnnotation { annotations -> JavaObject(annotations.annotations()) }
else -> throw IllegalStateException("${type.presentableText} is not supported by PSI parser")
}
-
- //We would like to cache most of the bounds since it is not common to annotate them,
- //but if this is the case, we treat them as 'one of'
- return if (type.annotations.toList().toListOfAnnotations().isEmpty()) {
- cachedBounds.getOrPut(type.canonicalText) {
- bound()
- }
- } else {
- bound()
- }
}