From 70000c87a37caa2a6b518a555f53c98514434403 Mon Sep 17 00:00:00 2001 From: Marcin Aman Date: Fri, 5 Feb 2021 14:55:45 +0100 Subject: Annotations for parameters (#1710) * Annotations for parameters * Annotations for parameters --- core/src/main/kotlin/model/Documentable.kt | 60 ++++++++++++---- core/src/main/kotlin/model/additionalExtras.kt | 8 +-- .../main/kotlin/signatures/JvmSignatureUtils.kt | 18 +++-- .../kotlin/signatures/KotlinSignatureProvider.kt | 25 +++++-- .../main/kotlin/signatures/KotlinSignatureUtils.kt | 5 +- .../DefaultDescriptorToDocumentableTranslator.kt | 83 +++++++++++++--------- .../psi/DefaultPsiToDocumentableTranslator.kt | 70 ++++++++++++------ .../annotations/ContentForAnnotationsTest.kt | 54 +++++++++++++- .../JavaAnnotationsForParametersTest.kt | 35 ++++++++- .../KotlinAnnotationsForParametersTest.kt | 23 ++++++ .../kotlin/converters/KotlinToJavaConverter.kt | 2 +- .../main/kotlin/signatures/JavaSignatureUtils.kt | 5 +- 12 files changed, 291 insertions(+), 97 deletions(-) diff --git a/core/src/main/kotlin/model/Documentable.kt b/core/src/main/kotlin/model/Documentable.kt index 62268fef..fcbb9591 100644 --- a/core/src/main/kotlin/model/Documentable.kt +++ b/core/src/main/kotlin/model/Documentable.kt @@ -6,8 +6,10 @@ import org.jetbrains.dokka.model.doc.DocumentationNode import org.jetbrains.dokka.model.properties.PropertyContainer import org.jetbrains.dokka.model.properties.WithExtraProperties +interface AnnotationTarget -abstract class Documentable : WithChildren { +abstract class Documentable : WithChildren, + AnnotationTarget { abstract val name: String? abstract val dri: DRI abstract val documentation: SourceSetDependent @@ -346,7 +348,14 @@ data class DTypeParameter( bounds: List, sourceSets: Set, extra: PropertyContainer = PropertyContainer.empty() - ) : this(Invariance(TypeParameter(dri, name, presentableName)), documentation, expectPresentInSet, bounds, sourceSets, extra) + ) : this( + Invariance(TypeParameter(dri, name, presentableName)), + documentation, + expectPresentInSet, + bounds, + sourceSets, + extra + ) override val dri: DRI by variantTypeParameter.inner::dri override val name: String by variantTypeParameter.inner::name @@ -376,8 +385,17 @@ data class DTypeAlias( } sealed class Projection -sealed class Bound : Projection() -data class TypeParameter(val dri: DRI, val name: String, val presentableName: String? = null) : Bound() +sealed class Bound : Projection(), AnnotationTarget +data class TypeParameter( + val dri: DRI, + val name: String, + val presentableName: String? = null, + override val extra: PropertyContainer = PropertyContainer.empty() +) : Bound(), WithExtraProperties { + override fun withNewExtras(newExtras: PropertyContainer): TypeParameter = + copy(extra = extra) +} + object Star : Projection() sealed class TypeConstructor : Bound() { @@ -389,49 +407,63 @@ sealed class TypeConstructor : Bound() { data class GenericTypeConstructor( override val dri: DRI, override val projections: List, - override val presentableName: String? = null -) : TypeConstructor() + override val presentableName: String? = null, + override val extra: PropertyContainer = PropertyContainer.empty() +) : TypeConstructor(), WithExtraProperties { + override fun withNewExtras(newExtras: PropertyContainer): GenericTypeConstructor = + copy(extra = newExtras) +} data class FunctionalTypeConstructor( override val dri: DRI, override val projections: List, val isExtensionFunction: Boolean = false, val isSuspendable: Boolean = false, - override val presentableName: String? = null -) : TypeConstructor() + override val presentableName: String? = null, + override val extra: PropertyContainer = PropertyContainer.empty(), +) : TypeConstructor(), WithExtraProperties { + override fun withNewExtras(newExtras: PropertyContainer): FunctionalTypeConstructor = + copy(extra = newExtras) +} data class Nullable(val inner: Bound) : Bound() sealed class Variance : Projection() { abstract val inner: T } + data class Covariance(override val inner: T) : Variance() { override fun toString() = "out" } + data class Contravariance(override val inner: T) : Variance() { override fun toString() = "in" } + data class Invariance(override val inner: T) : Variance() { override fun toString() = "" } data class TypeAliased(val typeAlias: Bound, val inner: Bound) : Bound() data class PrimitiveJavaType(val name: String) : Bound() + object Void : Bound() -object JavaObject : Bound() + +data class JavaObject(override val extra: PropertyContainer = PropertyContainer.empty()) : Bound(), + WithExtraProperties { + override fun withNewExtras(newExtras: PropertyContainer): JavaObject = + copy(extra = newExtras) +} + object Dynamic : Bound() data class UnresolvedBound(val name: String) : Bound() -fun Variance.withDri(dri: DRI) = when(this) { +fun Variance.withDri(dri: DRI) = when (this) { is Contravariance -> Contravariance(TypeParameter(dri, inner.name, inner.presentableName)) is Covariance -> Covariance(TypeParameter(dri, inner.name, inner.presentableName)) is Invariance -> Invariance(TypeParameter(dri, inner.name, inner.presentableName)) } -private fun String.shorten(maxLength: Int) = lineSequence().first().let { - if (it.length != length || it.length > maxLength) it.take(maxLength - 3) + "..." else it -} - fun Documentable.dfs(predicate: (Documentable) -> Boolean): Documentable? = if (predicate(this)) { this diff --git a/core/src/main/kotlin/model/additionalExtras.kt b/core/src/main/kotlin/model/additionalExtras.kt index c67ca7fd..f092572a 100644 --- a/core/src/main/kotlin/model/additionalExtras.kt +++ b/core/src/main/kotlin/model/additionalExtras.kt @@ -23,13 +23,13 @@ fun SourceSetDependent>.toAdditionalModifiers() = Additional data class Annotations( private val myContent: SourceSetDependent> -) : ExtraProperty { - companion object : ExtraProperty.Key { - override fun mergeStrategyFor(left: Annotations, right: Annotations): MergeStrategy = +) : ExtraProperty { + companion object : ExtraProperty.Key { + override fun mergeStrategyFor(left: Annotations, right: Annotations): MergeStrategy = MergeStrategy.Replace(Annotations(left.myContent + right.myContent)) } - override val key: ExtraProperty.Key = Annotations + override val key: ExtraProperty.Key = Annotations data class Annotation( val dri: DRI, diff --git a/plugins/base/src/main/kotlin/signatures/JvmSignatureUtils.kt b/plugins/base/src/main/kotlin/signatures/JvmSignatureUtils.kt index 3851b39c..a5aa6879 100644 --- a/plugins/base/src/main/kotlin/signatures/JvmSignatureUtils.kt +++ b/plugins/base/src/main/kotlin/signatures/JvmSignatureUtils.kt @@ -6,25 +6,25 @@ import org.jetbrains.dokka.model.* import org.jetbrains.dokka.model.properties.WithExtraProperties import org.jetbrains.dokka.pages.* import org.jetbrains.dokka.DokkaConfiguration.DokkaSourceSet -import org.jetbrains.dokka.base.signatures.KotlinSignatureUtils.driOrNull import org.jetbrains.dokka.base.signatures.KotlinSignatureUtils.drisOfAllNestedBounds +import org.jetbrains.dokka.model.AnnotationTarget interface JvmSignatureUtils { - fun PageContentBuilder.DocumentableContentBuilder.annotationsBlock(d: Documentable) + fun PageContentBuilder.DocumentableContentBuilder.annotationsBlock(d: AnnotationTarget) - fun PageContentBuilder.DocumentableContentBuilder.annotationsInline(d: Documentable) + fun PageContentBuilder.DocumentableContentBuilder.annotationsInline(d: AnnotationTarget) fun WithExtraProperties.modifiers(): SourceSetDependent> fun Collection.toSignatureString(): String = joinToString("") { it.name.toLowerCase() + " " } - fun WithExtraProperties.annotations(): SourceSetDependent> = + fun WithExtraProperties.annotations(): SourceSetDependent> = extra[Annotations]?.directAnnotations ?: emptyMap() private fun PageContentBuilder.DocumentableContentBuilder.annotations( - d: Documentable, + d: AnnotationTarget, ignored: Set, styles: Set