package org.jetbrains.dokka.model import org.jetbrains.dokka.model.doc.* import org.jetbrains.dokka.transformers.descriptors.KotlinTypeWrapper import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.pages.PlatformData class Module(override val name: String, val packages: List) : Documentable() { override val dri: DRI = DRI.topLevel override val children: List = packages override val extra: MutableSet = mutableSetOf() } class Package( override val dri: DRI, override val functions: List, override val properties: List, override val classes: List, override val extra: MutableSet = mutableSetOf() ) : ScopeNode() { override val name = dri.packageName.orEmpty() } class Class( override val dri: DRI, override val name: String, val kind: ClassKind, val constructors: List, override val functions: List, override val properties: List, override val classes: List, override val expected: ClassPlatformInfo?, override val actual: List, override val extra: MutableSet = mutableSetOf() ) : ScopeNode() { val inherited by lazy { platformInfo.mapNotNull { (it as? ClassPlatformInfo)?.inherited }.flatten() } } class Function( override val dri: DRI, override val name: String, val returnType: TypeWrapper?, val isConstructor: Boolean, override val receiver: Parameter?, val parameters: List, override val expected: PlatformInfo?, override val actual: List, override val extra: MutableSet = mutableSetOf() ) : CallableNode() { override val children: List get() = listOfNotNull(receiver) + parameters } class Property( 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() ) : CallableNode() { override val children: List get() = listOfNotNull(receiver) } // TODO: treat named Parameters and receivers differently class Parameter( override val dri: DRI, override val name: String?, val type: TypeWrapper, override val actual: List, override val extra: MutableSet = mutableSetOf() ) : Documentable() { override val children: List get() = emptyList() } 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 commentsData: List get() = platformInfo.map { it.documentationNode } val briefDocTagString: String get() = platformInfo .firstOrNull() ?.documentationNode ?.children ?.firstOrNull() ?.root ?.docTagSummary() ?.shorten(40) ?: "" open val extra: MutableSet = mutableSetOf() } abstract class ScopeNode : Documentable() { abstract val functions: List abstract val properties: List abstract val classes: List override val children: List get() = functions + properties + classes } abstract class CallableNode : Documentable() { abstract val receiver: Parameter? } private fun String.shorten(maxLength: Int) = lineSequence().first().let { if (it.length != length || it.length > maxLength) it.take(maxLength - 3) + "..." else it } interface TypeWrapper { val constructorFqName: String? val constructorNamePathSegments: List val arguments: List val dri: DRI? } interface ClassKind fun Documentable.dfs(predicate: (Documentable) -> Boolean): Documentable? = if (predicate(this)) { this } else { this.children.asSequence().mapNotNull { it.dfs(predicate) }.firstOrNull() } interface Extra