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 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() } 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 expectDescriptor: Descriptor?, override val actualDescriptors: List> ) : ScopeNode() class Function( override val dri: DRI, val name: String, override val receiver: Parameter?, val parameters: List, override val expectDescriptor: Descriptor?, override val actualDescriptors: List> ) : CallableNode() { override val children: List get() = listOfNotNull(receiver) + parameters } class Property( override val dri: DRI, val name: String, override val receiver: Parameter?, override val expectDescriptor: Descriptor?, override val actualDescriptors: List> ) : 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 actualDescriptors: List> ) : DocumentationNode() { override val children: List> get() = emptyList() } class Descriptor( val descriptor: T, val docTag: KDocTag?, val links: Map, val passes: List ) : DeclarationDescriptor by descriptor { override fun equals(other: Any?): Boolean = other is Descriptor<*> && ( descriptor.toString() == other.descriptor.toString() && docTag?.text == other.docTag?.text && links == other.links) override fun hashCode(): Int = listOf(descriptor.toString(), docTag?.text, links).hashCode() } abstract class DocumentationNode { open val expectDescriptor: Descriptor? = null open val actualDescriptors: List> = emptyList() val descriptors by lazy { listOfNotNull(expectDescriptor) + actualDescriptors } 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 commentsData: List>> get() = descriptors.mapNotNull { it.docTag?.let { tag -> Pair(tag.getContent(), it.links) } } val briefDocstring: String get() = descriptors.firstOrNull()?.docTag?.getContent().orEmpty().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 }