diff options
author | Andrzej Ratajczak <andrzej.ratajczak98@gmail.com> | 2020-05-28 21:12:50 +0200 |
---|---|---|
committer | Paweł Marks <pmarks@virtuslab.com> | 2020-06-12 14:15:24 +0200 |
commit | dd44b839eac1b7b647e97f2cc73dd96bd054713b (patch) | |
tree | 6ab5a393c2a7d926519626f5f45f5c8b8cd60505 /plugins/base/src/main/kotlin | |
parent | d7be30c841cb925fd0d6322ccdd9877169730b92 (diff) | |
download | dokka-dd44b839eac1b7b647e97f2cc73dd96bd054713b.tar.gz dokka-dd44b839eac1b7b647e97f2cc73dd96bd054713b.tar.bz2 dokka-dd44b839eac1b7b647e97f2cc73dd96bd054713b.zip |
Refactor of Annotations and ExtraModifiers to be platform depedent
Diffstat (limited to 'plugins/base/src/main/kotlin')
5 files changed, 241 insertions, 223 deletions
diff --git a/plugins/base/src/main/kotlin/signatures/JvmSignatureUtils.kt b/plugins/base/src/main/kotlin/signatures/JvmSignatureUtils.kt index 0cf085b8..7f0e985e 100644 --- a/plugins/base/src/main/kotlin/signatures/JvmSignatureUtils.kt +++ b/plugins/base/src/main/kotlin/signatures/JvmSignatureUtils.kt @@ -3,6 +3,7 @@ package org.jetbrains.dokka.base.signatures import org.jetbrains.dokka.base.translators.documentables.PageContentBuilder import org.jetbrains.dokka.model.* import org.jetbrains.dokka.model.properties.WithExtraProperties +import org.jetbrains.dokka.pages.* interface JvmSignatureUtils { @@ -10,7 +11,7 @@ interface JvmSignatureUtils { fun PageContentBuilder.DocumentableContentBuilder.annotationsInline(d: Documentable) - fun <T : Documentable> WithExtraProperties<T>.modifiers(): Set<ExtraModifiers> + fun <T : Documentable> WithExtraProperties<T>.modifiers(): SourceSetDependent<Set<ExtraModifiers>> fun Set<ExtraModifiers>.toSignatureString(): String = joinToString("") { it.name.toLowerCase() + " " } @@ -40,9 +41,11 @@ interface JvmSignatureUtils { else -> null }?.let { it.entries.forEach { - group(sourceSets = setOf(it.key)) { - it.value.filter { it !in ignored }.forEach { - operation(it) + it.value.filter { it !in ignored }.takeIf { it.isNotEmpty() }?.let { annotations -> + group(sourceSets = setOf(it.key), styles = setOf(TextStyle.Block)) { + annotations.forEach { + operation(it) + } } } } @@ -62,14 +65,16 @@ interface JvmSignatureUtils { link(a.dri.classNames!!, a.dri) text("(") a.params.entries.forEachIndexed { i, it -> - text(it.key + " = ") - when (renderAtStrategy) { - is All -> All - is Never, is OnlyOnce -> Never - }.let { strategy -> - valueToSignature(it.value, strategy, listBrackets, classExtension) + group(styles = setOf(TextStyle.Span)) { + text(it.key + " = ") + when (renderAtStrategy) { + is All -> All + is Never, is OnlyOnce -> Never + }.let { strategy -> + valueToSignature(it.value, strategy, listBrackets, classExtension) + } + if (i != a.params.entries.size - 1) text(", ") } - if (i != a.params.entries.size - 1) text(", ") } text(")") } diff --git a/plugins/base/src/main/kotlin/signatures/KotlinSignatureProvider.kt b/plugins/base/src/main/kotlin/signatures/KotlinSignatureProvider.kt index 5514f9e5..542f6a1a 100644 --- a/plugins/base/src/main/kotlin/signatures/KotlinSignatureProvider.kt +++ b/plugins/base/src/main/kotlin/signatures/KotlinSignatureProvider.kt @@ -67,34 +67,34 @@ class KotlinSignatureProvider(ctcc: CommentsToContentConverter, logger: DokkaLog private fun regularSignature(c: DClasslike, sourceSets: Set<SourceSetData> = c.sourceSets.toSet()) = contentBuilder.contentFor(c, ContentKind.Symbol, setOf(TextStyle.Monospace), sourceSets = sourceSets) { - group(styles = setOf(TextStyle.Block)) { - annotationsBlock(c) - platformText( - c.visibility, - sourceSets - ) { it.takeIf { it !in ignoredVisibilities }?.name?.let { "$it " } ?: "" } - if (c is DClass) { - platformText(c.modifier, sourceSets) { - if (it !in ignoredModifiers) - if (c.extra[AdditionalModifiers]?.content?.contains(ExtraModifiers.KotlinOnlyModifiers.Data) == true) "" - else (if (it is JavaModifier.Empty) KotlinModifier.Open else it).let { it.name + " " } - else - "" - } - } - when (c) { - is DClass -> text("class ") - is DInterface -> text("interface ") - is DEnum -> text("enum ") - is DObject -> text("object ") - is DAnnotation -> text("annotation class ") + annotationsBlock(c) + platformText( + c.visibility, + sourceSets + ) { it.takeIf { it !in ignoredVisibilities }?.name?.let { "$it " } ?: "" } + if (c is DClass) { + platformText(c.modifier, sourceSets) { + if (it !in ignoredModifiers) + if (c.extra[AdditionalModifiers]?.content?.contains(ExtraModifiers.KotlinOnlyModifiers.Data) == true) "" + else (if (it is JavaModifier.Empty) KotlinModifier.Open else it).let { it.name + " " } + else + "" } - link(c.name!!, c.dri) - if (c is WithGenerics) { - list(c.generics, prefix = "<", suffix = "> ") { - +buildSignature(it) - } + } + when (c) { + is DClass -> text("class ") + is DInterface -> text("interface ") + is DEnum -> text("enum ") + is DObject -> text("object ") + is DAnnotation -> text("annotation class ") + } + link(c.name!!, c.dri) + if (c is WithGenerics) { + list(c.generics, prefix = "<", suffix = "> ") { + +buildSignature(it) } + } + if (c is DClass) { if (c is WithConstructors) { val pConstructor = c.constructors.singleOrNull { it.extra[PrimaryConstructorExtra] != null } if (pConstructor?.annotations()?.isNotEmpty() == true) { @@ -115,11 +115,11 @@ class KotlinSignatureProvider(ctcc: CommentsToContentConverter, logger: DokkaLog signatureForProjection(it.type) } } - if (c is WithSupertypes) { - c.supertypes.filter { it.key in sourceSets }.map { (s, dris) -> - list(dris, prefix = " : ", sourceSets = setOf(s)) { - link(it.sureClassNames, it, sourceSets = setOf(s)) - } + } + if (c is WithSupertypes) { + c.supertypes.filter { it.key in sourceSets }.map { (s, dris) -> + list(dris, prefix = " : ", sourceSets = setOf(s)) { + link(it.sureClassNames, it, sourceSets = setOf(s)) } } } @@ -128,62 +128,58 @@ class KotlinSignatureProvider(ctcc: CommentsToContentConverter, logger: DokkaLog private fun propertySignature(p: DProperty, sourceSets: Set<SourceSetData> = p.sourceSets.toSet()) = contentBuilder.contentFor(p, ContentKind.Symbol, setOf(TextStyle.Monospace), sourceSets = sourceSets) { - group(styles = setOf(TextStyle.Block)) { - annotationsBlock(p) - platformText(p.visibility) { it.takeIf { it !in ignoredVisibilities }?.name?.let { "$it " } ?: "" } - platformText(p.modifier) { - it.takeIf { it !in ignoredModifiers }?.let { - if (it is JavaModifier.Empty) KotlinModifier.Open else it - }?.name?.let { "$it " } ?: "" - } - text(p.modifiers().toSignatureString()) - p.setter?.let { text("var ") } ?: text("val ") - list(p.generics, prefix = "<", suffix = "> ") { - +buildSignature(it) - } - p.receiver?.also { - signatureForProjection(it.type) - text(".") - } - link(p.name, p.dri) - text(": ") - signatureForProjection(p.type) + annotationsBlock(p) + platformText(p.visibility) { it.takeIf { it !in ignoredVisibilities }?.name?.let { "$it " } ?: "" } + platformText(p.modifier) { + it.takeIf { it !in ignoredModifiers }?.let { + if (it is JavaModifier.Empty) KotlinModifier.Open else it + }?.name?.let { "$it " } ?: "" + } + platformText(p.modifiers()) { it.toSignatureString() } + p.setter?.let { text("var ") } ?: text("val ") + list(p.generics, prefix = "<", suffix = "> ") { + +buildSignature(it) + } + p.receiver?.also { + signatureForProjection(it.type) + text(".") } + link(p.name, p.dri) + text(": ") + signatureForProjection(p.type) } private fun functionSignature(f: DFunction, sourceSets: Set<SourceSetData> = f.sourceSets.toSet()) = contentBuilder.contentFor(f, ContentKind.Symbol, setOf(TextStyle.Monospace), sourceSets = sourceSets) { - group(styles = setOf(TextStyle.Block)) { - annotationsBlock(f) - platformText(f.visibility) { it.takeIf { it !in ignoredVisibilities }?.name?.let { "$it " } ?: "" } - platformText(f.modifier) { - it.takeIf { it !in ignoredModifiers }?.let { - if (it is JavaModifier.Empty) KotlinModifier.Open else it - }?.name?.let { "$it " } ?: "" - } - text(f.modifiers().toSignatureString()) - text("fun ") - list(f.generics, prefix = "<", suffix = "> ") { - +buildSignature(it) - } - f.receiver?.also { - signatureForProjection(it.type) - text(".") - } - link(f.name, f.dri) - text("(") - list(f.parameters) { - annotationsInline(it) - text(it.modifiers().toSignatureString()) - text(it.name!!) - text(": ") - signatureForProjection(it.type) - } - text(")") - if (f.documentReturnType()) { - text(": ") - signatureForProjection(f.type) - } + annotationsBlock(f) + platformText(f.visibility) { it.takeIf { it !in ignoredVisibilities }?.name?.let { "$it " } ?: "" } + platformText(f.modifier) { + it.takeIf { it !in ignoredModifiers }?.let { + if (it is JavaModifier.Empty) KotlinModifier.Open else it + }?.name?.let { "$it " } ?: "" + } + platformText(f.modifiers()) { it.toSignatureString() } + text("fun ") + list(f.generics, prefix = "<", suffix = "> ") { + +buildSignature(it) + } + f.receiver?.also { + signatureForProjection(it.type) + text(".") + } + link(f.name, f.dri) + text("(") + list(f.parameters) { + annotationsInline(it) + platformText(it.modifiers()) { it.toSignatureString() } + text(it.name!!) + text(": ") + signatureForProjection(it.type) + } + text(")") + if (f.documentReturnType()) { + text(": ") + signatureForProjection(f.type) } } @@ -204,7 +200,7 @@ class KotlinSignatureProvider(ctcc: CommentsToContentConverter, logger: DokkaLog sourceSets = platforms.toSet() ) { platformText(t.visibility) { it.takeIf { it !in ignoredVisibilities }?.name?.let { "$it " } ?: "" } - text(t.modifiers().toSignatureString()) + platformText(t.modifiers()) { it.toSignatureString() } text("typealias ") signatureForProjection(t.type) text(" = ") diff --git a/plugins/base/src/main/kotlin/signatures/KotlinSignatureUtils.kt b/plugins/base/src/main/kotlin/signatures/KotlinSignatureUtils.kt index 48cfe304..1f323473 100644 --- a/plugins/base/src/main/kotlin/signatures/KotlinSignatureUtils.kt +++ b/plugins/base/src/main/kotlin/signatures/KotlinSignatureUtils.kt @@ -19,5 +19,7 @@ object KotlinSignatureUtils : JvmSignatureUtils { annotationsInlineWithIgnored(d, emptySet(), strategy, listBrackets, classExtension) override fun <T : Documentable> WithExtraProperties<T>.modifiers() = - extra[AdditionalModifiers]?.content?.filterIsInstance<ExtraModifiers.KotlinOnlyModifiers>()?.toSet() ?: emptySet() + extra[AdditionalModifiers]?.content?.entries?.map { + it.key to it.value.filterIsInstance<ExtraModifiers.KotlinOnlyModifiers>().toSet() + }?.toMap() ?: emptyMap() }
\ No newline at end of file diff --git a/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt b/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt index a8175405..bf92c849 100644 --- a/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt +++ b/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt @@ -146,7 +146,10 @@ private class DokkaDescriptorVisitor( generics = descriptor.declaredTypeParameters.map { it.toTypeParameter() }, companion = descriptor.companion(driWithPlatform), sourceSets = listOf(sourceSet), - extra = PropertyContainer.withAll(descriptor.additionalExtras(), descriptor.getAnnotations()) + extra = PropertyContainer.withAll( + descriptor.additionalExtras().toSourceSetDependent().toAdditionalModifiers(), + descriptor.getAnnotations().toSourceSetDependent().toAnnotations() + ) ) } @@ -169,7 +172,10 @@ private class DokkaDescriptorVisitor( supertypes = info.supertypes.toSourceSetDependent(), documentation = info.docs, sourceSets = listOf(sourceSet), - extra = PropertyContainer.withAll(descriptor.additionalExtras(), descriptor.getAnnotations()) + extra = PropertyContainer.withAll( + descriptor.additionalExtras().toSourceSetDependent().toAdditionalModifiers(), + descriptor.getAnnotations().toSourceSetDependent().toAnnotations() + ) ) } @@ -194,7 +200,10 @@ private class DokkaDescriptorVisitor( documentation = info.docs, companion = descriptor.companion(driWithPlatform), sourceSets = listOf(sourceSet), - extra = PropertyContainer.withAll(descriptor.additionalExtras(), descriptor.getAnnotations()) + extra = PropertyContainer.withAll( + descriptor.additionalExtras().toSourceSetDependent().toAdditionalModifiers(), + descriptor.getAnnotations().toSourceSetDependent().toAnnotations() + ) ) } @@ -213,8 +222,8 @@ private class DokkaDescriptorVisitor( sourceSets = listOf(sourceSet), expectPresentInSet = sourceSet.takeIf { isExpect }, extra = PropertyContainer.withAll( - descriptor.additionalExtras(), - descriptor.getAnnotations(), + descriptor.additionalExtras().toSourceSetDependent().toAdditionalModifiers(), + descriptor.getAnnotations().toSourceSetDependent().toAnnotations(), ConstructorValues(descriptor.getAppliedConstructorParameters().toSourceSetDependent()) ) ) @@ -233,7 +242,10 @@ private class DokkaDescriptorVisitor( properties = scope.properties(driWithPlatform), expectPresentInSet = null, sourceSets = listOf(sourceSet), - extra = PropertyContainer.withAll(descriptor.additionalExtras(), descriptor.getAnnotations()), + extra = PropertyContainer.withAll( + descriptor.additionalExtras().toSourceSetDependent().toAdditionalModifiers(), + descriptor.getAnnotations().toSourceSetDependent().toAnnotations() + ), companion = descriptor.companionObjectDescriptor?.let { objectDescriptor(it, driWithPlatform) }, visibility = descriptor.visibility.toDokkaVisibility().toSourceSetDependent(), generics = descriptor.declaredTypeParameters.map { it.toTypeParameter() }, @@ -271,7 +283,10 @@ private class DokkaDescriptorVisitor( modifier = descriptor.modifier().toSourceSetDependent(), companion = descriptor.companion(driWithPlatform), sourceSets = listOf(sourceSet), - extra = PropertyContainer.withAll(descriptor.additionalExtras(), descriptor.getAnnotations()) + extra = PropertyContainer.withAll( + descriptor.additionalExtras().toSourceSetDependent().toAdditionalModifiers(), + descriptor.getAnnotations().toSourceSetDependent().toAnnotations() + ) ) } @@ -301,9 +316,9 @@ private class DokkaDescriptorVisitor( sourceSets = listOf(sourceSet), generics = descriptor.typeParameters.map { it.toTypeParameter() }, extra = PropertyContainer.withAll( - (descriptor.additionalExtras() + (descriptor.backingField?.getAnnotationsAsExtraModifiers()?.entries?.single()?.value - ?: emptyList())).toProperty(), - descriptor.getAllAnnotations() + (descriptor.additionalExtras() + descriptor.getAnnotationsWithBackingField() + .toAdditionalExtras()).toSet().toSourceSetDependent().toAdditionalModifiers(), + descriptor.getAnnotationsWithBackingField().toSourceSetDependent().toAnnotations() ) ) } @@ -339,7 +354,8 @@ private class DokkaDescriptorVisitor( sourceSets = listOf(sourceSet), extra = PropertyContainer.withAll( InheritedFunction(isInherited), - descriptor.additionalExtras(), descriptor.getAnnotations() + descriptor.additionalExtras().toSourceSetDependent().toAdditionalModifiers(), + descriptor.getAnnotations().toSourceSetDependent().toAnnotations() ) ) } @@ -379,12 +395,14 @@ private class DokkaDescriptorVisitor( modifier = descriptor.modifier().toSourceSetDependent(), generics = descriptor.typeParameters.map { it.toTypeParameter() }, sourceSets = listOf(sourceSet), - extra = PropertyContainer.withAll<DFunction>(descriptor.additionalExtras(), descriptor.getAnnotations()) - .let { - if (descriptor.isPrimary) { - it + PrimaryConstructorExtra - } else it - } + extra = PropertyContainer.withAll<DFunction>( + descriptor.additionalExtras().toSourceSetDependent().toAdditionalModifiers(), + descriptor.getAnnotations().toSourceSetDependent().toAnnotations() + ).let { + if (descriptor.isPrimary) { + it + PrimaryConstructorExtra + } else it + } ) } @@ -398,7 +416,7 @@ private class DokkaDescriptorVisitor( expectPresentInSet = null, documentation = descriptor.resolveDescriptorData(), sourceSets = listOf(sourceSet), - extra = PropertyContainer.withAll(descriptor.getAnnotations()) + extra = PropertyContainer.withAll(descriptor.getAnnotations().toSourceSetDependent().toAnnotations()) ) private fun visitPropertyAccessorDescriptor( @@ -418,7 +436,10 @@ private class DokkaDescriptorVisitor( expectPresentInSet = sourceSet.takeIf { isExpect }, documentation = descriptor.resolveDescriptorData(), sourceSets = listOf(sourceSet), - extra = PropertyContainer.withAll(descriptor.additionalExtras(), getAllAnnotations()) + extra = PropertyContainer.withAll( + descriptor.additionalExtras().toSourceSetDependent().toAdditionalModifiers(), + getAnnotationsWithBackingField().toSourceSetDependent().toAnnotations() + ) ) val name = run { @@ -453,7 +474,10 @@ private class DokkaDescriptorVisitor( }, sources = descriptor.createSources(), sourceSets = listOf(sourceSet), - extra = PropertyContainer.withAll(descriptor.additionalExtras(), descriptor.getAnnotations()) + extra = PropertyContainer.withAll( + descriptor.additionalExtras().toSourceSetDependent().toAdditionalModifiers(), + descriptor.getAnnotations().toSourceSetDependent().toAnnotations() + ) ) } @@ -479,12 +503,11 @@ private class DokkaDescriptorVisitor( expectPresentInSet = null, documentation = descriptor.resolveDescriptorData(), sourceSets = listOf(sourceSet), - extra = PropertyContainer.withAll( - listOfNotNull( - descriptor.additionalExtras(), - descriptor.getAnnotations(), - descriptor.getDefaultValue()?.let { DefaultValue(it) }) - ) + extra = PropertyContainer.withAll(listOfNotNull( + descriptor.additionalExtras().toSourceSetDependent().toAdditionalModifiers(), + descriptor.getAnnotations().toSourceSetDependent().toAnnotations(), + descriptor.getDefaultValue()?.let { DefaultValue(it) } + )) ) private fun MemberScope.getContributedDescriptors(kindFilter: DescriptorKindFilter, shouldFilter: Boolean) = @@ -542,7 +565,7 @@ private class DokkaDescriptorVisitor( null, upperBounds.map { it.toBound() }, listOf(sourceSet), - extra = PropertyContainer.withAll(additionalExtras()) + extra = PropertyContainer.withAll(additionalExtras().toSourceSetDependent().toAdditionalModifiers()) ) private fun KotlinType.toBound(): Bound = when (this) { @@ -606,28 +629,26 @@ private class DokkaDescriptorVisitor( ExtraModifiers.KotlinOnlyModifiers.TailRec.takeIf { isTailrec }, ExtraModifiers.KotlinOnlyModifiers.External.takeIf { isExternal }, ExtraModifiers.KotlinOnlyModifiers.Override.takeIf { DescriptorUtils.isOverride(this) } - ).toProperty() + ).toSet() private fun ClassDescriptor.additionalExtras() = listOfNotNull( ExtraModifiers.KotlinOnlyModifiers.Inline.takeIf { isInline }, ExtraModifiers.KotlinOnlyModifiers.External.takeIf { isExternal }, ExtraModifiers.KotlinOnlyModifiers.Inner.takeIf { isInner }, ExtraModifiers.KotlinOnlyModifiers.Data.takeIf { isData } - ).toProperty() - - private fun ValueParameterDescriptor.additionalExtras() = - listOfNotNull( - ExtraModifiers.KotlinOnlyModifiers.NoInline.takeIf { isNoinline }, - ExtraModifiers.KotlinOnlyModifiers.CrossInline.takeIf { isCrossinline }, - ExtraModifiers.KotlinOnlyModifiers.Const.takeIf { isConst }, - ExtraModifiers.KotlinOnlyModifiers.LateInit.takeIf { isLateInit }, - ExtraModifiers.KotlinOnlyModifiers.VarArg.takeIf { isVararg } - ).toProperty() - - private fun TypeParameterDescriptor.additionalExtras() = - listOfNotNull( - ExtraModifiers.KotlinOnlyModifiers.Reified.takeIf { isReified } - ).toProperty() + ).toSet() + + private fun ValueParameterDescriptor.additionalExtras() = listOfNotNull( + ExtraModifiers.KotlinOnlyModifiers.NoInline.takeIf { isNoinline }, + ExtraModifiers.KotlinOnlyModifiers.CrossInline.takeIf { isCrossinline }, + ExtraModifiers.KotlinOnlyModifiers.Const.takeIf { isConst }, + ExtraModifiers.KotlinOnlyModifiers.LateInit.takeIf { isLateInit }, + ExtraModifiers.KotlinOnlyModifiers.VarArg.takeIf { isVararg } + ).toSet() + + private fun TypeParameterDescriptor.additionalExtras() = listOfNotNull( + ExtraModifiers.KotlinOnlyModifiers.Reified.takeIf { isReified } + ).toSet() private fun PropertyDescriptor.additionalExtras() = listOfNotNull( ExtraModifiers.KotlinOnlyModifiers.Const.takeIf { isConst }, @@ -637,13 +658,7 @@ private class DokkaDescriptorVisitor( ExtraModifiers.KotlinOnlyModifiers.Override.takeIf { DescriptorUtils.isOverride(this) } ) - private fun List<ExtraModifiers>.toProperty() = - AdditionalModifiers(this.toSet()) - - private fun Annotated.getAnnotations() = getListOfSourceSetDependentAnnotations().let(::Annotations) - - private fun Annotated.getListOfSourceSetDependentAnnotations() = - mapOf(sourceSet to annotations.mapNotNull { it.toAnnotation() }) + private fun Annotated.getAnnotations() = annotations.mapNotNull { it.toAnnotation() } private fun ConstantValue<*>.toValue(): AnnotationParameterValue? = when (this) { is ConstantsAnnotationValue -> value.toAnnotation()?.let { AnnotationValue(it) } @@ -670,30 +685,31 @@ private class DokkaDescriptorVisitor( } 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> - ) - } + if(annotationClass?.visibility == Visibilities.PUBLIC) + 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> + ) + } + else + null - private fun PropertyDescriptor.getAllAnnotations(): Annotations = - (getListOfSourceSetDependentAnnotations() + (backingField?.getListOfSourceSetDependentAnnotations() - ?: emptyMap())).let(::Annotations) + private fun PropertyDescriptor.getAnnotationsWithBackingField(): List<Annotations.Annotation> = + getAnnotations() + (backingField?.getAnnotations() ?: emptyList()) - private fun FieldDescriptor.getAnnotationsAsExtraModifiers() = getAnnotations().content.mapValues { - it.value.mapNotNull { - try { - ExtraModifiers.valueOf(it.dri.classNames?.toLowerCase() ?: "") - } catch (e: IllegalArgumentException) { - null - } + private fun List<Annotations.Annotation>.toAdditionalExtras() = mapNotNull { + try { + ExtraModifiers.valueOf(it.dri.classNames?.toLowerCase() ?: "") + } catch (e: IllegalArgumentException) { + null } } + private fun ValueParameterDescriptor.getDefaultValue(): String? = (source as? KotlinSourceElement)?.psi?.children?.find { it is KtExpression }?.text diff --git a/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt b/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt index 1f52b2d9..d68f0e01 100644 --- a/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt +++ b/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt @@ -116,8 +116,7 @@ object DefaultPsiToDocumentableTranslator : SourceToDocumentableTranslator { return false } - private fun <T> T.toPlatformDependant() = - mapOf(sourceSetData to this) + private fun <T> T.toSourceSetDependent() = mapOf(sourceSetData to this) fun parseClasslike(psi: PsiClass, parent: DRI): DClasslike = with(psi) { val dri = parent.withClass(name.toString()) @@ -144,14 +143,14 @@ object DefaultPsiToDocumentableTranslator : SourceToDocumentableTranslator { } parseSupertypes(superTypes) val (regularFunctions, accessors) = splitFunctionsAndAccessors() - val documentation = javadocParser.parseDocumentation(this).toPlatformDependant() + val documentation = javadocParser.parseDocumentation(this).toSourceSetDependent() val allFunctions = regularFunctions.mapNotNull { if (!it.isConstructor) parseFunction(it) else null } + superMethods.map { parseFunction(it, isInherited = true) } - val source = PsiDocumentableSource(this).toPlatformDependant() + val source = PsiDocumentableSource(this).toSourceSetDependent() val classlikes = innerClasses.map { parseClasslike(it, dri) } - val visibility = getVisibility().toPlatformDependant() - val ancestors = ancestorsSet.toList().toPlatformDependant() - val modifiers = getModifier().toPlatformDependant() + val visibility = getVisibility().toSourceSetDependent() + val ancestors = ancestorsSet.toList().toSourceSetDependent() + val modifiers = getModifier().toSourceSetDependent() return when { isAnnotationType -> DAnnotation( @@ -168,8 +167,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().getAnnotations() + .toSourceSetDependent().toAnnotations() ) isEnum -> DEnum( dri, @@ -178,14 +177,14 @@ object DefaultPsiToDocumentableTranslator : SourceToDocumentableTranslator { DEnumEntry( dri.withClass("$name.${entry.name}"), entry.name.orEmpty(), - javadocParser.parseDocumentation(entry).toPlatformDependant(), + javadocParser.parseDocumentation(entry).toSourceSetDependent(), null, emptyList(), emptyList(), emptyList(), listOf(sourceSetData), - PropertyContainer.empty<DEnumEntry>() + entry.annotations.toList().toListOfAnnotations() - .let(::Annotations) + PropertyContainer.empty<DEnumEntry>() + entry.annotations.toList().getAnnotations() + .toSourceSetDependent().toAnnotations() ) }, documentation, @@ -199,7 +198,8 @@ object DefaultPsiToDocumentableTranslator : SourceToDocumentableTranslator { constructors.map { parseFunction(it, true) }, ancestors, listOf(sourceSetData), - PropertyContainer.empty<DEnum>() + annotations.toList().toListOfAnnotations().let(::Annotations) + PropertyContainer.empty<DEnum>() + annotations.toList().getAnnotations().toSourceSetDependent() + .toAnnotations() ) isInterface -> DInterface( dri, @@ -215,8 +215,8 @@ object DefaultPsiToDocumentableTranslator : SourceToDocumentableTranslator { mapTypeParameters(dri), ancestors, listOf(sourceSetData), - PropertyContainer.empty<DInterface>() + annotations.toList().toListOfAnnotations() - .let(::Annotations) + PropertyContainer.empty<DInterface>() + annotations.toList().getAnnotations().toSourceSetDependent() + .toAnnotations() ) else -> DClass( dri, @@ -234,7 +234,8 @@ object DefaultPsiToDocumentableTranslator : SourceToDocumentableTranslator { null, modifiers, listOf(sourceSetData), - PropertyContainer.empty<DClass>() + annotations.toList().toListOfAnnotations().let(::Annotations) + PropertyContainer.empty<DClass>() + annotations.toList().getAnnotations().toSourceSetDependent() + .toAnnotations() ) } } @@ -253,49 +254,49 @@ object DefaultPsiToDocumentableTranslator : SourceToDocumentableTranslator { DParameter( dri.copy(target = dri.target.nextTarget()), psiParameter.name, - javadocParser.parseDocumentation(psiParameter).toPlatformDependant(), + javadocParser.parseDocumentation(psiParameter).toSourceSetDependent(), null, getBound(psiParameter.type), listOf(sourceSetData) ) }, - javadocParser.parseDocumentation(psi).toPlatformDependant(), + javadocParser.parseDocumentation(psi).toSourceSetDependent(), null, - PsiDocumentableSource(psi).toPlatformDependant(), - psi.getVisibility().toPlatformDependant(), + PsiDocumentableSource(psi).toSourceSetDependent(), + psi.getVisibility().toSourceSetDependent(), psi.returnType?.let { getBound(type = it) } ?: Void, psi.mapTypeParameters(dri), null, - psi.getModifier().toPlatformDependant(), + psi.getModifier().toSourceSetDependent(), listOf(sourceSetData), psi.additionalExtras().let { PropertyContainer.withAll( InheritedFunction(isInherited), - it, - (psi.annotations.toList().toListOfAnnotations() + it.toListOfAnnotations()).let(::Annotations) + it.toSourceSetDependent().toAdditionalModifiers(), + (psi.annotations.toList().getAnnotations() + it.getAnnotations()).toSourceSetDependent() + .toAnnotations() ) } ) } - private fun PsiModifierListOwner.additionalExtras() = AdditionalModifiers( - listOfNotNull( - ExtraModifiers.JavaOnlyModifiers.Static.takeIf { hasModifier(JvmModifier.STATIC) }, - ExtraModifiers.JavaOnlyModifiers.Native.takeIf { hasModifier(JvmModifier.NATIVE) }, - ExtraModifiers.JavaOnlyModifiers.Synchronized.takeIf { hasModifier(JvmModifier.SYNCHRONIZED) }, - ExtraModifiers.JavaOnlyModifiers.StrictFP.takeIf { hasModifier(JvmModifier.STRICTFP) }, - ExtraModifiers.JavaOnlyModifiers.Transient.takeIf { hasModifier(JvmModifier.TRANSIENT) }, - ExtraModifiers.JavaOnlyModifiers.Volatile.takeIf { hasModifier(JvmModifier.VOLATILE) }, - ExtraModifiers.JavaOnlyModifiers.Transitive.takeIf { hasModifier(JvmModifier.TRANSITIVE) } - ).toSet() - ) + private fun PsiModifierListOwner.additionalExtras() = listOfNotNull( + ExtraModifiers.JavaOnlyModifiers.Static.takeIf { hasModifier(JvmModifier.STATIC) }, + ExtraModifiers.JavaOnlyModifiers.Native.takeIf { hasModifier(JvmModifier.NATIVE) }, + ExtraModifiers.JavaOnlyModifiers.Synchronized.takeIf { hasModifier(JvmModifier.SYNCHRONIZED) }, + ExtraModifiers.JavaOnlyModifiers.StrictFP.takeIf { hasModifier(JvmModifier.STRICTFP) }, + ExtraModifiers.JavaOnlyModifiers.Transient.takeIf { hasModifier(JvmModifier.TRANSIENT) }, + ExtraModifiers.JavaOnlyModifiers.Volatile.takeIf { hasModifier(JvmModifier.VOLATILE) }, + ExtraModifiers.JavaOnlyModifiers.Transitive.takeIf { hasModifier(JvmModifier.TRANSITIVE) } + ).toSet() + - private fun AdditionalModifiers.toListOfAnnotations() = mapOf(sourceSetData to this.content.map { + private fun Set<ExtraModifiers>.getAnnotations() = 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) { @@ -346,7 +347,7 @@ object DefaultPsiToDocumentableTranslator : SourceToDocumentableTranslator { DTypeParameter( dri.copy(target = dri.target.nextTarget()), type.name.orEmpty(), - javadocParser.parseDocumentation(type).toPlatformDependant(), + javadocParser.parseDocumentation(type).toSourceSetDependent(), null, mapBounds(type.bounds), listOf(sourceSetData) @@ -383,41 +384,39 @@ object DefaultPsiToDocumentableTranslator : SourceToDocumentableTranslator { return DProperty( dri, psi.name!!, // TODO: Investigate if this is indeed nullable - javadocParser.parseDocumentation(psi).toPlatformDependant(), + javadocParser.parseDocumentation(psi).toSourceSetDependent(), null, - PsiDocumentableSource(psi).toPlatformDependant(), - psi.getVisibility().toPlatformDependant(), + PsiDocumentableSource(psi).toSourceSetDependent(), + psi.getVisibility().toSourceSetDependent(), getBound(psi.type), null, accessors.firstOrNull { it.hasParameters() }?.let { parseFunction(it) }, accessors.firstOrNull { it.returnType == psi.type }?.let { parseFunction(it) }, - psi.getModifier().toPlatformDependant(), + psi.getModifier().toSourceSetDependent(), listOf(sourceSetData), emptyList(), psi.additionalExtras().let { PropertyContainer.withAll<DProperty>( - it, - (psi.annotations.toList().toListOfAnnotations() + it.toListOfAnnotations()).let(::Annotations) + it.toSourceSetDependent().toAdditionalModifiers(), + (psi.annotations.toList().getAnnotations() + it.getAnnotations()).toSourceSetDependent() + .toAnnotations() ) } ) } - private fun Collection<PsiAnnotation>.toListOfAnnotations() = - mapOf(sourceSetData to filter { it !is KtLightAbstractAnnotation }.mapNotNull { it.toAnnotation() }) + private fun Collection<PsiAnnotation>.getAnnotations() = + 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) { - is PsiAnnotation -> AnnotationValue(toAnnotation()) - is PsiArrayInitializerMemberValue -> ArrayValue(initializers.map { it.toValue() }) - is PsiReferenceExpression -> EnumValue( - text ?: "", - driOfReference() - ) + private fun PsiAnnotationMemberValue.toValue(): AnnotationParameterValue? = when (this) { + is PsiAnnotation -> toAnnotation()?.let { AnnotationValue(it) } + is PsiArrayInitializerMemberValue -> ArrayValue(initializers.mapNotNull { it.toValue() }) + is PsiReferenceExpression -> driOfReference()?.let { EnumValue(text ?: "", it) } is PsiClassObjectAccessExpression -> ClassValue( text ?: "", DRI.from(((type as PsiImmediateClassType).parameters.single() as PsiClassReferenceType).resolve()!!) @@ -425,16 +424,16 @@ object DefaultPsiToDocumentableTranslator : SourceToDocumentableTranslator { else -> StringValue(text ?: "") } - private fun PsiAnnotation.toAnnotation() = Annotations.Annotation( - driOfReference(), - attributes.filter { it !is KtLightAbstractAnnotation }.mapNotNull { it.attributeName to it.toValue() } - .toMap() - ) + private fun PsiAnnotation.toAnnotation() = driOfReference()?.let { + Annotations.Annotation( + it, + attributes.filter { it !is KtLightAbstractAnnotation }.mapNotNull { it.attributeName to it.toValue() } + .toMap() + ) + } private fun PsiElement.driOfReference() = getChildOfType<PsiJavaCodeReferenceElement>()?.resolve()?.let { DRI.from(it) - } ?: DRI("", getChildOfType<PsiJavaCodeReferenceElement>()?.qualifiedName ?: "").also { - logger.error("$this cannot be resolved to symbol!") } } } |