From 972ec7adb70a62f825bb67dd05d37ebbcbdfd352 Mon Sep 17 00:00:00 2001 From: Paweł Marks Date: Thu, 31 Oct 2019 19:10:02 +0100 Subject: Model updated --- core/src/main/kotlin/Model/DocumentationNode.kt | 133 ++++++++++++--------- core/src/main/kotlin/links/DRI.kt | 69 ++++++++--- .../DefaultDocumentationToPageTransformer.kt | 1 - 3 files changed, 130 insertions(+), 73 deletions(-) (limited to 'core/src') diff --git a/core/src/main/kotlin/Model/DocumentationNode.kt b/core/src/main/kotlin/Model/DocumentationNode.kt index 19ba22a6..5312915b 100644 --- a/core/src/main/kotlin/Model/DocumentationNode.kt +++ b/core/src/main/kotlin/Model/DocumentationNode.kt @@ -1,75 +1,94 @@ package org.jetbrains.dokka -import org.jetbrains.dokka.links.ClassReference +import org.jetbrains.dokka.links.Callable +import org.jetbrains.dokka.links.DRI +import org.jetbrains.dokka.links.withClass import org.jetbrains.kotlin.descriptors.* -class DocumentationNodes { - - class Module(name: String, parent: DocumentationNode<*>? = null): - DocumentationNode(name, parent = parent) +class Module(val packages: List) : DocumentationNode(DRI.topLevel, DRI.topLevel) { + override val children: List + get() = packages +} - class Package(name: String, parent: DocumentationNode<*>): - DocumentationNode(name, parent = parent) +class Package( + val name: String, + override val functions: List, + override val properties: List, + override val classes: List +) : ScopeNode(DRI(packageName = name), DRI.topLevel) - class Class(name: String, descriptor: ClassDescriptor, parent: DocumentationNode<*>? = null) : - DocumentationNode(name, descriptor, parent) { - override val classLike: Boolean = true - val supertypes = mutableListOf() - val isInterface: Boolean - get() = descriptor?.kind == ClassKind.CLASS - val isEnum: Boolean - get() = descriptor?.kind == ClassKind.ENUM_CLASS - val isEnumEntry: Boolean - get() = descriptor?.kind == ClassKind.ENUM_ENTRY - val isAnnotationClass: Boolean - get() = descriptor?.kind == ClassKind.ANNOTATION_CLASS - val isObject: Boolean - get() = descriptor?.kind == ClassKind.OBJECT - } - - class Constructor(name: String, descriptor: ConstructorDescriptor) : - DocumentationNode(name, descriptor) { - override val memberLike = true - } +class Class( + val name: String, + override val functions: List, + override val properties: List, + override val classes: List, + override val descriptor: ClassDescriptor, + parent: DRI +) : ScopeNode(parent.withClass(name), parent) - class Function(name: String, descriptor: FunctionDescriptor) : - DocumentationNode(name, descriptor) { - override val memberLike = true - } +class Function( + val name: String, + override val receiver: Parameter?, + val parameters: List, + override val descriptor: FunctionDescriptor, + parent: DRI +) : CallableNode(parent, descriptor) { + override val children: List + get() = listOfNotNull(receiver) + parameters +} - class Property(name: String, descriptor: PropertyDescriptor) : - DocumentationNode(name, descriptor) { - override val memberLike = true - } -/* - class Field(name: String, descriptor: FieldDescriptor) : - DocumentationNode(name, descriptor) { - override val memberLike = true - }*/ +class Property( + val name: String, + override val receiver: Parameter?, + override val descriptor: PropertyDescriptor, + parent: DRI +) : CallableNode(parent, descriptor) { + override val children: List + get() = listOfNotNull(receiver) +} - class Parameter(name: String, descriptor: ParameterDescriptor?) : - DocumentationNode(name, descriptor) -/* - class Annotation(name: String, descriptor: AnnotationDescriptor?) : - DocumentationNode(name, descriptor)*/ +class Parameter( + val name: String, + override val descriptor: ParameterDescriptor, + parent: DRI, + index: Int +) : DocumentationNode(parent, parent.copy(target = index)) { + override val children: List> + get() = emptyList() } -abstract class DocumentationNode( - var name: String, - val descriptor: T? = null, - val parent: DocumentationNode<*>? = null +abstract class DocumentationNode( + val dri: DRI, + val parent: DRI ) { + open val descriptor: T? = null - private val children = mutableListOf>() + abstract val children: List> - open val classLike = false + override fun toString(): String { + return "${javaClass.name}($dri)" + } - open val memberLike = false + override fun equals(other: Any?) = other is DocumentationNode<*> && this.dri == other.dri - fun addChild(child: DocumentationNode<*>) = - children.add(child) + override fun hashCode() = dri.hashCode() +} - override fun toString(): String { - return "${javaClass.name}:$name" - } +abstract class ScopeNode( + dri: DRI, + parent: DRI +) : DocumentationNode(dri, parent) { + abstract val functions: List + abstract val properties: List + abstract val classes: List + + override val children: List> + get() = functions + properties + classes +} + +abstract class CallableNode( + parent: DRI, + descriptor: CallableDescriptor +) : DocumentationNode(parent.copy(callable = Callable.from(descriptor)), parent) { + abstract val receiver: Parameter? } \ No newline at end of file diff --git a/core/src/main/kotlin/links/DRI.kt b/core/src/main/kotlin/links/DRI.kt index 35eb6497..7ab25444 100644 --- a/core/src/main/kotlin/links/DRI.kt +++ b/core/src/main/kotlin/links/DRI.kt @@ -1,22 +1,28 @@ package org.jetbrains.dokka.links +import org.jetbrains.kotlin.descriptors.CallableDescriptor +import org.jetbrains.kotlin.types.KotlinType import java.text.ParseException /** * [DRI] stands for DokkaResourceIdentifier */ -data class DRI(val packageName: String? = null, - val classNames: String? = null, - val callable: Callable? = null, - val target: Int? = null, - val extra: String? = null) { - - constructor(packageName: String? = null, - classNames: String? = null, - callableName: String? = null, - signature: String? = null, - target: Int? = null, - extra: String? = null) : this(packageName, classNames, Callable.from(callableName, signature), target, extra) +data class DRI( + val packageName: String? = null, + val classNames: String? = null, + val callable: Callable? = null, + val target: Int? = null, + val extra: String? = null +) { + + constructor( + packageName: String? = null, + classNames: String? = null, + callableName: String? = null, + signature: String? = null, + target: Int? = null, + extra: String? = null + ) : this(packageName, classNames, Callable.from(callableName, signature), target, extra) override fun toString(): String = "${packageName.orEmpty()}/${classNames.orEmpty()}/${callable?.name.orEmpty()}/${callable?.signature().orEmpty()}/${target?.toString().orEmpty()}/${extra.orEmpty()}" @@ -29,7 +35,11 @@ data class DRI(val packageName: String? = null, DRI( packageName, classNames, - try { Callable.from(callableName, callableSignature) } catch(e: ParseException) { null }, + try { + Callable.from(callableName, callableSignature) + } catch (e: ParseException) { + null + }, target?.toInt(), ext ) @@ -37,11 +47,25 @@ data class DRI(val packageName: String? = null, } catch (e: Throwable) { throw ParseException(s, 0) } + + val topLevel = DRI() } } +fun DRI.withClass(name: String) = copy(classNames = classNames.orEmpty() + ".$name") + +val DRI.parent: DRI + get() = when { + extra != null -> copy(extra = null) + target != null -> copy(target = null) + callable != null -> copy(callable = null) + classNames != null -> copy(classNames = classNames.substringBeforeLast('.').takeIf { it.isNotBlank() }) + else -> DRI.topLevel + } + data class Callable(val name: String, val receiver: String, val returnType: String, val params: List) { - fun signature() = "$receiver.$returnType.${params.joinToString(".")}" + fun signature() = "$receiver#$returnType#${params.joinToString("#")}" + companion object { fun from(name: String?, signature: String?): Callable = try { signature.toString() @@ -63,10 +87,25 @@ data class Callable(val name: String, val receiver: String, val returnType: Stri } catch (e: Throwable) { throw ParseException(s, 0) } + + fun from(descriptor: CallableDescriptor) = with(descriptor) { + Callable( + name.asString(), + extensionReceiverParameter?.value?.type?.constructorName.orEmpty(), + returnType?.constructorName.orEmpty(), + valueParameters.map { it.type.constructorName.orEmpty() } + ) + } } } -data class ClassReference(val dri: DRI, val subs: MutableList = mutableListOf()) +data class ClassReference(val dri: DRI, val subs: MutableList = mutableListOf()) { + private val subsText = subs.takeIf { it.isNotEmpty() }?.toString().orEmpty() + override fun toString() = "$dri$subsText" +} private operator fun List.component6(): T = get(5) +private val KotlinType.constructorName + get() = constructor.declarationDescriptor?.name?.asString() + diff --git a/core/src/main/kotlin/transformers/DefaultDocumentationToPageTransformer.kt b/core/src/main/kotlin/transformers/DefaultDocumentationToPageTransformer.kt index 13e9b0d0..fd2e6797 100644 --- a/core/src/main/kotlin/transformers/DefaultDocumentationToPageTransformer.kt +++ b/core/src/main/kotlin/transformers/DefaultDocumentationToPageTransformer.kt @@ -1,7 +1,6 @@ package org.jetbrains.dokka.transformers import org.jetbrains.dokka.DocumentationNode -import org.jetbrains.dokka.DocumentationNodes import org.jetbrains.dokka.pages.ContentNode import org.jetbrains.dokka.pages.ContentSymbol import org.jetbrains.dokka.pages.ModulePageNode -- cgit