diff options
-rw-r--r-- | core/src/main/kotlin/model/Documentable.kt | 393 |
1 files changed, 213 insertions, 180 deletions
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<Package>) : Documentable() { +abstract class Documentable { + abstract val name: String? + abstract val dri: DRI + abstract val children: List<Documentable> + abstract val documentation: PlatformDependent<DocumentationNode> + abstract val original: PlatformDependent<Documentable> + + 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<out T>(val map: Map<PlatformData, T>) : Map<PlatformData, T> 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<Documentable> +} + +interface WithScope { + val functions: List<Function> + val properties: List<Property> + val classlikes: List<Classlike> +} + +interface WithPackages { + val packages: List<Package> +} + +interface WithVisibility { + val visibility: PlatformDependent<Visibility> // TODO custom visibility +} + +interface WithType { + val type: PlatformDependent<TypeWrapper> +} + +interface WithAbstraction { + val modifier: PlatformDependent<Modifier> + enum class Modifier { + Abstract, Open, Final + } +} + +interface WithCompanion { + val companion: Object? +} + +interface WithConstructors { + val constructors: List<Function> +} + +interface WithGenerics { + val generics: PlatformDependent<TypeWrapper> // TODO: fix the generics +} + +interface Callable : WithVisibility, WithType, WithAbstraction, WithExpectActual { + val receiver: PlatformDependent<Parameter> +} + +interface CanBeSupertype + +interface Classlike : WithScope, WithVisibility, WithExpectActual, CanBeSupertype { + val supertypes: PlatformDependent<CanBeSupertype> +} + +class Module( + override val name: String, + override val packages: List<Package>, + override val documentation: PlatformDependent<DocumentationNode>, + override val original: PlatformDependent<Module> +) : Documentable(), WithPackages { override val dri: DRI = DRI.topLevel - override val children: List<Package> = packages - override val extra: MutableSet<Extra> = mutableSetOf() + override val children: List<Documentable> + get() = packages } class Package( @@ -16,220 +103,170 @@ class Package( override val functions: List<Function>, override val properties: List<Property>, override val classlikes: List<Classlike>, - override val extra: MutableSet<Extra> = mutableSetOf() -) : ScopeNode() { + override val packages: List<Package>, + override val documentation: PlatformDependent<DocumentationNode>, + override val original: PlatformDependent<Package> +) : Documentable(), WithScope, WithPackages { override val name = dri.packageName.orEmpty() + override val children: List<Documentable> + get() = (functions + properties + classlikes + packages) as List<Documentable> } class Class( override val dri: DRI, override val name: String, - override val kind: ClassKind, - val constructors: List<Function>, + override val constructors: List<Function>, override val functions: List<Function>, override val properties: List<Property>, override val classlikes: List<Classlike>, - override val expected: ClassPlatformInfo?, - override val actual: List<ClassPlatformInfo>, - override val extra: MutableSet<Extra> = mutableSetOf(), - override val visibility: Map<PlatformData, Visibility> -) : 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<Class>, + override val visibility: PlatformDependent<Visibility>, + override val companion: Object, + override val generics: PlatformDependent<TypeWrapper>, + override val supertypes: PlatformDependent<Classlike>, + override val documentation: PlatformDependent<DocumentationNode>, + override val original: PlatformDependent<Class>, + override val modifier: PlatformDependent<WithAbstraction.Modifier> +) : Documentable(), Classlike, WithAbstraction, WithCompanion, WithConstructors, WithGenerics { + override val children: List<Documentable> + get() = (functions + properties + classlikes + companion + constructors) as List<Documentable> +} class Enum( override val dri: DRI, override val name: String, val entries: List<EnumEntry>, - val constructors: List<Function>, - override val functions: List<Function> = emptyList(), - override val properties: List<Property> = emptyList(), - override val classlikes: List<Classlike> = emptyList(), - override val expected: ClassPlatformInfo? = null, - override val actual: List<ClassPlatformInfo>, - override val extra: MutableSet<Extra> = mutableSetOf(), - override val visibility: Map<PlatformData, Visibility> -) : Classlike(dri = dri, name = name, kind = KotlinClassKindTypes.ENUM_CLASS, actual = actual), WithVisibility { - constructor(c: Classlike, entries: List<EnumEntry>, ctors: List<Function>) : 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<DocumentationNode>, + override val expect: Enum?, + override val actual: PlatformDependent<Enum>, + override val functions: List<Function>, + override val properties: List<Property>, + override val classlikes: List<Classlike>, + override val visibility: PlatformDependent<Visibility>, + override val companion: Object, + override val constructors: List<Function>, + override val supertypes: PlatformDependent<Classlike>, + override val original: PlatformDependent<Enum> +) : Documentable(), Classlike, WithCompanion, WithConstructors { override val children: List<Documentable> - get() = entries + get() = (entries + functions + properties + classlikes + listOf(companion) + constructors) as List<Documentable> } class EnumEntry( + override val name: String?, override val dri: DRI, - override val name: String, - override val expected: ClassPlatformInfo? = null, - override val actual: List<ClassPlatformInfo>, - override val extra: MutableSet<Extra> = mutableSetOf(), - override val visibility: Map<PlatformData, Visibility> -) : 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<Parameter> - get() = emptyList() + override val documentation: PlatformDependent<DocumentationNode>, + override val functions: List<Function>, + override val properties: List<Property>, + override val classlikes: List<Classlike>, + override val original: PlatformDependent<EnumEntry> +) : Documentable(), WithScope { + override val children: List<Documentable> + get() = (functions + properties + classlikes) as List<Documentable> } 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<Parameter>, - override val expected: PlatformInfo?, - override val actual: List<PlatformInfo>, - override val extra: MutableSet<Extra> = mutableSetOf(), - override val visibility: Map<PlatformData, Visibility> -) : CallableNode(), WithVisibility { - override val children: List<Parameter> - get() = listOfNotNull(receiver) + parameters + override val documentation: PlatformDependent<DocumentationNode>, + override val expect: Function?, + override val actual: PlatformDependent<Function>, + override val visibility: PlatformDependent<Visibility>, + override val type: PlatformDependent<TypeWrapper>, + override val generics: PlatformDependent<TypeWrapper>, + override val receiver: PlatformDependent<Parameter>, + override val original: PlatformDependent<Function>, + override val modifier: PlatformDependent<WithAbstraction.Modifier> + ) : Documentable(), Callable, WithGenerics { + override val children: List<Documentable> + 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<PlatformInfo>, - override val extra: MutableSet<Extra> = mutableSetOf(), - val accessors: List<Function>, - override val visibility: Map<PlatformData, Visibility> -) : CallableNode(), WithVisibility { - override val children: List<Parameter> - get() = listOfNotNull(receiver) + override val documentation: PlatformDependent<DocumentationNode>, + override val original: PlatformDependent<Interface>, + override val expect: Interface?, + override val actual: PlatformDependent<Interface>, + override val functions: List<Function>, + override val properties: List<Property>, + override val classlikes: List<Classlike>, + override val visibility: PlatformDependent<Visibility>, + override val companion: Object, + override val generics: PlatformDependent<TypeWrapper>, + override val supertypes: PlatformDependent<Classlike> +) : Documentable(), Classlike, WithCompanion, WithGenerics { + override val children: List<Documentable> + get() = (functions + properties + classlikes + companion) as List<Documentable> } -// 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<PlatformInfo>, - override val extra: MutableSet<Extra> = mutableSetOf() -) : Documentable() { + override val dri: DRI, + override val documentation: PlatformDependent<DocumentationNode>, + override val original: PlatformDependent<Object>, + override val expect: Object?, + override val actual: PlatformDependent<Object>, + override val functions: List<Function>, + override val properties: List<Property>, + override val classlikes: List<Classlike>, + override val visibility: PlatformDependent<Visibility>, + override val supertypes: PlatformDependent<Classlike> +) : Documentable(), Classlike { override val children: List<Documentable> - get() = emptyList() + get() = (functions + properties + classlikes) as List<Documentable> } -interface PlatformInfo { - val documentationNode: DocumentationNode - val platformData: List<PlatformData> -} - -class BasePlatformInfo( - override val documentationNode: DocumentationNode, - override val platformData: List<PlatformData> -) : 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<DRI> -) : PlatformInfo by info - -abstract class Documentable { - open val expected: PlatformInfo? = null - open val actual: List<PlatformInfo> = 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<Documentable> - - 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<Extra> = mutableSetOf() +class Annotation( + override val name: String?, + override val dri: DRI, + override val documentation: PlatformDependent<DocumentationNode>, + override val original: PlatformDependent<Annotation>, + override val expect: Annotation?, + override val actual: PlatformDependent<Annotation>, + override val functions: List<Function>, + override val properties: List<Property>, + override val classlikes: List<Classlike>, + override val visibility: PlatformDependent<Visibility>, + override val companion: Object, + override val constructors: List<Function> +) : Documentable(), WithScope, WithVisibility, WithCompanion, WithConstructors, WithExpectActual, CanBeSupertype { + override val children: List<Documentable> + get() = (functions + properties + classlikes + constructors + companion) as List<Documentable> } -abstract class Classlike( +class Property( override val dri: DRI, override val name: String, - open val kind: ClassKind, - override val functions: List<Function> = emptyList(), - override val properties: List<Property> = emptyList(), - override val classlikes: List<Classlike> = emptyList(), - override val expected: ClassPlatformInfo? = null, - override val actual: List<ClassPlatformInfo>, - override val extra: MutableSet<Extra> = mutableSetOf() -) : ScopeNode(), WithVisibility { - val inherited by lazy { platformInfo.mapNotNull { (it as? ClassPlatformInfo)?.inherited }.flatten() } -} - -abstract class ScopeNode : Documentable() { - abstract val functions: List<Function> - abstract val properties: List<Property> - abstract val classlikes: List<Classlike> - - override val children: List<Documentable> // It is written so awkwardly because of type inference being lost here - get() = mutableListOf<Documentable>().apply { - addAll(functions) - addAll(properties) - addAll(classlikes) - } + override val documentation: PlatformDependent<DocumentationNode>, + override val expect: Property?, + override val actual: PlatformDependent<Property>, + override val visibility: PlatformDependent<Visibility>, + override val type: PlatformDependent<TypeWrapper>, + override val receiver: PlatformDependent<Parameter>, + val accessors: PlatformDependent<Function>, // TODO > extra + override val original: PlatformDependent<Property>, + override val modifier: PlatformDependent<WithAbstraction.Modifier> +) : Documentable(), Callable { + override val children: List<Documentable> + 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<DocumentationNode>, + val type: TypeWrapper, + override val original: PlatformDependent<Parameter> +) : Documentable() { + override val children: List<Documentable> + 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<PlatformData, Visibility> -}
\ No newline at end of file |