diff options
Diffstat (limited to 'core/src/main/kotlin/Model')
-rw-r--r-- | core/src/main/kotlin/Model/DocumentationNode.kt | 101 | ||||
-rw-r--r-- | core/src/main/kotlin/Model/transformers/DocumentationNodesMerger.kt | 38 |
2 files changed, 86 insertions, 53 deletions
diff --git a/core/src/main/kotlin/Model/DocumentationNode.kt b/core/src/main/kotlin/Model/DocumentationNode.kt index 55ae5902..623f2ea3 100644 --- a/core/src/main/kotlin/Model/DocumentationNode.kt +++ b/core/src/main/kotlin/Model/DocumentationNode.kt @@ -1,11 +1,11 @@ package org.jetbrains.dokka.Model +import org.jetbrains.dokka.KotlinTypeWrapper import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.pages.PlatformData -import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.kdoc.psi.impl.KDocTag -class Module(val packages: List<Package>) : DocumentationNode<Nothing>() { +class Module(val packages: List<Package>) : DocumentationNode() { override val dri: DRI = DRI.topLevel override val children: List<Package> = packages override val extra: MutableSet<Extra> = mutableSetOf() @@ -17,31 +17,36 @@ class Package( override val properties: List<Property>, override val classes: List<Class>, override val extra: MutableSet<Extra> = mutableSetOf() -) : ScopeNode<Nothing>() { +) : ScopeNode() { val name = dri.packageName.orEmpty() } class Class( override val dri: DRI, val name: String, + val kind: ClassKind, val constructors: List<Function>, override val functions: List<Function>, override val properties: List<Property>, override val classes: List<Class>, - override val expectDescriptor: Descriptor<ClassDescriptor>?, - override val actualDescriptors: List<Descriptor<ClassDescriptor>>, + override val expected: ClassPlatformInfo?, + override val actual: List<ClassPlatformInfo>, override val extra: MutableSet<Extra> = mutableSetOf() -) : ScopeNode<ClassDescriptor>() +) : ScopeNode() { + val inherited by lazy { platformInfo.mapNotNull { (it as? ClassPlatformInfo)?.inherited }.flatten() } +} class Function( override val dri: DRI, val name: String, + val returnType: TypeWrapper?, + val isConstructor: Boolean, override val receiver: Parameter?, val parameters: List<Parameter>, - override val expectDescriptor: Descriptor<FunctionDescriptor>?, - override val actualDescriptors: List<Descriptor<FunctionDescriptor>>, + override val expected: PlatformInfo?, + override val actual: List<PlatformInfo>, override val extra: MutableSet<Extra> = mutableSetOf() -) : CallableNode<FunctionDescriptor>() { +) : CallableNode() { override val children: List<Parameter> get() = listOfNotNull(receiver) + parameters } @@ -50,10 +55,10 @@ class Property( override val dri: DRI, val name: String, override val receiver: Parameter?, - override val expectDescriptor: Descriptor<PropertyDescriptor>?, - override val actualDescriptors: List<Descriptor<PropertyDescriptor>>, + override val expected: PlatformInfo?, + override val actual: List<PlatformInfo>, override val extra: MutableSet<Extra> = mutableSetOf() -) : CallableNode<PropertyDescriptor>() { +) : CallableNode() { override val children: List<Parameter> get() = listOfNotNull(receiver) } @@ -62,67 +67,75 @@ class Property( class Parameter( override val dri: DRI, val name: String?, - override val actualDescriptors: List<Descriptor<ParameterDescriptor>>, + val type: TypeWrapper, + override val actual: List<PlatformInfo>, override val extra: MutableSet<Extra> = mutableSetOf() -) : DocumentationNode<ParameterDescriptor>() { - override val children: List<DocumentationNode<*>> +) : DocumentationNode() { + override val children: List<DocumentationNode> get() = emptyList() } -class Descriptor<out T : DeclarationDescriptor>( - val descriptor: T, - val docTag: KDocTag?, - val links: Map<String, DRI>, +interface PlatformInfo { + val docTag: KDocTag? + val links: Map<String, DRI> val platformData: List<PlatformData> -) : DeclarationDescriptor by descriptor { +} + +class BasePlatformInfo( + override val docTag: KDocTag?, + override val links: Map<String, DRI>, + override val platformData: List<PlatformData>) : PlatformInfo { override fun equals(other: Any?): Boolean = - other is Descriptor<*> && ( - descriptor.toString() == other.descriptor.toString() && - docTag?.text == other.docTag?.text && + other is PlatformInfo && ( + docTag?.text == other.docTag?.text && links == other.links) override fun hashCode(): Int = - listOf(descriptor.toString(), docTag?.text, links).hashCode() + listOf(docTag?.text, links).hashCode() } -abstract class DocumentationNode<out T : DeclarationDescriptor> { - open val expectDescriptor: Descriptor<T>? = null - open val actualDescriptors: List<Descriptor<T>> = emptyList() - val descriptors by lazy { listOfNotNull(expectDescriptor) + actualDescriptors } - val platformData by lazy { descriptors.flatMap { it.platformData }.toSet() } +class ClassPlatformInfo( + val info: PlatformInfo, + val inherited: List<DRI>) : PlatformInfo by info + +abstract class DocumentationNode { + open val expected: PlatformInfo? = null + open val actual: List<PlatformInfo> = emptyList() + val platformInfo by lazy { listOfNotNull(expected) + actual } + val platformData by lazy { platformInfo.flatMap { it.platformData }.toSet() } abstract val dri: DRI - abstract val children: List<DocumentationNode<*>> + abstract val children: List<DocumentationNode> 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 equals(other: Any?) = other is DocumentationNode && this.dri == other.dri override fun hashCode() = dri.hashCode() val commentsData: List<Pair<String, Map<String, DRI>>> - get() = descriptors.mapNotNull { it.docTag?.let { tag -> Pair(tag.getContent(), it.links) } } + get() = platformInfo.mapNotNull { it.docTag?.let { tag -> Pair(tag.getContent(), it.links) } } val briefDocstring: String - get() = descriptors.firstOrNull()?.docTag?.getContent().orEmpty().shorten(40) + get() = platformInfo.firstOrNull()?.docTag?.getContent().orEmpty().shorten(40) open val extra: MutableSet<Extra> = mutableSetOf() } -abstract class ScopeNode<out T : ClassOrPackageFragmentDescriptor> : DocumentationNode<T>() { +abstract class ScopeNode : DocumentationNode() { abstract val functions: List<Function> abstract val properties: List<Property> abstract val classes: List<Class> - override val children: List<DocumentationNode<MemberDescriptor>> + override val children: List<DocumentationNode> get() = functions + properties + classes } -abstract class CallableNode<out T : CallableDescriptor> : DocumentationNode<T>() { +abstract class CallableNode : DocumentationNode() { abstract val receiver: Parameter? } @@ -130,20 +143,28 @@ private fun String.shorten(maxLength: Int) = lineSequence().first().let { if (it.length != length || it.length > maxLength) it.take(maxLength - 3) + "..." else it } -fun DocumentationNode<*>.walk(process: DocumentationNode<*>.() -> Unit) { +interface TypeWrapper { + val constructorFqName: String? + val constructorNamePathSegments: List<String> + val arguments: List<KotlinTypeWrapper> + val dri: DRI? +} +interface ClassKind + +fun DocumentationNode.walk(process: DocumentationNode.() -> Unit) { this.process() this.children.forEach { it.process() } } -fun DocumentationNode<*>.dfs(predicate: (DocumentationNode<*>) -> Boolean): DocumentationNode<*>? = +fun DocumentationNode.dfs(predicate: (DocumentationNode) -> Boolean): DocumentationNode? = if (predicate(this)) { this } else { this.children.asSequence().mapNotNull { it.dfs(predicate) }.firstOrNull() } -fun DocumentationNode<*>.findAll(predicate: (DocumentationNode<*>) -> Boolean): Set<DocumentationNode<*>> { - val found = mutableSetOf<DocumentationNode<*>>() +fun DocumentationNode.findAll(predicate: (DocumentationNode) -> Boolean): Set<DocumentationNode> { + val found = mutableSetOf<DocumentationNode>() if (predicate(this)) { found.add(this) } else { diff --git a/core/src/main/kotlin/Model/transformers/DocumentationNodesMerger.kt b/core/src/main/kotlin/Model/transformers/DocumentationNodesMerger.kt index 2fb2f7c0..ae4f8d99 100644 --- a/core/src/main/kotlin/Model/transformers/DocumentationNodesMerger.kt +++ b/core/src/main/kotlin/Model/transformers/DocumentationNodesMerger.kt @@ -2,7 +2,6 @@ package org.jetbrains.dokka.Model.transformers import org.jetbrains.dokka.Model.* import org.jetbrains.dokka.Model.Function -import org.jetbrains.kotlin.descriptors.DeclarationDescriptor internal object DocumentationNodesMerger : DocumentationNodeTransformer { override fun invoke(original: Module) = Module( @@ -19,55 +18,68 @@ private fun mergePackageContent(original: Package) = Package( merge(original.classes, Class::mergeWith) ) -private fun <T: DocumentationNode<*>> merge(elements: List<T>, reducer: (T, T) -> T): List<T> = +private fun <T: DocumentationNode> merge(elements: List<T>, reducer: (T, T) -> T): List<T> = elements.groupingBy { it.dri } .reduce { _, left, right -> reducer(left, right)} .values.toList() -fun <T:DeclarationDescriptor> Descriptor<T>.mergeWith(other: Descriptor<T>?) = Descriptor( - descriptor, +fun PlatformInfo.mergeWith(other: PlatformInfo?) = BasePlatformInfo( docTag, links, (platformData + (other?.platformData ?: emptyList())).distinct() ) -fun <T:DeclarationDescriptor> List<Descriptor<T>>.merge() : List<Descriptor<T>> = - groupingBy { it.descriptor }.reduce { +fun ClassPlatformInfo.mergeWith(other: ClassPlatformInfo?) = ClassPlatformInfo( + info.mergeWith(other?.info), + (inherited + (other?.inherited ?: emptyList())).distinct() +) + +fun List<ClassPlatformInfo>.mergeClassPlatformInfo() : List<ClassPlatformInfo> = + groupingBy { it.docTag.toString() + it.links + it.inherited}.reduce { + _, left, right -> left.mergeWith(right) + }.values.toList() + +fun List<PlatformInfo>.merge() : List<PlatformInfo> = + groupingBy { it.docTag.toString() + it.links }.reduce { _, left, right -> left.mergeWith(right) }.values.toList() fun Function.mergeWith(other: Function) = Function( dri, name, + returnType, + isConstructor, if (receiver != null && other.receiver != null) receiver.mergeWith(other.receiver) else null, merge(parameters + other.parameters, Parameter::mergeWith), - expectDescriptor?.mergeWith(other.expectDescriptor), - (actualDescriptors + other.actualDescriptors).merge() + expected?.mergeWith(other.expected), + (actual + other.actual).merge() ) fun Property.mergeWith(other: Property) = Property( dri, name, if (receiver != null && other.receiver != null) receiver.mergeWith(other.receiver) else null, - expectDescriptor?.mergeWith(other.expectDescriptor), - (actualDescriptors + other.actualDescriptors).merge() + expected?.mergeWith(other.expected), + (actual + other.actual).merge() ) fun Class.mergeWith(other: Class) = Class( dri, name, + kind, merge(constructors + other.constructors, Function::mergeWith), merge(functions + other.functions, Function::mergeWith), merge(properties + other.properties, Property::mergeWith), merge(classes + other.classes, Class::mergeWith), - expectDescriptor?.mergeWith(other.expectDescriptor), - (actualDescriptors + other.actualDescriptors).merge() + expected?.mergeWith(other.expected), + (actual + other.actual).mergeClassPlatformInfo() ) fun Parameter.mergeWith(other: Parameter) = Parameter( dri, name, - (actualDescriptors + other.actualDescriptors).merge() + type, + (actual + other.actual).merge() ) fun Package.mergeWith(other: Package) = Package( |