From 3f64f853c1d193cc079384ed32d24d95e915354a Mon Sep 17 00:00:00 2001 From: Paweł Marks Date: Mon, 24 Feb 2020 16:44:22 +0100 Subject: Makes Documentables into property containers --- core/src/main/kotlin/model/Documentable.kt | 139 +++++++++++++-------- .../kotlin/model/properties/PropertyContainer.kt | 2 +- 2 files changed, 91 insertions(+), 50 deletions(-) (limited to 'core') diff --git a/core/src/main/kotlin/model/Documentable.kt b/core/src/main/kotlin/model/Documentable.kt index 121d0293..29792292 100644 --- a/core/src/main/kotlin/model/Documentable.kt +++ b/core/src/main/kotlin/model/Documentable.kt @@ -3,6 +3,8 @@ package org.jetbrains.dokka.model import com.intellij.psi.PsiNamedElement import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.model.doc.DocumentationNode +import org.jetbrains.dokka.model.properties.PropertyContainer +import org.jetbrains.dokka.model.properties.WithExtraProperties import org.jetbrains.dokka.pages.PlatformData import org.jetbrains.kotlin.descriptors.DeclarationDescriptor import org.jetbrains.kotlin.descriptors.Visibility @@ -15,13 +17,15 @@ abstract class Documentable { abstract val original: PlatformDependent override fun toString(): String = - "${javaClass.simpleName}($dri)" //+ briefDocTagString.takeIf { it.isNotBlank() }?.let { " [$it]" }.orEmpty() + "${javaClass.simpleName}($dri)" - override fun equals(other: Any?) = other is Documentable && this.dri == other.dri // TODO: https://github.com/Kotlin/dokka/pull/667#discussion_r382555806 + override fun equals(other: Any?) = + other is Documentable && this.dri == other.dri // TODO: https://github.com/Kotlin/dokka/pull/667#discussion_r382555806 override fun hashCode() = dri.hashCode() - val briefDocTagString: String by lazy { // TODO > utils + val briefDocTagString: String by lazy { + // TODO > utils documentation.values .firstOrNull() ?.children @@ -32,9 +36,11 @@ abstract class Documentable { } } -data class PlatformDependent(val map: Map, val expect: T? = null) : Map by map { +data class PlatformDependent( + val map: Map, val expect: T? = null +) : Map by map { val prevalentValue: T? - get() = if (map.all { values.first() == it.value }) values.first() else null + get() = map.values.distinct().singleOrNull() companion object { fun empty() = PlatformDependent(mapOf()) @@ -65,6 +71,7 @@ interface WithType { interface WithAbstraction { val modifier: Modifier + enum class Modifier { Abstract, Open, Final } @@ -90,32 +97,38 @@ abstract class Classlike : Documentable(), WithScope, WithVisibility, WithExpect abstract val supertypes: PlatformDependent> } -class Module( +data class Module( override val name: String, override val packages: List, override val documentation: PlatformDependent, - override val original: PlatformDependent = PlatformDependent.empty() -) : Documentable(), WithPackages { + override val original: PlatformDependent = PlatformDependent.empty(), + override val extra: PropertyContainer = PropertyContainer.empty() +) : Documentable(), WithPackages, WithExtraProperties { override val dri: DRI = DRI.topLevel override val children: List get() = packages + + override fun withNewExtras(newExtras: PropertyContainer) = copy(extra = newExtras) } -class Package( +data class Package( override val dri: DRI, override val functions: List, override val properties: List, override val classlikes: List, override val packages: List, override val documentation: PlatformDependent, - override val original: PlatformDependent = PlatformDependent.empty() -) : Documentable(), WithScope, WithPackages { + override val original: PlatformDependent = PlatformDependent.empty(), + override val extra: PropertyContainer = PropertyContainer.empty() +) : Documentable(), WithScope, WithPackages, WithExtraProperties { override val name = dri.packageName.orEmpty() override val children: List get() = (properties + functions + classlikes + packages) as List + + override fun withNewExtras(newExtras: PropertyContainer) = copy(extra = newExtras) } -class Class( +data class Class( override val dri: DRI, override val name: String, override val constructors: List, @@ -129,13 +142,16 @@ class Class( override val supertypes: PlatformDependent>, override val documentation: PlatformDependent, override val original: PlatformDependent = PlatformDependent.empty(), - override val modifier: WithAbstraction.Modifier -) : Classlike(), WithAbstraction, WithCompanion, WithConstructors, WithGenerics { + override val modifier: WithAbstraction.Modifier, + override val extra: PropertyContainer +) : Classlike(), WithAbstraction, WithCompanion, WithConstructors, WithGenerics, WithExtraProperties { override val children: List get() = (functions + properties + classlikes + listOfNotNull(companion) + constructors) as List + + override fun withNewExtras(newExtras: PropertyContainer) = copy(extra = newExtras) } -class Enum( +data class Enum( override val dri: DRI, override val name: String, val entries: List, @@ -148,26 +164,32 @@ class Enum( override val companion: Object?, override val constructors: List, override val supertypes: PlatformDependent>, - override val original: PlatformDependent = PlatformDependent.empty() -) : Classlike(), WithCompanion, WithConstructors { + override val original: PlatformDependent = PlatformDependent.empty(), + override val extra: PropertyContainer = PropertyContainer.empty() +) : Classlike(), WithCompanion, WithConstructors, WithExtraProperties { override val children: List get() = (entries + functions + properties + classlikes + listOfNotNull(companion) + constructors) as List + + override fun withNewExtras(newExtras: PropertyContainer) = copy(extra = newExtras) } -class EnumEntry( - override val name: String?, +data class EnumEntry( override val dri: DRI, + override val name: String?, override val documentation: PlatformDependent, override val functions: List, override val properties: List, override val classlikes: List, - override val original: PlatformDependent = PlatformDependent.empty() -) : Documentable(), WithScope { + override val original: PlatformDependent = PlatformDependent.empty(), + override val extra: PropertyContainer = PropertyContainer.empty() +) : Documentable(), WithScope, WithExtraProperties { override val children: List get() = (functions + properties + classlikes) as List + + override fun withNewExtras(newExtras: PropertyContainer) = copy(extra = newExtras) } -class Function( +data class Function( override val dri: DRI, override val name: String, val isConstructor: Boolean, @@ -180,15 +202,18 @@ class Function( override val generics: List, override val receiver: Parameter?, override val original: PlatformDependent = PlatformDependent.empty(), - override val modifier: WithAbstraction.Modifier - ) : Documentable(), Callable, WithGenerics { + override val modifier: WithAbstraction.Modifier, + override val extra: PropertyContainer = PropertyContainer.empty() + ) : Documentable(), Callable, WithGenerics, WithExtraProperties { override val children: List get() = parameters + + override fun withNewExtras(newExtras: PropertyContainer) = copy(extra = newExtras) } -class Interface( - override val name: String?, +data class Interface( override val dri: DRI, + override val name: String?, override val documentation: PlatformDependent, override val original: PlatformDependent, override val actual: PlatformDependent, @@ -198,13 +223,16 @@ class Interface( override val visibility: PlatformDependent, override val companion: Object?, override val generics: List, - override val supertypes: PlatformDependent> -) : Classlike(), WithCompanion, WithGenerics { + override val supertypes: PlatformDependent>, + override val extra: PropertyContainer = PropertyContainer.empty() +) : Classlike(), WithCompanion, WithGenerics, WithExtraProperties { override val children: List get() = (functions + properties + classlikes + listOfNotNull(companion)) as List + + override fun withNewExtras(newExtras: PropertyContainer) = copy(extra = newExtras) } -class Object( +data class Object( override val name: String?, override val dri: DRI, override val documentation: PlatformDependent, @@ -214,13 +242,16 @@ class Object( override val properties: List, override val classlikes: List, override val visibility: PlatformDependent, - override val supertypes: PlatformDependent> -) : Classlike() { + override val supertypes: PlatformDependent>, + override val extra: PropertyContainer = PropertyContainer.empty() +) : Classlike(), WithExtraProperties { override val children: List get() = (functions + properties + classlikes) as List + + override fun withNewExtras(newExtras: PropertyContainer) = copy(extra = newExtras) } -class Annotation( +data class Annotation( override val name: String?, override val dri: DRI, override val documentation: PlatformDependent, @@ -231,13 +262,18 @@ class Annotation( override val classlikes: List, override val visibility: PlatformDependent, override val companion: Object?, - override val constructors: List -) : Documentable(), WithScope, WithVisibility, WithCompanion, WithConstructors, WithExpectActual { + override val constructors: List, + override val extra: PropertyContainer = PropertyContainer.empty() +) : Documentable(), WithScope, WithVisibility, WithCompanion, WithConstructors, WithExpectActual, + WithExtraProperties { + override val children: List get() = (functions + properties + classlikes + constructors + listOfNotNull(companion)) as List + + override fun withNewExtras(newExtras: PropertyContainer) = copy(extra = newExtras) } -class Property( +data class Property( override val dri: DRI, override val name: String, override val documentation: PlatformDependent, @@ -247,33 +283,42 @@ class Property( override val receiver: Parameter?, val accessors: PlatformDependent, // TODO > extra override val original: PlatformDependent = PlatformDependent.empty(), - override val modifier: WithAbstraction.Modifier -) : Documentable(), Callable { + override val modifier: WithAbstraction.Modifier, + override val extra: PropertyContainer = PropertyContainer.empty() +) : Documentable(), Callable, WithExtraProperties { override val children: List get() = emptyList() + + override fun withNewExtras(newExtras: PropertyContainer) = copy(extra = newExtras) } // TODO: treat named Parameters and receivers differently -class Parameter( +data class Parameter( override val dri: DRI, override val name: String?, override val documentation: PlatformDependent, val type: TypeWrapper, - override val original: PlatformDependent -) : Documentable() { + override val original: PlatformDependent, + override val extra: PropertyContainer = PropertyContainer.empty() +) : Documentable(), WithExtraProperties { override val children: List get() = emptyList() + + override fun withNewExtras(newExtras: PropertyContainer) = copy(extra = newExtras) } -class TypeParameter( +data class TypeParameter( override val dri: DRI, override val name: String, override val documentation: PlatformDependent, override val original: PlatformDependent = PlatformDependent.empty(), - val bounds: List -): Documentable() { + val bounds: List, + override val extra: PropertyContainer +): Documentable(), WithExtraProperties { override val children: List get() = emptyList() + + override fun withNewExtras(newExtras: PropertyContainer) = copy(extra = newExtras) } sealed class Projection { @@ -294,10 +339,6 @@ fun Documentable.dfs(predicate: (Documentable) -> Boolean): Documentable? = this.children.asSequence().mapNotNull { it.dfs(predicate) }.firstOrNull() } -interface Extra -object STATIC : Extra -object INHERITED: Extra - sealed class DocumentableSource -class DescriptorDocumentableSource(val descriptor: DeclarationDescriptor): DocumentableSource() -class PsiDocumentableSource(val psi: PsiNamedElement): DocumentableSource() +class DescriptorDocumentableSource(val descriptor: DeclarationDescriptor) : DocumentableSource() +class PsiDocumentableSource(val psi: PsiNamedElement) : DocumentableSource() diff --git a/core/src/main/kotlin/model/properties/PropertyContainer.kt b/core/src/main/kotlin/model/properties/PropertyContainer.kt index ed4b99de..50216278 100644 --- a/core/src/main/kotlin/model/properties/PropertyContainer.kt +++ b/core/src/main/kotlin/model/properties/PropertyContainer.kt @@ -16,7 +16,7 @@ class PropertyContainer internal constructor( } companion object { - val empty: PropertyContainer = PropertyContainer(emptyMap()) + fun empty(): PropertyContainer = PropertyContainer(emptyMap()) } } -- cgit