From 77fa898e8abdc8e74169fa789f2ba0e32d8fa9d7 Mon Sep 17 00:00:00 2001 From: Andrzej Ratajczak Date: Thu, 21 May 2020 11:29:22 +0200 Subject: Apply request changes --- .../main/kotlin/signatures/JvmSignatureUtils.kt | 123 +++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 plugins/base/src/main/kotlin/signatures/JvmSignatureUtils.kt (limited to 'plugins/base/src/main/kotlin/signatures/JvmSignatureUtils.kt') diff --git a/plugins/base/src/main/kotlin/signatures/JvmSignatureUtils.kt b/plugins/base/src/main/kotlin/signatures/JvmSignatureUtils.kt new file mode 100644 index 00000000..0ae72109 --- /dev/null +++ b/plugins/base/src/main/kotlin/signatures/JvmSignatureUtils.kt @@ -0,0 +1,123 @@ +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 + +interface JvmSignatureUtils { + + fun PageContentBuilder.DocumentableContentBuilder.annotationsBlock(d: Documentable) + + fun PageContentBuilder.DocumentableContentBuilder.annotationsInline(d: Documentable) + + fun WithExtraProperties.modifiers(): Set + + fun Set.toSignatureString(): String = + joinToString("") { it.name.toLowerCase() + " " } + + fun WithExtraProperties.annotations(): List = + extra[Annotations]?.content ?: emptyList() + + private fun Annotations.Annotation.toSignatureString(): String = + "@" + this.dri.classNames + "(" + this.params.entries.joinToString { it.key + "=" + it.value } + ")" + + private fun PageContentBuilder.DocumentableContentBuilder.annotations( + d: Documentable, + ignored: Set, + operation: (Annotations.Annotation) -> Unit + ): Unit = when (d) { + is DFunction -> d.annotations() + is DProperty -> d.annotations() + is DClass -> d.annotations() + is DInterface -> d.annotations() + is DObject -> d.annotations() + is DEnum -> d.annotations() + is DAnnotation -> d.annotations() + is DTypeParameter -> d.annotations() + is DEnumEntry -> d.annotations() + is DTypeAlias -> d.annotations() + is DParameter -> d.annotations() + else -> null + }?.let { + it.filter { it !in ignored }.forEach { + operation(it) + } + } ?: Unit + + fun PageContentBuilder.DocumentableContentBuilder.toSignatureString( + a: Annotations.Annotation, + renderAtStrategy: AtStrategy, + listBrackets: Pair, + classExtension: String + ) { + when (renderAtStrategy) { + is All, is OnlyOnce -> text("@") + is Never -> Unit + } + 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) + } + if (i != a.params.entries.size - 1) text(", ") + } + text(")") + } + + private fun PageContentBuilder.DocumentableContentBuilder.valueToSignature( + a: AnnotationParameterValue, + renderAtStrategy: AtStrategy, + listBrackets: Pair, + classExtension: String + ): Unit = when (a) { + is AnnotationValue -> toSignatureString(a.annotation, renderAtStrategy, listBrackets, classExtension) + is ArrayValue -> { + text(listBrackets.first.toString()) + a.value.forEachIndexed { i, it -> + valueToSignature(it, renderAtStrategy, listBrackets, classExtension) + if (i != a.value.size - 1) text(", ") + } + text(listBrackets.second.toString()) + } + is EnumValue -> link(a.enumName, a.enumDri) + is ClassValue -> link(a.className + classExtension, a.classDRI) + is StringValue -> text(a.value) + } + + fun PageContentBuilder.DocumentableContentBuilder.annotationsBlockWithIgnored( + d: Documentable, + ignored: Set, + renderAtStrategy: AtStrategy, + listBrackets: Pair, + classExtension: String + ) { + annotations(d, ignored) { + group { + toSignatureString(it, renderAtStrategy, listBrackets, classExtension) + } + } + } + + fun PageContentBuilder.DocumentableContentBuilder.annotationsInlineWithIgnored( + d: Documentable, + ignored: Set, + renderAtStrategy: AtStrategy, + listBrackets: Pair, + classExtension: String + ) { + annotations(d, ignored) { + toSignatureString(it, renderAtStrategy, listBrackets, classExtension) + text(Typography.nbsp.toString()) + } + } +} + +sealed class AtStrategy +object All : AtStrategy() +object OnlyOnce : AtStrategy() +object Never : AtStrategy() \ No newline at end of file -- cgit From d7be30c841cb925fd0d6322ccdd9877169730b92 Mon Sep 17 00:00:00 2001 From: Andrzej Ratajczak Date: Thu, 28 May 2020 16:04:20 +0200 Subject: Add platform dependent annotations --- core/src/main/kotlin/model/aditionalExtras.kt | 4 +- .../main/kotlin/signatures/JvmSignatureUtils.kt | 15 +++-- .../DeprecatedStrikethroughTransformer.kt | 53 +++++++++-------- .../DefaultDescriptorToDocumentableTranslator.kt | 69 +++++++++++++--------- .../psi/DefaultPsiToDocumentableTranslator.kt | 23 +++++--- 5 files changed, 97 insertions(+), 67 deletions(-) (limited to 'plugins/base/src/main/kotlin/signatures/JvmSignatureUtils.kt') diff --git a/core/src/main/kotlin/model/aditionalExtras.kt b/core/src/main/kotlin/model/aditionalExtras.kt index 1023545d..fcf1efa6 100644 --- a/core/src/main/kotlin/model/aditionalExtras.kt +++ b/core/src/main/kotlin/model/aditionalExtras.kt @@ -19,10 +19,10 @@ class AdditionalModifiers(val content: Set) : ExtraProperty = AdditionalModifiers } -class Annotations(val content: List) : ExtraProperty { +class Annotations(val content: SourceSetDependent>) : ExtraProperty { companion object : ExtraProperty.Key { override fun mergeStrategyFor(left: Annotations, right: Annotations): MergeStrategy = - MergeStrategy.Replace(Annotations((left.content + right.content).distinct())) + MergeStrategy.Replace(Annotations(left.content + right.content)) } override val key: ExtraProperty.Key = Annotations diff --git a/plugins/base/src/main/kotlin/signatures/JvmSignatureUtils.kt b/plugins/base/src/main/kotlin/signatures/JvmSignatureUtils.kt index 0ae72109..0cf085b8 100644 --- a/plugins/base/src/main/kotlin/signatures/JvmSignatureUtils.kt +++ b/plugins/base/src/main/kotlin/signatures/JvmSignatureUtils.kt @@ -15,8 +15,8 @@ interface JvmSignatureUtils { fun Set.toSignatureString(): String = joinToString("") { it.name.toLowerCase() + " " } - fun WithExtraProperties.annotations(): List = - extra[Annotations]?.content ?: emptyList() + fun WithExtraProperties.annotations(): SourceSetDependent> = + extra[Annotations]?.content ?: emptyMap() private fun Annotations.Annotation.toSignatureString(): String = "@" + this.dri.classNames + "(" + this.params.entries.joinToString { it.key + "=" + it.value } + ")" @@ -24,7 +24,7 @@ interface JvmSignatureUtils { private fun PageContentBuilder.DocumentableContentBuilder.annotations( d: Documentable, ignored: Set, - operation: (Annotations.Annotation) -> Unit + operation: PageContentBuilder.DocumentableContentBuilder.(Annotations.Annotation) -> Unit ): Unit = when (d) { is DFunction -> d.annotations() is DProperty -> d.annotations() @@ -39,8 +39,12 @@ interface JvmSignatureUtils { is DParameter -> d.annotations() else -> null }?.let { - it.filter { it !in ignored }.forEach { - operation(it) + it.entries.forEach { + group(sourceSets = setOf(it.key)) { + it.value.filter { it !in ignored }.forEach { + operation(it) + } + } } } ?: Unit @@ -50,6 +54,7 @@ interface JvmSignatureUtils { listBrackets: Pair, classExtension: String ) { + when (renderAtStrategy) { is All, is OnlyOnce -> text("@") is Never -> Unit diff --git a/plugins/base/src/main/kotlin/transformers/pages/annotations/DeprecatedStrikethroughTransformer.kt b/plugins/base/src/main/kotlin/transformers/pages/annotations/DeprecatedStrikethroughTransformer.kt index 96e2c907..f7d6f88f 100644 --- a/plugins/base/src/main/kotlin/transformers/pages/annotations/DeprecatedStrikethroughTransformer.kt +++ b/plugins/base/src/main/kotlin/transformers/pages/annotations/DeprecatedStrikethroughTransformer.kt @@ -1,5 +1,6 @@ package org.jetbrains.dokka.base.transformers.pages.annotations +import org.jetbrains.dokka.base.renderers.platforms import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.model.* import org.jetbrains.dokka.model.properties.WithExtraProperties @@ -9,17 +10,22 @@ import org.jetbrains.dokka.transformers.pages.PageTransformer class DeprecatedStrikethroughTransformer(val context: DokkaContext) : PageTransformer { override fun invoke(input: RootPageNode): RootPageNode = input.transformContentPagesTree { contentPage -> - if (contentPage.documentable?.isDeprecated() == true || contentPage.documentable?.hasDeprecatedChildren() == true) { - val deprecatedDRIs = - if (contentPage.documentable?.isDeprecated() == true) contentPage.dri else emptySet() + - contentPage.documentable?.children - ?.filter { it.isDeprecated() } - ?.map { it.dri } - ?.toSet().orEmpty() - - contentPage.modified(content = contentPage.content.addStrikethroughToSignature(deprecatedDRIs)) - } else { - contentPage + contentPage.platforms().fold(contentPage) { acc, sourceSetData -> + if (acc.documentable?.isDeprecated(sourceSetData) == true || acc.documentable?.hasDeprecatedChildren( + sourceSetData + ) == true + ) { + val deprecatedDRIs = + if (acc.documentable?.isDeprecated(sourceSetData) == true) contentPage.dri else emptySet() + + contentPage.documentable?.children + ?.filter { it.isDeprecated(sourceSetData) } + ?.map { it.dri } + ?.toSet().orEmpty() + + acc.modified(content = acc.content.addStrikethroughToSignature(deprecatedDRIs)) + } else { + acc + } } } @@ -34,23 +40,24 @@ class DeprecatedStrikethroughTransformer(val context: DokkaContext) : PageTransf else -> this } - private fun Documentable.isDeprecated(): Boolean = when (this) { - is DClass -> this.isKotlinOrJavaDeprecated() - is DAnnotation -> this.isKotlinOrJavaDeprecated() - is DObject -> this.isKotlinOrJavaDeprecated() - is DInterface -> this.isKotlinOrJavaDeprecated() - is DEnum -> this.isKotlinOrJavaDeprecated() - is DFunction -> this.isKotlinOrJavaDeprecated() - is DProperty -> this.isKotlinOrJavaDeprecated() - is DEnumEntry -> this.isKotlinOrJavaDeprecated() + private fun Documentable.isDeprecated(sourceSetData: SourceSetData) = when (this) { + is DClass -> this.isKotlinOrJavaDeprecated(sourceSetData) + is DAnnotation -> this.isKotlinOrJavaDeprecated(sourceSetData) + is DObject -> this.isKotlinOrJavaDeprecated(sourceSetData) + is DInterface -> this.isKotlinOrJavaDeprecated(sourceSetData) + is DEnum -> this.isKotlinOrJavaDeprecated(sourceSetData) + is DFunction -> this.isKotlinOrJavaDeprecated(sourceSetData) + is DProperty -> this.isKotlinOrJavaDeprecated(sourceSetData) + is DEnumEntry -> this.isKotlinOrJavaDeprecated(sourceSetData) else -> false } - private fun Documentable.hasDeprecatedChildren() = children.any { it.isDeprecated() } + private fun Documentable.hasDeprecatedChildren(sourceSetData: SourceSetData) = + children.any { it.isDeprecated(sourceSetData) } - private fun WithExtraProperties.isKotlinOrJavaDeprecated() = - extra[Annotations]?.content?.any { + private fun WithExtraProperties.isKotlinOrJavaDeprecated(sourceSetData: SourceSetData) = + extra[Annotations]?.content?.get(sourceSetData)?.any { it.dri == DRI("kotlin", "Deprecated") || it.dri == DRI("java.lang", "Deprecated") } == true 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.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.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()) + entry.value.children.filterIsInstance())) + Pair( + entry.key, + entry.value.copy(children = (entry.value.children.find { it is Constructor }?.root?.let { constructor -> + listOf(Description(constructor)) + } ?: emptyList()) + entry.value.children.filterIsInstance())) }.toMap() } else { sourceSetDependent @@ -379,8 +381,9 @@ private class DokkaDescriptorVisitor( sourceSets = listOf(sourceSet), extra = PropertyContainer.withAll(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.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 != "" }?.let { + Annotations.Annotation( + it, + allValueArguments.map { it.key.asString() to it.value.toValue() }.filter { + it.second != null + }.toMap() as Map + ) + } - 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() + annotations.toList().toListOfAnnotations().let(::Annotations) + PropertyContainer.empty() + annotations.toList().toListOfAnnotations() + .let(::Annotations) ) isEnum -> DEnum( dri, @@ -183,7 +184,8 @@ object DefaultPsiToDocumentableTranslator : SourceToDocumentableTranslator { emptyList(), emptyList(), listOf(sourceSetData), - PropertyContainer.empty() + entry.annotations.toList().toListOfAnnotations().let(::Annotations) + PropertyContainer.empty() + entry.annotations.toList().toListOfAnnotations() + .let(::Annotations) ) }, documentation, @@ -213,7 +215,8 @@ object DefaultPsiToDocumentableTranslator : SourceToDocumentableTranslator { mapTypeParameters(dri), ancestors, listOf(sourceSetData), - PropertyContainer.empty() + annotations.toList().toListOfAnnotations().let(::Annotations) + PropertyContainer.empty() + 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.toListOfAnnotations() = filter { it !is KtLightAbstractAnnotation }.mapNotNull { it.toAnnotation() } + private fun Collection.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()?.resolve()?.let { -- cgit From dd44b839eac1b7b647e97f2cc73dd96bd054713b Mon Sep 17 00:00:00 2001 From: Andrzej Ratajczak Date: Thu, 28 May 2020 21:12:50 +0200 Subject: Refactor of Annotations and ExtraModifiers to be platform depedent --- core/src/main/kotlin/model/aditionalExtras.kt | 6 +- .../main/kotlin/signatures/JvmSignatureUtils.kt | 27 ++-- .../kotlin/signatures/KotlinSignatureProvider.kt | 162 ++++++++++----------- .../main/kotlin/signatures/KotlinSignatureUtils.kt | 4 +- .../DefaultDescriptorToDocumentableTranslator.kt | 154 +++++++++++--------- .../psi/DefaultPsiToDocumentableTranslator.kt | 117 ++++++++------- .../annotations/ContentForAnnotationsTest.kt | 2 - plugins/base/src/test/kotlin/model/ClassesTest.kt | 38 ++--- .../base/src/test/kotlin/model/FunctionsTest.kt | 62 ++++---- plugins/base/src/test/kotlin/model/JavaTest.kt | 10 +- plugins/base/src/test/kotlin/model/PropertyTest.kt | 12 +- plugins/base/src/test/kotlin/utils/contentUtils.kt | 154 ++++++++++---------- .../kotlin/converters/KotlinToJavaConverter.kt | 29 +++- .../kotlin/signatures/JavaSignatureProvider.kt | 8 +- .../main/kotlin/signatures/JavaSignatureUtils.kt | 4 +- 15 files changed, 416 insertions(+), 373 deletions(-) (limited to 'plugins/base/src/main/kotlin/signatures/JvmSignatureUtils.kt') diff --git a/core/src/main/kotlin/model/aditionalExtras.kt b/core/src/main/kotlin/model/aditionalExtras.kt index fcf1efa6..b4355d4b 100644 --- a/core/src/main/kotlin/model/aditionalExtras.kt +++ b/core/src/main/kotlin/model/aditionalExtras.kt @@ -4,7 +4,7 @@ import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.model.properties.ExtraProperty import org.jetbrains.dokka.model.properties.MergeStrategy -class AdditionalModifiers(val content: Set) : ExtraProperty { +class AdditionalModifiers(val content: SourceSetDependent>) : ExtraProperty { companion object : ExtraProperty.Key { override fun mergeStrategyFor( left: AdditionalModifiers, @@ -19,6 +19,8 @@ class AdditionalModifiers(val content: Set) : ExtraProperty = AdditionalModifiers } +fun SourceSetDependent>.toAdditionalModifiers() = AdditionalModifiers(this) + class Annotations(val content: SourceSetDependent>) : ExtraProperty { companion object : ExtraProperty.Key { override fun mergeStrategyFor(left: Annotations, right: Annotations): MergeStrategy = @@ -37,6 +39,8 @@ class Annotations(val content: SourceSetDependent>) : ExtraProp } } +fun SourceSetDependent>.toAnnotations() = Annotations(this) + sealed class AnnotationParameterValue data class AnnotationValue(val annotation: Annotations.Annotation) : AnnotationParameterValue() data class ArrayValue(val value: List) : AnnotationParameterValue() 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 WithExtraProperties.modifiers(): Set + fun WithExtraProperties.modifiers(): SourceSetDependent> fun Set.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 = 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 = 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 = 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 WithExtraProperties.modifiers() = - extra[AdditionalModifiers]?.content?.filterIsInstance()?.toSet() ?: emptySet() + extra[AdditionalModifiers]?.content?.entries?.map { + it.key to it.value.filterIsInstance().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(descriptor.additionalExtras(), descriptor.getAnnotations()) - .let { - if (descriptor.isPrimary) { - it + PrimaryConstructorExtra - } else it - } + extra = PropertyContainer.withAll( + 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.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 != "" }?.let { - Annotations.Annotation( - it, - allValueArguments.map { it.key.asString() to it.value.toValue() }.filter { - it.second != null - }.toMap() as Map - ) - } + if(annotationClass?.visibility == Visibilities.PUBLIC) + DRI.from(annotationClass as DeclarationDescriptor) + .takeIf { it.classNames != "" }?.let { + Annotations.Annotation( + it, + allValueArguments.map { it.key.asString() to it.value.toValue() }.filter { + it.second != null + }.toMap() as Map + ) + } + else + null - private fun PropertyDescriptor.getAllAnnotations(): Annotations = - (getListOfSourceSetDependentAnnotations() + (backingField?.getListOfSourceSetDependentAnnotations() - ?: emptyMap())).let(::Annotations) + private fun PropertyDescriptor.getAnnotationsWithBackingField(): List = + 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.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.toPlatformDependant() = - mapOf(sourceSetData to this) + private fun 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() + annotations.toList().toListOfAnnotations() - .let(::Annotations) + PropertyContainer.empty() + 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() + entry.annotations.toList().toListOfAnnotations() - .let(::Annotations) + PropertyContainer.empty() + entry.annotations.toList().getAnnotations() + .toSourceSetDependent().toAnnotations() ) }, documentation, @@ -199,7 +198,8 @@ object DefaultPsiToDocumentableTranslator : SourceToDocumentableTranslator { constructors.map { parseFunction(it, true) }, ancestors, listOf(sourceSetData), - PropertyContainer.empty() + annotations.toList().toListOfAnnotations().let(::Annotations) + PropertyContainer.empty() + annotations.toList().getAnnotations().toSourceSetDependent() + .toAnnotations() ) isInterface -> DInterface( dri, @@ -215,8 +215,8 @@ object DefaultPsiToDocumentableTranslator : SourceToDocumentableTranslator { mapTypeParameters(dri), ancestors, listOf(sourceSetData), - PropertyContainer.empty() + annotations.toList().toListOfAnnotations() - .let(::Annotations) + PropertyContainer.empty() + annotations.toList().getAnnotations().toSourceSetDependent() + .toAnnotations() ) else -> DClass( dri, @@ -234,7 +234,8 @@ object DefaultPsiToDocumentableTranslator : SourceToDocumentableTranslator { null, modifiers, listOf(sourceSetData), - PropertyContainer.empty() + annotations.toList().toListOfAnnotations().let(::Annotations) + PropertyContainer.empty() + 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.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( - it, - (psi.annotations.toList().toListOfAnnotations() + it.toListOfAnnotations()).let(::Annotations) + it.toSourceSetDependent().toAdditionalModifiers(), + (psi.annotations.toList().getAnnotations() + it.getAnnotations()).toSourceSetDependent() + .toAnnotations() ) } ) } - private fun Collection.toListOfAnnotations() = - mapOf(sourceSetData to filter { it !is KtLightAbstractAnnotation }.mapNotNull { it.toAnnotation() }) + private fun Collection.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()?.resolve()?.let { DRI.from(it) - } ?: DRI("", getChildOfType()?.qualifiedName ?: "").also { - logger.error("$this cannot be resolved to symbol!") } } } diff --git a/plugins/base/src/test/kotlin/content/annotations/ContentForAnnotationsTest.kt b/plugins/base/src/test/kotlin/content/annotations/ContentForAnnotationsTest.kt index 98f572b4..d10bd151 100644 --- a/plugins/base/src/test/kotlin/content/annotations/ContentForAnnotationsTest.kt +++ b/plugins/base/src/test/kotlin/content/annotations/ContentForAnnotationsTest.kt @@ -149,6 +149,4 @@ class ContentForAnnotationsTest : AbstractCoreTest() { } } } - - } \ No newline at end of file diff --git a/plugins/base/src/test/kotlin/model/ClassesTest.kt b/plugins/base/src/test/kotlin/model/ClassesTest.kt index 4ca34b00..5616a6c3 100644 --- a/plugins/base/src/test/kotlin/model/ClassesTest.kt +++ b/plugins/base/src/test/kotlin/model/ClassesTest.kt @@ -1,5 +1,6 @@ package model +import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.model.* import org.jetbrains.dokka.model.KotlinModifier.* import org.junit.jupiter.api.Assertions.assertNull @@ -150,9 +151,9 @@ class ClassesTest : AbstractModelTest("/src/main/kotlin/classes/Test.kt", "class with((this / "classes" / "Klass").cast()) { name equals "Klass" visibility.values allEquals KotlinVisibility.Public - with(extra[AdditionalModifiers].assertNotNull("Extras")) { - content counts 1 - content.first() equals ExtraModifiers.KotlinOnlyModifiers.Data + with(extra[AdditionalModifiers]!!.content.entries.single().value.assertNotNull("Extras")) { + this counts 1 + first() equals ExtraModifiers.KotlinOnlyModifiers.Data } } } @@ -180,9 +181,9 @@ class ClassesTest : AbstractModelTest("/src/main/kotlin/classes/Test.kt", "class """ ) { with((this / "classes" / "Foo").cast()) { - with(extra[Annotations].assertNotNull("Annotations")) { - this.content counts 1 - with(content.first()) { + with(extra[Annotations]!!.content.entries.single().value.assertNotNull("Annotations")) { + this counts 1 + with(first()) { dri.classNames equals "Deprecated" params.entries counts 1 (params["message"].assertNotNull("message") as StringValue).value equals "\"should no longer be used\"" @@ -273,9 +274,9 @@ class ClassesTest : AbstractModelTest("/src/main/kotlin/classes/Test.kt", "class with((this / "classes" / "C").cast()) { with((this / "D").cast()) { - with(extra[AdditionalModifiers].assertNotNull("AdditionalModifiers")) { - content counts 1 - content.first() equals ExtraModifiers.KotlinOnlyModifiers.Inner + with(extra[AdditionalModifiers]!!.content.entries.single().value.assertNotNull("AdditionalModifiers")) { + this counts 1 + first() equals ExtraModifiers.KotlinOnlyModifiers.Inner } } } @@ -359,9 +360,9 @@ class ClassesTest : AbstractModelTest("/src/main/kotlin/classes/Test.kt", "class """ ) { with((this / "classes" / "C").cast()) { - with(extra[Annotations].assertNotNull("Annotations")) { - this.content counts 1 - with(content.first()) { + with(extra[Annotations]!!.content.entries.single().value.assertNotNull("Annotations")) { + this counts 1 + with(first()) { dri.classNames equals "SinceKotlin" params.entries counts 1 (params["version"].assertNotNull("version") as StringValue).value equals "\"1.1\"" @@ -424,7 +425,7 @@ class ClassesTest : AbstractModelTest("/src/main/kotlin/classes/Test.kt", "class """@Suppress("abc") class Foo() {}""" ) { with((this / "classes" / "Foo").cast()) { - with(extra[Annotations]?.content?.firstOrNull().assertNotNull("annotations")) { + with(extra[Annotations]!!.content.entries.single().value.firstOrNull().assertNotNull("annotations")) { dri.toString() equals "kotlin/Suppress///PointingToDeclaration/" (params["names"].assertNotNull("param") as ArrayValue).value equals listOf(StringValue("\"abc\"")) } @@ -444,11 +445,14 @@ class ClassesTest : AbstractModelTest("/src/main/kotlin/classes/Test.kt", "class """ ) { with((this / "classes" / "throws").cast()) { - with(extra[Annotations].assertNotNull("Annotations")) { - content counts 1 - with(content.first()) { + with(extra[Annotations]!!.content.entries.single().value.assertNotNull("Annotations")) { + this counts 1 + with(first()) { dri.classNames equals "Retention" -// params["value"].assertNotNull("value") equals "(java/lang/annotation/RetentionPolicy, SOURCE)" + params["value"].assertNotNull("value") equals EnumValue( + "RetentionPolicy.SOURCE", + DRI("java.lang.annotation", "RetentionPolicy.SOURCE") + ) } } } diff --git a/plugins/base/src/test/kotlin/model/FunctionsTest.kt b/plugins/base/src/test/kotlin/model/FunctionsTest.kt index 38f64df5..c96e7df6 100644 --- a/plugins/base/src/test/kotlin/model/FunctionsTest.kt +++ b/plugins/base/src/test/kotlin/model/FunctionsTest.kt @@ -141,9 +141,9 @@ class FunctionTest : AbstractModelTest("/src/main/kotlin/function/Test.kt", "fun """ ) { with((this / "function" / "f").cast()) { - with(extra[Annotations].assertNotNull("Annotations")) { - content counts 1 - with(content.first()) { + with(extra[Annotations]!!.content.entries.single().value.assertNotNull("Annotations")) { + this counts 1 + with(first()) { dri.classNames equals "Suppress" params.entries counts 1 (params["names"].assertNotNull("param") as ArrayValue).value equals listOf(StringValue("\"FOO\"")) @@ -161,8 +161,8 @@ class FunctionTest : AbstractModelTest("/src/main/kotlin/function/Test.kt", "fun """ ) { with((this / "function" / "f").cast()) { - extra[AdditionalModifiers]?.content counts 1 - extra[AdditionalModifiers]?.content exists ExtraModifiers.KotlinOnlyModifiers.Inline + extra[AdditionalModifiers]!!.content.entries.single().value counts 1 + extra[AdditionalModifiers]!!.content.entries.single().value exists ExtraModifiers.KotlinOnlyModifiers.Inline } } } @@ -175,8 +175,8 @@ class FunctionTest : AbstractModelTest("/src/main/kotlin/function/Test.kt", "fun """ ) { with((this / "function" / "f").cast()) { - extra[AdditionalModifiers]?.content counts 1 - extra[AdditionalModifiers]?.content exists ExtraModifiers.KotlinOnlyModifiers.Suspend + extra[AdditionalModifiers]!!.content.entries.single().value counts 1 + extra[AdditionalModifiers]!!.content.entries.single().value exists ExtraModifiers.KotlinOnlyModifiers.Suspend } } } @@ -189,9 +189,9 @@ class FunctionTest : AbstractModelTest("/src/main/kotlin/function/Test.kt", "fun """ ) { with((this / "function" / "f").cast()) { - extra[AdditionalModifiers]?.content counts 2 - extra[AdditionalModifiers]?.content exists ExtraModifiers.KotlinOnlyModifiers.Suspend - extra[AdditionalModifiers]?.content exists ExtraModifiers.KotlinOnlyModifiers.Inline + extra[AdditionalModifiers]!!.content.entries.single().value counts 2 + extra[AdditionalModifiers]!!.content.entries.single().value exists ExtraModifiers.KotlinOnlyModifiers.Suspend + extra[AdditionalModifiers]!!.content.entries.single().value exists ExtraModifiers.KotlinOnlyModifiers.Inline } } } @@ -204,10 +204,10 @@ class FunctionTest : AbstractModelTest("/src/main/kotlin/function/Test.kt", "fun """ ) { with((this / "function" / "f").cast()) { - with(extra[AdditionalModifiers].assertNotNull("AdditionalModifiers")) { - content counts 2 - content exists ExtraModifiers.KotlinOnlyModifiers.Suspend - content exists ExtraModifiers.KotlinOnlyModifiers.Inline + with(extra[AdditionalModifiers]!!.content.entries.single().value.assertNotNull("AdditionalModifiers")) { + this counts 2 + this exists ExtraModifiers.KotlinOnlyModifiers.Suspend + this exists ExtraModifiers.KotlinOnlyModifiers.Inline } } } @@ -226,9 +226,9 @@ class FunctionTest : AbstractModelTest("/src/main/kotlin/function/Test.kt", "fun """ ) { with((this / "function" / "Fancy").cast()) { - with(extra[Annotations].assertNotNull("Annotations")) { - content counts 3 - with(content.map { it.dri.classNames to it }.toMap()) { + with(extra[Annotations]!!.content.entries.single().value.assertNotNull("Annotations")) { + this counts 3 + with(map { it.dri.classNames to it }.toMap()) { with(this["Target"].assertNotNull("Target")) { (params["allowedTargets"].assertNotNull("allowedTargets") as ArrayValue).value equals listOf( EnumValue( @@ -249,9 +249,9 @@ class FunctionTest : AbstractModelTest("/src/main/kotlin/function/Test.kt", "fun } with((this / "function" / "function" / "notInlined").cast()) { - with(this.extra[Annotations].assertNotNull("Annotations")) { - content counts 1 - with(content.first()) { + with(this.extra[Annotations]!!.content.entries.single().value.assertNotNull("Annotations")) { + this counts 1 + with(first()) { dri.classNames equals "Fancy" params.entries counts 0 } @@ -268,8 +268,8 @@ class FunctionTest : AbstractModelTest("/src/main/kotlin/function/Test.kt", "fun """ ) { with((this / "function" / "f" / "notInlined").cast()) { - extra[AdditionalModifiers]?.content counts 1 - extra[AdditionalModifiers]?.content exists ExtraModifiers.KotlinOnlyModifiers.NoInline + extra[AdditionalModifiers]!!.content.entries.single().value counts 1 + extra[AdditionalModifiers]!!.content.entries.single().value exists ExtraModifiers.KotlinOnlyModifiers.NoInline } } } @@ -296,9 +296,9 @@ class FunctionTest : AbstractModelTest("/src/main/kotlin/function/Test.kt", "fun } } - with(extra[Annotations].assertNotNull("Annotations")) { - content counts 3 - with(content.map { it.dri.classNames to it }.toMap()) { + with(extra[Annotations]!!.content.entries.single().value.assertNotNull("Annotations")) { + this counts 3 + with(map { it.dri.classNames to it }.toMap()) { with(this["Target"].assertNotNull("Target")) { (params["allowedTargets"].assertNotNull("allowedTargets") as ArrayValue).value equals listOf( EnumValue( @@ -319,9 +319,9 @@ class FunctionTest : AbstractModelTest("/src/main/kotlin/function/Test.kt", "fun } with((this / "function" / "f").cast()) { - with(this.extra[Annotations].assertNotNull("Annotations")) { - content counts 1 - with(content.first()) { + with(this.extra[Annotations]!!.content.entries.single().value.assertNotNull("Annotations")) { + this counts 1 + with(this.first()) { dri.classNames equals "Fancy" params.entries counts 1 (params["size"] as StringValue).value equals "1" @@ -381,9 +381,9 @@ class FunctionTest : AbstractModelTest("/src/main/kotlin/function/Test.kt", "fun """ ) { with((this / "function" / "f").cast()) { - with(extra[Annotations].assertNotNull("Annotations")) { - this.content counts 1 - with(content.first()) { + with(extra[Annotations]!!.content.entries.single().value.assertNotNull("Annotations")) { + this counts 1 + with(first()) { dri.classNames equals "SinceKotlin" params.entries counts 1 (params["version"].assertNotNull("version") as StringValue).value equals "\"1.1\"" diff --git a/plugins/base/src/test/kotlin/model/JavaTest.kt b/plugins/base/src/test/kotlin/model/JavaTest.kt index fd293b5c..8f52fcc8 100644 --- a/plugins/base/src/test/kotlin/model/JavaTest.kt +++ b/plugins/base/src/test/kotlin/model/JavaTest.kt @@ -295,9 +295,9 @@ class JavaTest : AbstractModelTest("/src/main/kotlin/java/Test.java", "java") { """ ) { with((this / "java" / "C" / "foo").cast()) { - with(extra[AdditionalModifiers].assertNotNull("AdditionalModifiers")) { - content counts 1 - content.first() equals ExtraModifiers.JavaOnlyModifiers.Static + with(extra[AdditionalModifiers]!!.content.entries.single().value.assertNotNull("AdditionalModifiers")) { + this counts 1 + first() equals ExtraModifiers.JavaOnlyModifiers.Static } } } @@ -335,8 +335,8 @@ class JavaTest : AbstractModelTest("/src/main/kotlin/java/Test.java", "java") { """ ) { with((this / "java" / "Attribute").cast()) { - with(extra[Annotations].assertNotNull("Annotations")) { - with(content.single()) { + with(extra[Annotations]!!.content.entries.single().value.assertNotNull("Annotations")) { + with(single()) { dri.classNames equals "Target" (params["value"].assertNotNull("value") as ArrayValue).value equals listOf( EnumValue("ElementType.FIELD", DRI("java.lang.annotation", "ElementType")), diff --git a/plugins/base/src/test/kotlin/model/PropertyTest.kt b/plugins/base/src/test/kotlin/model/PropertyTest.kt index 90a9d782..e384b920 100644 --- a/plugins/base/src/test/kotlin/model/PropertyTest.kt +++ b/plugins/base/src/test/kotlin/model/PropertyTest.kt @@ -150,9 +150,9 @@ class PropertyTest : AbstractModelTest("/src/main/kotlin/property/Test.kt", "pro """ ) { with((this / "property" / "prop").cast()) { - with(extra[Annotations].assertNotNull("Annotations")) { - this.content counts 1 - with(content.first()) { + with(extra[Annotations]!!.content.entries.single().value.assertNotNull("Annotations")) { + this counts 1 + with(first()) { dri.classNames equals "SinceKotlin" params.entries counts 1 (params["version"].assertNotNull("version") as StringValue).value equals "\"1.1\"" @@ -178,9 +178,9 @@ class PropertyTest : AbstractModelTest("/src/main/kotlin/property/Test.kt", "pro } ) { with((this / "property" / "property").cast()) { - with(extra[Annotations].assertNotNull("Annotations")) { - this.content counts 1 - with(content.first()) { + with(extra[Annotations]!!.content.entries.single().value.assertNotNull("Annotations")) { + this counts 1 + with(first()) { dri.classNames equals "Strictfp" params.entries counts 0 } diff --git a/plugins/base/src/test/kotlin/utils/contentUtils.kt b/plugins/base/src/test/kotlin/utils/contentUtils.kt index 7e1b8bf4..c7cea1f1 100644 --- a/plugins/base/src/test/kotlin/utils/contentUtils.kt +++ b/plugins/base/src/test/kotlin/utils/contentUtils.kt @@ -28,36 +28,34 @@ fun ContentMatcherBuilder<*>.bareSignature( returnType: String? = null, vararg params: Pair ) = group { // TODO: remove it when double wrapping for signatures will be resolved - group { - annotations.entries.forEach { - group { - unwrapAnnotation(it) - } + annotations.entries.forEach { + group { + unwrapAnnotation(it) } - +("$visibility $modifier ${keywords.joinToString("") { "$it " }} fun") - link { +name } - +"(" - params.forEachIndexed { id, (n, t) -> - - t.annotations.forEach { - unwrapAnnotation(it) - } - t.keywords.forEach { - +it - } + } + +("$visibility $modifier ${keywords.joinToString("") { "$it " }} fun") + link { +name } + +"(" + params.forEachIndexed { id, (n, t) -> - +"$n:" - group { link { +(t.type) } } - if (id != params.lastIndex) - +", " + t.annotations.forEach { + unwrapAnnotation(it) } - +")" - if (returnType != null) { - +(": ") - group { - link { - +(returnType) - } + t.keywords.forEach { + +it + } + + +"$n:" + group { link { +(t.type) } } + if (id != params.lastIndex) + +", " + } + +")" + if (returnType != null) { + +(": ") + group { + link { + +(returnType) } } } @@ -87,40 +85,38 @@ fun ContentMatcherBuilder<*>.bareSignatureWithReceiver( returnType: String? = null, vararg params: Pair ) = group { // TODO: remove it when double wrapping for signatures will be resolved - group { - annotations.entries.forEach { - group { - unwrapAnnotation(it) - } - } - +("$visibility $modifier ${keywords.joinToString("") { "$it " }} fun") + annotations.entries.forEach { group { - link { +receiver } + unwrapAnnotation(it) } - +"." - link { +name } - +"(" - params.forEachIndexed { id, (n, t) -> - - t.annotations.forEach { - unwrapAnnotation(it) - } - t.keywords.forEach { - +it - } + } + +("$visibility $modifier ${keywords.joinToString("") { "$it " }} fun") + group { + link { +receiver } + } + +"." + link { +name } + +"(" + params.forEachIndexed { id, (n, t) -> - +"$n:" - group { link { +(t.type) } } - if (id != params.lastIndex) - +", " + t.annotations.forEach { + unwrapAnnotation(it) } - +")" - if (returnType != null) { - +(": ") - group { - link { - +(returnType) - } + t.keywords.forEach { + +it + } + + +"$n:" + group { link { +(t.type) } } + if (id != params.lastIndex) + +", " + } + +")" + if (returnType != null) { + +(": ") + group { + link { + +(returnType) } } } @@ -148,20 +144,18 @@ fun ContentMatcherBuilder<*>.propertySignature( link { +name } platformHinted { group { - group { - annotations.entries.forEach { - group { - unwrapAnnotation(it) - } + annotations.entries.forEach { + group { + unwrapAnnotation(it) } - +("$visibility $modifier ${keywords.joinToString("") { "$it " }} $preposition") - link { +name } - if (type != null) { - +(": ") - group { - link { - +(type) - } + } + +("$visibility $modifier ${keywords.joinToString("") { "$it " }} $preposition") + link { +name } + if (type != null) { + +(": ") + group { + link { + +(type) } } } @@ -184,15 +178,19 @@ fun ContentMatcherBuilder<*>.unnamedTag(tag: String, content: ContentMatcherBuil group { content() } } -fun ContentMatcherBuilder<*>.unwrapAnnotation(elem: Map.Entry>) { - +"@" - link { +elem.key } - +"(" - elem.value.forEach { - +("$it = ") - skipAllNotMatching() +private fun ContentMatcherBuilder<*>.unwrapAnnotation(elem: Map.Entry>) { + group { + +"@" + link { +elem.key } + +"(" + elem.value.forEach { + group { + +("$it = ") + skipAllNotMatching() + } + } + +")" } - +")" } data class ParamAttributes( diff --git a/plugins/kotlin-as-java/src/main/kotlin/converters/KotlinToJavaConverter.kt b/plugins/kotlin-as-java/src/main/kotlin/converters/KotlinToJavaConverter.kt index 8f20f942..693fffac 100644 --- a/plugins/kotlin-as-java/src/main/kotlin/converters/KotlinToJavaConverter.kt +++ b/plugins/kotlin-as-java/src/main/kotlin/converters/KotlinToJavaConverter.kt @@ -76,7 +76,13 @@ internal fun DProperty.asJava(isTopLevel: Boolean = false, relocateToClass: Stri type = type.asJava(), // TODO: check setter = null, getter = null, // Removing getters and setters as they will be available as functions - extra = if (isTopLevel) extra.plus(extra.mergeAdditionalModifiers(setOf(ExtraModifiers.JavaOnlyModifiers.Static))) else extra + extra = if (isTopLevel) extra + + extra.mergeAdditionalModifiers( + sourceSets.map { + it to setOf(ExtraModifiers.JavaOnlyModifiers.Static) + }.toMap() + ) + else extra ) internal fun DProperty.javaAccessors(isTopLevel: Boolean = false, relocateToClass: String? = null): List = @@ -95,7 +101,13 @@ internal fun DProperty.javaAccessors(isTopLevel: Boolean = false, relocateToClas }, visibility = visibility.mapValues { JavaVisibility.Public }, type = type.asJava(), // TODO: check - extra = if (isTopLevel) getter!!.extra.plus(getter!!.extra.mergeAdditionalModifiers(setOf(ExtraModifiers.JavaOnlyModifiers.Static))) else getter!!.extra + extra = if (isTopLevel) getter!!.extra + + getter!!.extra.mergeAdditionalModifiers( + sourceSets.map { + it to setOf(ExtraModifiers.JavaOnlyModifiers.Static) + }.toMap() + ) + else getter!!.extra ), setter?.copy( dri = if (relocateToClass.isNullOrBlank()) { @@ -111,7 +123,12 @@ internal fun DProperty.javaAccessors(isTopLevel: Boolean = false, relocateToClas }, visibility = visibility.mapValues { JavaVisibility.Public }, type = type.asJava(), // TODO: check - extra = if (isTopLevel) setter!!.extra.plus(setter!!.extra.mergeAdditionalModifiers(setOf(ExtraModifiers.JavaOnlyModifiers.Static))) else setter!!.extra + extra = if (isTopLevel) setter!!.extra + setter!!.extra.mergeAdditionalModifiers( + sourceSets.map { + it to setOf(ExtraModifiers.JavaOnlyModifiers.Static) + }.toMap() + ) + else setter!!.extra ) ) @@ -203,7 +220,9 @@ internal fun DObject.asJava(): DObject = copy( receiver = null, generics = emptyList(), expectPresentInSet = expectPresentInSet, - extra = PropertyContainer.empty() + AdditionalModifiers(setOf(ExtraModifiers.JavaOnlyModifiers.Static)) + extra = PropertyContainer.withAll(sourceSets.map { + mapOf(it to setOf(ExtraModifiers.JavaOnlyModifiers.Static)).toAdditionalModifiers() + }) ), classlikes = classlikes.map { it.asJava() }, supertypes = supertypes.mapValues { it.value.map { it.possiblyAsJava() } } @@ -248,7 +267,7 @@ internal fun ClassId.toDRI(dri: DRI?): DRI = DRI( target = PointingToDeclaration ) -private fun PropertyContainer.mergeAdditionalModifiers(second: Set) = +private fun PropertyContainer.mergeAdditionalModifiers(second: SourceSetDependent>) = this[AdditionalModifiers]?.squash(AdditionalModifiers(second)) ?: AdditionalModifiers(second) private fun AdditionalModifiers.squash(second: AdditionalModifiers) = diff --git a/plugins/kotlin-as-java/src/main/kotlin/signatures/JavaSignatureProvider.kt b/plugins/kotlin-as-java/src/main/kotlin/signatures/JavaSignatureProvider.kt index a55f9339..5c4985ac 100644 --- a/plugins/kotlin-as-java/src/main/kotlin/signatures/JavaSignatureProvider.kt +++ b/plugins/kotlin-as-java/src/main/kotlin/signatures/JavaSignatureProvider.kt @@ -41,7 +41,7 @@ class JavaSignatureProvider(ctcc: CommentsToContentConverter, logger: DokkaLogge if (c is DClass) { platformText(c.modifier) { it.takeIf { it !in ignoredModifiers }?.name.orEmpty() + " " } - text(c.modifiers().toSignatureString()) + platformText(c.modifiers()) { it.toSignatureString() } } when (c) { @@ -71,7 +71,7 @@ class JavaSignatureProvider(ctcc: CommentsToContentConverter, logger: DokkaLogge annotationsBlock(p) platformText(p.visibility) { (it.takeIf { it !in ignoredVisibilities }?.name ?: "") + " " } platformText(p.modifier) { it.name + " " } - text(p.modifiers().toSignatureString()) + platformText(p.modifiers()) { it.toSignatureString() } signatureForProjection(p.type) text(nbsp.toString()) link(p.name, p.dri) @@ -82,7 +82,7 @@ class JavaSignatureProvider(ctcc: CommentsToContentConverter, logger: DokkaLogge group(styles = setOf(TextStyle.Block)) { annotationsBlock(f) platformText(f.modifier) { it.takeIf { it !in ignoredModifiers }?.name.orEmpty() + " " } - text(f.modifiers().toSignatureString()) + platformText(f.modifiers()) { it.toSignatureString() } val returnType = f.type signatureForProjection(returnType) text(nbsp.toString()) @@ -92,7 +92,7 @@ class JavaSignatureProvider(ctcc: CommentsToContentConverter, logger: DokkaLogge } list(f.parameters, "(", ")") { annotationsInline(it) - text(it.modifiers().toSignatureString()) + platformText(it.modifiers()) { it.toSignatureString() } signatureForProjection(it.type) text(nbsp.toString()) link(it.name!!, it.dri) diff --git a/plugins/kotlin-as-java/src/main/kotlin/signatures/JavaSignatureUtils.kt b/plugins/kotlin-as-java/src/main/kotlin/signatures/JavaSignatureUtils.kt index e2f73bf9..b4245834 100644 --- a/plugins/kotlin-as-java/src/main/kotlin/signatures/JavaSignatureUtils.kt +++ b/plugins/kotlin-as-java/src/main/kotlin/signatures/JavaSignatureUtils.kt @@ -28,6 +28,8 @@ object JavaSignatureUtils : JvmSignatureUtils { annotationsInlineWithIgnored(d, ignoredAnnotations, strategy, listBrackets, classExtension) override fun WithExtraProperties.modifiers() = - extra[AdditionalModifiers]?.content?.filterIsInstance()?.toSet() ?: emptySet() + extra[AdditionalModifiers]?.content?.entries?.map { + it.key to it.value.filterIsInstance().toSet() + }?.toMap() ?: emptyMap() } \ No newline at end of file -- cgit From 3ca8ca75c60892de68f4fe18335aa79f4622c93c Mon Sep 17 00:00:00 2001 From: Marcin Aman Date: Wed, 3 Jun 2020 09:30:28 +0200 Subject: Stdlib wrapping in signature --- core/src/main/kotlin/pages/ContentNodes.kt | 2 +- .../src/main/kotlin/renderers/html/HtmlRenderer.kt | 9 +++++++++ plugins/base/src/main/kotlin/renderers/html/Tags.kt | 12 ++++++++++++ .../src/main/kotlin/signatures/JvmSignatureUtils.kt | 19 +++++++++++-------- .../main/kotlin/signatures/KotlinSignatureProvider.kt | 2 +- 5 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 plugins/base/src/main/kotlin/renderers/html/Tags.kt (limited to 'plugins/base/src/main/kotlin/signatures/JvmSignatureUtils.kt') diff --git a/core/src/main/kotlin/pages/ContentNodes.kt b/core/src/main/kotlin/pages/ContentNodes.kt index d6105bec..b9a50cb6 100644 --- a/core/src/main/kotlin/pages/ContentNodes.kt +++ b/core/src/main/kotlin/pages/ContentNodes.kt @@ -224,7 +224,7 @@ enum class ContentKind : Kind { } enum class TextStyle : Style { - Bold, Italic, Strong, Strikethrough, Paragraph, Block, Monospace, Indented, Cover, UnderCoverText + Bold, Italic, Strong, Strikethrough, Paragraph, Block, Span, Monospace, Indented, Cover, UnderCoverText, BreakableAfter, Breakable } enum class ContentStyle : Style { diff --git a/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt b/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt index 67fa2d91..42273d20 100644 --- a/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt +++ b/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt @@ -67,6 +67,15 @@ open class HtmlRenderer( childrenCallback() if (node.hasStyle(TextStyle.Monospace)) copyButton() } + node.hasStyle(TextStyle.BreakableAfter) -> { + span(){ childrenCallback() } + wbr { } + } + node.hasStyle(TextStyle.Breakable) -> { + span("breakable-word"){ childrenCallback() } + } + node.hasStyle(TextStyle.Span) -> span(){ childrenCallback() } + node.dci.kind == ContentKind.Symbol -> div("symbol $additionalClasses") { childrenCallback() } node.dci.kind == ContentKind.BriefComment -> div("brief $additionalClasses") { childrenCallback() } node.dci.kind == ContentKind.Cover -> div("cover $additionalClasses") { filterButtons(node) diff --git a/plugins/base/src/main/kotlin/renderers/html/Tags.kt b/plugins/base/src/main/kotlin/renderers/html/Tags.kt new file mode 100644 index 00000000..a3951eff --- /dev/null +++ b/plugins/base/src/main/kotlin/renderers/html/Tags.kt @@ -0,0 +1,12 @@ +package org.jetbrains.dokka.base.renderers.html + +import kotlinx.html.* + +@HtmlTagMarker +fun FlowOrPhrasingContent.wbr(classes : String? = null, block : WBR.() -> Unit = {}) : Unit = WBR(attributesMapOf("class", classes), consumer).visit(block) + +@Suppress("unused") +open class WBR(initialAttributes : Map, override val consumer : TagConsumer<*>) : HTMLTag("wbr", consumer, initialAttributes, null, true, false), + HtmlBlockInlineTag { + +} \ No newline at end of file diff --git a/plugins/base/src/main/kotlin/signatures/JvmSignatureUtils.kt b/plugins/base/src/main/kotlin/signatures/JvmSignatureUtils.kt index 7f0e985e..f8141110 100644 --- a/plugins/base/src/main/kotlin/signatures/JvmSignatureUtils.kt +++ b/plugins/base/src/main/kotlin/signatures/JvmSignatureUtils.kt @@ -25,6 +25,7 @@ interface JvmSignatureUtils { private fun PageContentBuilder.DocumentableContentBuilder.annotations( d: Documentable, ignored: Set, + styles: Set