From 48077e9575ba6133b897ceb5794ade1ea0d8d84f Mon Sep 17 00:00:00 2001 From: Kamil Doległo Date: Fri, 21 Feb 2020 12:10:33 +0100 Subject: Refactor Documentable model --- core/src/main/kotlin/model/Documentable.kt | 393 ++++++++++++++++------------- 1 file changed, 213 insertions(+), 180 deletions(-) (limited to 'core/src/main/kotlin') diff --git a/core/src/main/kotlin/model/Documentable.kt b/core/src/main/kotlin/model/Documentable.kt index f0820256..8b059795 100644 --- a/core/src/main/kotlin/model/Documentable.kt +++ b/core/src/main/kotlin/model/Documentable.kt @@ -5,10 +5,97 @@ import org.jetbrains.dokka.model.doc.DocumentationNode import org.jetbrains.dokka.pages.PlatformData import org.jetbrains.kotlin.descriptors.Visibility -class Module(override val name: String, val packages: List) : Documentable() { +abstract class Documentable { + abstract val name: String? + abstract val dri: DRI + abstract val children: List + abstract val documentation: PlatformDependent + abstract val original: PlatformDependent + + override fun toString(): String = + "${javaClass.simpleName}($dri)" //+ briefDocTagString.takeIf { it.isNotBlank() }?.let { " [$it]" }.orEmpty() + + 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 + documentation.values + .firstOrNull() + ?.children + ?.firstOrNull() + ?.root + ?.docTagSummary() + ?.shorten(40) ?: "" + } +} + +data class PlatformDependent(val map: Map) : Map by map { + val prevalentValue: T? + get() = if (map.all { values.first() == it.value }) values.first() else null +} + +interface WithExpectActual { + val expect: Documentable? + val actual: PlatformDependent +} + +interface WithScope { + val functions: List + val properties: List + val classlikes: List +} + +interface WithPackages { + val packages: List +} + +interface WithVisibility { + val visibility: PlatformDependent // TODO custom visibility +} + +interface WithType { + val type: PlatformDependent +} + +interface WithAbstraction { + val modifier: PlatformDependent + enum class Modifier { + Abstract, Open, Final + } +} + +interface WithCompanion { + val companion: Object? +} + +interface WithConstructors { + val constructors: List +} + +interface WithGenerics { + val generics: PlatformDependent // TODO: fix the generics +} + +interface Callable : WithVisibility, WithType, WithAbstraction, WithExpectActual { + val receiver: PlatformDependent +} + +interface CanBeSupertype + +interface Classlike : WithScope, WithVisibility, WithExpectActual, CanBeSupertype { + val supertypes: PlatformDependent +} + +class Module( + override val name: String, + override val packages: List, + override val documentation: PlatformDependent, + override val original: PlatformDependent +) : Documentable(), WithPackages { override val dri: DRI = DRI.topLevel - override val children: List = packages - override val extra: MutableSet = mutableSetOf() + override val children: List + get() = packages } class Package( @@ -16,220 +103,170 @@ class Package( override val functions: List, override val properties: List, override val classlikes: List, - override val extra: MutableSet = mutableSetOf() -) : ScopeNode() { + override val packages: List, + override val documentation: PlatformDependent, + override val original: PlatformDependent +) : Documentable(), WithScope, WithPackages { override val name = dri.packageName.orEmpty() + override val children: List + get() = (functions + properties + classlikes + packages) as List } class Class( override val dri: DRI, override val name: String, - override val kind: ClassKind, - val constructors: List, + override val constructors: List, override val functions: List, override val properties: List, override val classlikes: List, - override val expected: ClassPlatformInfo?, - override val actual: List, - override val extra: MutableSet = mutableSetOf(), - override val visibility: Map -) : Classlike( - name = name, - dri = dri, - kind = kind, - functions = functions, - properties = properties, - classlikes = classlikes, - expected = expected, - actual = actual, - extra = extra -), WithVisibility + override val expect: Class?, + override val actual: PlatformDependent, + override val visibility: PlatformDependent, + override val companion: Object, + override val generics: PlatformDependent, + override val supertypes: PlatformDependent, + override val documentation: PlatformDependent, + override val original: PlatformDependent, + override val modifier: PlatformDependent +) : Documentable(), Classlike, WithAbstraction, WithCompanion, WithConstructors, WithGenerics { + override val children: List + get() = (functions + properties + classlikes + companion + constructors) as List +} class Enum( override val dri: DRI, override val name: String, val entries: List, - val constructors: List, - override val functions: List = emptyList(), - override val properties: List = emptyList(), - override val classlikes: List = emptyList(), - override val expected: ClassPlatformInfo? = null, - override val actual: List, - override val extra: MutableSet = mutableSetOf(), - override val visibility: Map -) : Classlike(dri = dri, name = name, kind = KotlinClassKindTypes.ENUM_CLASS, actual = actual), WithVisibility { - constructor(c: Classlike, entries: List, ctors: List) : this( - dri = c.dri, - name = c.name, - entries = entries, - constructors = ctors, - functions = c.functions, - properties = c.properties, - classlikes = c.classlikes, - expected = c.expected, - actual = c.actual, - extra = c.extra, - visibility = c.visibility - ) - + override val documentation: PlatformDependent, + override val expect: Enum?, + override val actual: PlatformDependent, + override val functions: List, + override val properties: List, + override val classlikes: List, + override val visibility: PlatformDependent, + override val companion: Object, + override val constructors: List, + override val supertypes: PlatformDependent, + override val original: PlatformDependent +) : Documentable(), Classlike, WithCompanion, WithConstructors { override val children: List - get() = entries + get() = (entries + functions + properties + classlikes + listOf(companion) + constructors) as List } class EnumEntry( + override val name: String?, override val dri: DRI, - override val name: String, - override val expected: ClassPlatformInfo? = null, - override val actual: List, - override val extra: MutableSet = mutableSetOf(), - override val visibility: Map -) : Classlike( - dri = dri, - name = name, - actual = actual, - expected = expected, - extra = extra, - kind = KotlinClassKindTypes.ENUM_ENTRY -) { - constructor(c: Classlike) : this( - dri = c.dri, - name = c.name, - actual = c.actual, - expected = c.expected, - extra = c.extra, - visibility = c.visibility - ) - - override val children: List - get() = emptyList() + override val documentation: PlatformDependent, + override val functions: List, + override val properties: List, + override val classlikes: List, + override val original: PlatformDependent +) : Documentable(), WithScope { + override val children: List + get() = (functions + properties + classlikes) as List } class Function( override val dri: DRI, override val name: String, - val returnType: TypeWrapper?, val isConstructor: Boolean, - override val receiver: Parameter?, + val returnType: TypeWrapper?, val parameters: List, - override val expected: PlatformInfo?, - override val actual: List, - override val extra: MutableSet = mutableSetOf(), - override val visibility: Map -) : CallableNode(), WithVisibility { - override val children: List - get() = listOfNotNull(receiver) + parameters + override val documentation: PlatformDependent, + override val expect: Function?, + override val actual: PlatformDependent, + override val visibility: PlatformDependent, + override val type: PlatformDependent, + override val generics: PlatformDependent, + override val receiver: PlatformDependent, + override val original: PlatformDependent, + override val modifier: PlatformDependent + ) : Documentable(), Callable, WithGenerics { + override val children: List + get() = parameters } -class Property( +class Interface( + override val name: String?, override val dri: DRI, - override val name: String, - override val receiver: Parameter?, - override val expected: PlatformInfo?, - override val actual: List, - override val extra: MutableSet = mutableSetOf(), - val accessors: List, - override val visibility: Map -) : CallableNode(), WithVisibility { - override val children: List - get() = listOfNotNull(receiver) + override val documentation: PlatformDependent, + override val original: PlatformDependent, + override val expect: Interface?, + override val actual: PlatformDependent, + override val functions: List, + override val properties: List, + override val classlikes: List, + override val visibility: PlatformDependent, + override val companion: Object, + override val generics: PlatformDependent, + override val supertypes: PlatformDependent +) : Documentable(), Classlike, WithCompanion, WithGenerics { + override val children: List + get() = (functions + properties + classlikes + companion) as List } -// TODO: treat named Parameters and receivers differently -class Parameter( - override val dri: DRI, +class Object( override val name: String?, - val type: TypeWrapper, - override val expected: PlatformInfo?, - override val actual: List, - override val extra: MutableSet = mutableSetOf() -) : Documentable() { + override val dri: DRI, + override val documentation: PlatformDependent, + override val original: PlatformDependent, + override val expect: Object?, + override val actual: PlatformDependent, + override val functions: List, + override val properties: List, + override val classlikes: List, + override val visibility: PlatformDependent, + override val supertypes: PlatformDependent +) : Documentable(), Classlike { override val children: List - get() = emptyList() + get() = (functions + properties + classlikes) as List } -interface PlatformInfo { - val documentationNode: DocumentationNode - val platformData: List -} - -class BasePlatformInfo( - override val documentationNode: DocumentationNode, - override val platformData: List -) : PlatformInfo { - - override fun equals(other: Any?): Boolean = - other is PlatformInfo && documentationNode == other.documentationNode - - override fun hashCode(): Int = - documentationNode.hashCode() -} - -class ClassPlatformInfo( - val info: PlatformInfo, - val inherited: List -) : PlatformInfo by info - -abstract class Documentable { - open val expected: PlatformInfo? = null - open val actual: List = emptyList() - open val name: String? = null - val platformInfo by lazy { listOfNotNull(expected) + actual } - val platformData by lazy { platformInfo.flatMap { it.platformData }.toSet() } - abstract val dri: DRI - - abstract val children: List - - override fun toString(): String { - return "${javaClass.simpleName}($dri)" + briefDocTagString.takeIf { it.isNotBlank() }?.let { " [$it]" }.orEmpty() - } - - override fun equals(other: Any?) = other is Documentable && this.dri == other.dri - - override fun hashCode() = dri.hashCode() - - val briefDocTagString: String by lazy { - platformInfo - .firstOrNull() - ?.documentationNode - ?.children - ?.firstOrNull() - ?.root - ?.docTagSummary() - ?.shorten(40) ?: "" - } - - open val extra: MutableSet = mutableSetOf() +class Annotation( + override val name: String?, + override val dri: DRI, + override val documentation: PlatformDependent, + override val original: PlatformDependent, + override val expect: Annotation?, + override val actual: PlatformDependent, + override val functions: List, + override val properties: List, + override val classlikes: List, + override val visibility: PlatformDependent, + override val companion: Object, + override val constructors: List +) : Documentable(), WithScope, WithVisibility, WithCompanion, WithConstructors, WithExpectActual, CanBeSupertype { + override val children: List + get() = (functions + properties + classlikes + constructors + companion) as List } -abstract class Classlike( +class Property( override val dri: DRI, override val name: String, - open val kind: ClassKind, - override val functions: List = emptyList(), - override val properties: List = emptyList(), - override val classlikes: List = emptyList(), - override val expected: ClassPlatformInfo? = null, - override val actual: List, - override val extra: MutableSet = mutableSetOf() -) : ScopeNode(), WithVisibility { - val inherited by lazy { platformInfo.mapNotNull { (it as? ClassPlatformInfo)?.inherited }.flatten() } -} - -abstract class ScopeNode : Documentable() { - abstract val functions: List - abstract val properties: List - abstract val classlikes: List - - override val children: List // It is written so awkwardly because of type inference being lost here - get() = mutableListOf().apply { - addAll(functions) - addAll(properties) - addAll(classlikes) - } + override val documentation: PlatformDependent, + override val expect: Property?, + override val actual: PlatformDependent, + override val visibility: PlatformDependent, + override val type: PlatformDependent, + override val receiver: PlatformDependent, + val accessors: PlatformDependent, // TODO > extra + override val original: PlatformDependent, + override val modifier: PlatformDependent +) : Documentable(), Callable { + override val children: List + get() = emptyList() } -abstract class CallableNode : Documentable() { - abstract val receiver: Parameter? +// TODO: treat named Parameters and receivers differently +class Parameter( + override val dri: DRI, + override val name: String?, + override val documentation: PlatformDependent, + val type: TypeWrapper, + override val original: PlatformDependent +) : Documentable() { + override val children: List + get() = emptyList() } private fun String.shorten(maxLength: Int) = lineSequence().first().let { @@ -245,7 +282,3 @@ fun Documentable.dfs(predicate: (Documentable) -> Boolean): Documentable? = interface Extra object STATIC : Extra - -interface WithVisibility { - val visibility: Map -} \ No newline at end of file -- cgit