From b05eb93d345cccd0cae9393f99acd428012d1106 Mon Sep 17 00:00:00 2001 From: Andrzej Ratajczak Date: Tue, 25 Aug 2020 14:51:53 +0200 Subject: Add handling for generic type parameters variance --- core/src/main/kotlin/model/Documentable.kt | 37 ++++++++++++++++++++++--- core/src/main/kotlin/model/documentableUtils.kt | 3 +- 2 files changed, 34 insertions(+), 6 deletions(-) (limited to 'core/src') diff --git a/core/src/main/kotlin/model/Documentable.kt b/core/src/main/kotlin/model/Documentable.kt index c9bf7760..2ea4a328 100644 --- a/core/src/main/kotlin/model/Documentable.kt +++ b/core/src/main/kotlin/model/Documentable.kt @@ -309,14 +309,27 @@ data class DParameter( } data class DTypeParameter( - override val dri: DRI, - override val name: String, + val variantTypeParameter: Variance, override val documentation: SourceSetDependent, override val expectPresentInSet: DokkaSourceSet?, val bounds: List, override val sourceSets: Set, override val extra: PropertyContainer = PropertyContainer.empty() ) : Documentable(), WithExtraProperties { + + constructor( + dri: DRI, + name: String, + documentation: SourceSetDependent, + expectPresentInSet: DokkaSourceSet?, + bounds: List, + sourceSets: Set, + extra: PropertyContainer = PropertyContainer.empty() + ) : this(Invariance(TypeParameter(dri, name)), documentation, expectPresentInSet, bounds, sourceSets, extra) + + override val dri: DRI by variantTypeParameter.inner::dri + override val name: String by variantTypeParameter.inner::name + override val children: List get() = emptyList() @@ -352,8 +365,18 @@ data class TypeConstructor( ) : Bound() data class Nullable(val inner: Bound) : Bound() -data class Variance(val kind: Kind, val inner: Bound) : Projection() { - enum class Kind { In, Out } + +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 PrimitiveJavaType(val name: String) : Bound() @@ -366,6 +389,12 @@ enum class FunctionModifiers { NONE, FUNCTION, EXTENSION } +fun Variance.withNewDri(dri: DRI) = when(this) { + is Contravariance -> Contravariance(TypeParameter(dri, inner.name)) + is Covariance -> Covariance(TypeParameter(dri, inner.name)) + is Invariance -> Invariance(TypeParameter(dri, inner.name)) +} + private fun String.shorten(maxLength: Int) = lineSequence().first().let { if (it.length != length || it.length > maxLength) it.take(maxLength - 3) + "..." else it } diff --git a/core/src/main/kotlin/model/documentableUtils.kt b/core/src/main/kotlin/model/documentableUtils.kt index 287cf313..e32605ca 100644 --- a/core/src/main/kotlin/model/documentableUtils.kt +++ b/core/src/main/kotlin/model/documentableUtils.kt @@ -11,8 +11,7 @@ fun DTypeParameter.filter(filteredSet: Set) = val intersection = filteredSet.intersect(sourceSets) if (intersection.isEmpty()) null else DTypeParameter( - dri, - name, + variantTypeParameter, documentation.filtered(intersection), expectPresentInSet?.takeIf { intersection.contains(expectPresentInSet) }, bounds, -- cgit