package org.jetbrains.dokka.Model import org.jetbrains.dokka.links.DRI import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.kdoc.psi.impl.KDocTag class Module(val packages: List) : DocumentationNode() { override val docTag: KDocTag? = null override val dri: DRI = DRI.topLevel override val children: List = packages } class Package( override val dri: DRI, override val functions: List, override val properties: List, override val classes: List ) : ScopeNode() { val name = dri.packageName.orEmpty() override val docTag: KDocTag? = null } class Class( override val dri: DRI, val name: String, val constructors: List, override val functions: List, override val properties: List, override val classes: List, override val docTag: KDocTag?, override val descriptor: ClassDescriptor ) : ScopeNode() class Function( override val dri: DRI, val name: String, override val receiver: Parameter?, val parameters: List, override val docTag: KDocTag?, override val descriptor: FunctionDescriptor ) : CallableNode() { override val children: List get() = listOfNotNull(receiver) + parameters } class Property( override val dri: DRI, val name: String, override val receiver: Parameter?, override val docTag: KDocTag?, override val descriptor: PropertyDescriptor ) : CallableNode() { override val children: List get() = listOfNotNull(receiver) } // TODO: treat named Parameters and receivers differently class Parameter( override val dri: DRI, val name: String?, override val docTag: KDocTag?, override val descriptor: ParameterDescriptor ) : DocumentationNode() { override val children: List> get() = emptyList() } abstract class DocumentationNode { open val descriptor: T? = null abstract val docTag: KDocTag? // TODO: replace in the future with more robust doc-comment model abstract val dri: DRI abstract val children: List> override fun toString(): String { return "${javaClass.simpleName}($dri)" + briefDocstring.takeIf { it.isNotBlank() }?.let { " [$it]"}.orEmpty() } override fun equals(other: Any?) = other is DocumentationNode<*> && this.dri == other.dri override fun hashCode() = dri.hashCode() val rawDocstring: String get() = docTag?.getContent().orEmpty() val briefDocstring: String get() = rawDocstring.shorten(40) } abstract class ScopeNode : DocumentationNode() { abstract val functions: List abstract val properties: List abstract val classes: List override val children: List> get() = functions + properties + classes } abstract class CallableNode : DocumentationNode() { 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 }