aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/src/main/kotlin/translators
diff options
context:
space:
mode:
authorAndrzej Ratajczak <andrzej.ratajczak98@gmail.com>2020-05-28 16:04:20 +0200
committerPaweł Marks <pmarks@virtuslab.com>2020-06-12 14:15:24 +0200
commitd7be30c841cb925fd0d6322ccdd9877169730b92 (patch)
tree0cc39a4046af6ea203d516c937d26eaf16ef53da /plugins/base/src/main/kotlin/translators
parentcacc5ec47759c9ea2b8edeec6b39d8d16e6c8860 (diff)
downloaddokka-d7be30c841cb925fd0d6322ccdd9877169730b92.tar.gz
dokka-d7be30c841cb925fd0d6322ccdd9877169730b92.tar.bz2
dokka-d7be30c841cb925fd0d6322ccdd9877169730b92.zip
Add platform dependent annotations
Diffstat (limited to 'plugins/base/src/main/kotlin/translators')
-rw-r--r--plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt69
-rw-r--r--plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt23
2 files changed, 55 insertions, 37 deletions
diff --git a/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt b/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt
index 9f01267e..a8175405 100644
--- a/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt
+++ b/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt
@@ -16,7 +16,6 @@ import org.jetbrains.kotlin.builtins.isExtensionFunctionType
import org.jetbrains.kotlin.builtins.isFunctionType
import org.jetbrains.kotlin.codegen.isJvmStaticInObjectOrClassOrInterface
import org.jetbrains.kotlin.descriptors.*
-import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor.Kind.FAKE_OVERRIDE
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.descriptors.annotations.Annotated
@@ -48,6 +47,7 @@ import org.jetbrains.kotlin.resolve.constants.AnnotationValue as ConstantsAnnota
import org.jetbrains.kotlin.resolve.constants.ArrayValue as ConstantsArrayValue
import org.jetbrains.kotlin.resolve.constants.EnumValue as ConstantsEnumValue
import org.jetbrains.kotlin.resolve.constants.KClassValue as ConstantsKtClassValue
+import kotlin.IllegalArgumentException
object DefaultDescriptorToDocumentableTranslator : SourceToDocumentableTranslator {
@@ -88,11 +88,11 @@ private class DokkaDescriptorVisitor(
}
private fun Collection<DeclarationDescriptor>.filterDescriptorsInSourceSet() = filter {
- it.toSourceElement.containingFile.toString().let { path ->
- path.isNotBlank() && sourceSet.sourceRoots.any { root ->
- Paths.get(path).startsWith(Paths.get(root.path))
- }
+ it.toSourceElement.containingFile.toString().let { path ->
+ path.isNotBlank() && sourceSet.sourceRoots.any { root ->
+ Paths.get(path).startsWith(Paths.get(root.path))
}
+ }
}
private fun <T> T.toSourceSetDependent() = mapOf(sourceSet to this)
@@ -301,7 +301,7 @@ private class DokkaDescriptorVisitor(
sourceSets = listOf(sourceSet),
generics = descriptor.typeParameters.map { it.toTypeParameter() },
extra = PropertyContainer.withAll(
- (descriptor.additionalExtras() + (descriptor.backingField?.getAnnotationsAsExtraModifiers()
+ (descriptor.additionalExtras() + (descriptor.backingField?.getAnnotationsAsExtraModifiers()?.entries?.single()?.value
?: emptyList())).toProperty(),
descriptor.getAllAnnotations()
)
@@ -365,9 +365,11 @@ private class DokkaDescriptorVisitor(
documentation = descriptor.resolveDescriptorData().let { sourceSetDependent ->
if (descriptor.isPrimary) {
sourceSetDependent.map { entry ->
- Pair(entry.key, entry.value.copy(children = (entry.value.children.find { it is Constructor }?.root?.let { constructor ->
- listOf( Description(constructor) )
- } ?: emptyList<TagWrapper>()) + entry.value.children.filterIsInstance<Param>()))
+ Pair(
+ entry.key,
+ entry.value.copy(children = (entry.value.children.find { it is Constructor }?.root?.let { constructor ->
+ listOf(Description(constructor))
+ } ?: emptyList<TagWrapper>()) + entry.value.children.filterIsInstance<Param>()))
}.toMap()
} else {
sourceSetDependent
@@ -379,8 +381,9 @@ private class DokkaDescriptorVisitor(
sourceSets = listOf(sourceSet),
extra = PropertyContainer.withAll<DFunction>(descriptor.additionalExtras(), descriptor.getAnnotations())
.let {
- if(descriptor.isPrimary) { it + PrimaryConstructorExtra }
- else it
+ if (descriptor.isPrimary) {
+ it + PrimaryConstructorExtra
+ } else it
}
)
}
@@ -637,18 +640,19 @@ private class DokkaDescriptorVisitor(
private fun List<ExtraModifiers>.toProperty() =
AdditionalModifiers(this.toSet())
- private fun Annotated.getAnnotations() = getListOfAnnotations().let(::Annotations)
+ private fun Annotated.getAnnotations() = getListOfSourceSetDependentAnnotations().let(::Annotations)
- private fun Annotated.getListOfAnnotations() = annotations.map { it.toAnnotation() }
+ private fun Annotated.getListOfSourceSetDependentAnnotations() =
+ mapOf(sourceSet to annotations.mapNotNull { it.toAnnotation() })
- private fun ConstantValue<*>.toValue(): AnnotationParameterValue = when (this) {
- is ConstantsAnnotationValue -> AnnotationValue(value.toAnnotation())
- is ConstantsArrayValue -> ArrayValue(value.map { it.toValue() })
+ private fun ConstantValue<*>.toValue(): AnnotationParameterValue? = when (this) {
+ is ConstantsAnnotationValue -> value.toAnnotation()?.let { AnnotationValue(it) }
+ is ConstantsArrayValue -> ArrayValue(value.mapNotNull { it.toValue() })
is ConstantsEnumValue -> EnumValue(
fullEnumEntryName(),
DRI(enumClassId.packageFqName.asString(), fullEnumEntryName())
)
- is ConstantsKtClassValue -> when(value) {
+ is ConstantsKtClassValue -> when (value) {
is NormalClass -> (value as NormalClass).value.classId.let {
ClassValue(
it.relativeClassName.asString(),
@@ -665,19 +669,28 @@ private class DokkaDescriptorVisitor(
else -> StringValue(toString())
}
- private fun AnnotationDescriptor.toAnnotation() = Annotations.Annotation(
- DRI.from(annotationClass as DeclarationDescriptor),
- allValueArguments.map { it.key.asString() to it.value.toValue() }.toMap()
- )
+ private fun AnnotationDescriptor.toAnnotation(): Annotations.Annotation? =
+ DRI.from(annotationClass as DeclarationDescriptor)
+ .takeIf { it.classNames != "<ERROR CLASS>" }?.let {
+ Annotations.Annotation(
+ it,
+ allValueArguments.map { it.key.asString() to it.value.toValue() }.filter {
+ it.second != null
+ }.toMap() as Map<String, AnnotationParameterValue>
+ )
+ }
- private fun PropertyDescriptor.getAllAnnotations() =
- (getListOfAnnotations() + (backingField?.getListOfAnnotations() ?: emptyList())).let(::Annotations)
+ private fun PropertyDescriptor.getAllAnnotations(): Annotations =
+ (getListOfSourceSetDependentAnnotations() + (backingField?.getListOfSourceSetDependentAnnotations()
+ ?: emptyMap())).let(::Annotations)
- private fun FieldDescriptor.getAnnotationsAsExtraModifiers() = getAnnotations().content.mapNotNull {
- try {
- ExtraModifiers.valueOf(it.dri.classNames?.toLowerCase() ?: "")
- } catch (e: IllegalArgumentException) {
- null
+ private fun FieldDescriptor.getAnnotationsAsExtraModifiers() = getAnnotations().content.mapValues {
+ it.value.mapNotNull {
+ try {
+ ExtraModifiers.valueOf(it.dri.classNames?.toLowerCase() ?: "")
+ } catch (e: IllegalArgumentException) {
+ null
+ }
}
}
diff --git a/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt b/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt
index efd689ba..1f52b2d9 100644
--- a/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt
+++ b/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt
@@ -35,7 +35,7 @@ object DefaultPsiToDocumentableTranslator : SourceToDocumentableTranslator {
override fun invoke(sourceSetData: SourceSetData, context: DokkaContext): DModule {
- fun isFileInSourceRoots(file: File) : Boolean {
+ fun isFileInSourceRoots(file: File): Boolean {
return sourceSetData.sourceRoots.any { root -> file.path.startsWith(File(root.path).absolutePath) }
}
@@ -168,7 +168,8 @@ object DefaultPsiToDocumentableTranslator : SourceToDocumentableTranslator {
constructors.map { parseFunction(it, true) },
mapTypeParameters(dri),
listOf(sourceSetData),
- PropertyContainer.empty<DAnnotation>() + annotations.toList().toListOfAnnotations().let(::Annotations)
+ PropertyContainer.empty<DAnnotation>() + annotations.toList().toListOfAnnotations()
+ .let(::Annotations)
)
isEnum -> DEnum(
dri,
@@ -183,7 +184,8 @@ object DefaultPsiToDocumentableTranslator : SourceToDocumentableTranslator {
emptyList(),
emptyList(),
listOf(sourceSetData),
- PropertyContainer.empty<DEnumEntry>() + entry.annotations.toList().toListOfAnnotations().let(::Annotations)
+ PropertyContainer.empty<DEnumEntry>() + entry.annotations.toList().toListOfAnnotations()
+ .let(::Annotations)
)
},
documentation,
@@ -213,7 +215,8 @@ object DefaultPsiToDocumentableTranslator : SourceToDocumentableTranslator {
mapTypeParameters(dri),
ancestors,
listOf(sourceSetData),
- PropertyContainer.empty<DInterface>() + annotations.toList().toListOfAnnotations().let(::Annotations)
+ PropertyContainer.empty<DInterface>() + annotations.toList().toListOfAnnotations()
+ .let(::Annotations)
)
else -> DClass(
dri,
@@ -287,12 +290,12 @@ object DefaultPsiToDocumentableTranslator : SourceToDocumentableTranslator {
).toSet()
)
- private fun AdditionalModifiers.toListOfAnnotations() = this.content.map {
+ private fun AdditionalModifiers.toListOfAnnotations() = mapOf(sourceSetData to this.content.map {
if (it !is ExtraModifiers.JavaOnlyModifiers.Static)
Annotations.Annotation(DRI("kotlin.jvm", it.name.toLowerCase().capitalize()), emptyMap())
else
Annotations.Annotation(DRI("kotlin.jvm", "JvmStatic"), emptyMap())
- }
+ })
private fun getBound(type: PsiType): Bound =
cachedBounds.getOrPut(type.canonicalText) {
@@ -400,14 +403,15 @@ object DefaultPsiToDocumentableTranslator : SourceToDocumentableTranslator {
)
}
- private fun Collection<PsiAnnotation>.toListOfAnnotations() = filter { it !is KtLightAbstractAnnotation }.mapNotNull { it.toAnnotation() }
+ private fun Collection<PsiAnnotation>.toListOfAnnotations() =
+ mapOf(sourceSetData to filter { it !is KtLightAbstractAnnotation }.mapNotNull { it.toAnnotation() })
private fun JvmAnnotationAttribute.toValue(): AnnotationParameterValue = when (this) {
is PsiNameValuePair -> value?.toValue() ?: StringValue("")
else -> StringValue(this.attributeName)
}
- private fun PsiAnnotationMemberValue.toValue(): AnnotationParameterValue = when(this) {
+ private fun PsiAnnotationMemberValue.toValue(): AnnotationParameterValue = when (this) {
is PsiAnnotation -> AnnotationValue(toAnnotation())
is PsiArrayInitializerMemberValue -> ArrayValue(initializers.map { it.toValue() })
is PsiReferenceExpression -> EnumValue(
@@ -423,7 +427,8 @@ object DefaultPsiToDocumentableTranslator : SourceToDocumentableTranslator {
private fun PsiAnnotation.toAnnotation() = Annotations.Annotation(
driOfReference(),
- attributes.filter { it !is KtLightAbstractAnnotation }.mapNotNull { it.attributeName to it.toValue() }.toMap()
+ attributes.filter { it !is KtLightAbstractAnnotation }.mapNotNull { it.attributeName to it.toValue() }
+ .toMap()
)
private fun PsiElement.driOfReference() = getChildOfType<PsiJavaCodeReferenceElement>()?.resolve()?.let {