From c33e8e16a0e446b78496cbcd878ba76ea51c0940 Mon Sep 17 00:00:00 2001 From: Błażej Kardyś Date: Thu, 31 Oct 2019 11:44:03 +0100 Subject: Temp changes --- core/src/main/kotlin/Model/DocumentationNode.kt | 339 +++------------------ .../main/kotlin/Model/DocumentationReference.kt | 115 ------- 2 files changed, 42 insertions(+), 412 deletions(-) delete mode 100644 core/src/main/kotlin/Model/DocumentationReference.kt (limited to 'core/src/main/kotlin/Model') diff --git a/core/src/main/kotlin/Model/DocumentationNode.kt b/core/src/main/kotlin/Model/DocumentationNode.kt index 66ee33e1..19ba22a6 100644 --- a/core/src/main/kotlin/Model/DocumentationNode.kt +++ b/core/src/main/kotlin/Model/DocumentationNode.kt @@ -1,330 +1,75 @@ package org.jetbrains.dokka -import org.jetbrains.kotlin.descriptors.DeclarationDescriptor -import java.util.* -import kotlin.reflect.KClass -import kotlin.reflect.full.primaryConstructor +import org.jetbrains.dokka.links.ClassReference +import org.jetbrains.kotlin.descriptors.* class DocumentationNodes { - class Unknown(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) + class Module(name: String, parent: DocumentationNode<*>? = null): + DocumentationNode(name, parent = parent) - class Package(name: String) : - DocumentationNode(name) + class Package(name: String, parent: DocumentationNode<*>): + DocumentationNode(name, parent = parent) - class Class(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) { + class Class(name: String, descriptor: ClassDescriptor, parent: DocumentationNode<*>? = null) : + DocumentationNode(name, descriptor, parent) { override val classLike: Boolean = true - } - - class Interface(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) { - override val classLike: Boolean = true - override val superclassType: DocumentationNode? = null - } - - class Enum(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) { - override val classLike: Boolean = true - } - - class AnnotationClass(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) { - override val classLike: Boolean = true - } - - class Exception(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) { - override val classLike: Boolean = true - } - - class EnumItem(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) { - override val memberLike = true - } - - class Object(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) { - override val classLike: Boolean = true - } - - class TypeAlias(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) - - class Constructor(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) { + 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 Function(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) { + class Function(name: String, descriptor: FunctionDescriptor) : + DocumentationNode(name, descriptor) { override val memberLike = true } - class Property(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) { + class Property(name: String, descriptor: PropertyDescriptor) : + DocumentationNode(name, descriptor) { override val memberLike = true } - - class Field(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) { +/* + class Field(name: String, descriptor: FieldDescriptor) : + DocumentationNode(name, descriptor) { override val memberLike = true - } - - class CompanionObjectProperty(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) { - override val memberLike = true - } - - class CompanionObjectFunction(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) { - override val memberLike = true - } - - class Parameter(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) - - class Receiver(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) - - class TypeParameter(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) - - class Type(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) - - class Supertype(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) { - override val superclassType: DocumentationNode? = - (links + listOfNotNull(externalType)).firstOrNull { it.classLike }?.superclassType - } - - class UpperBound(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) - - class LowerBound(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) - - class TypeAliasUnderlyingType(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) - - class NullabilityModifier(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) - - class Module(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) - - class ExternalClass(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) - - class Annotation(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) - - class Value(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) - - class SourceUrl(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) - - class SourcePosition(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) - - class ExternalLink(name: String) : DocumentationNode(name) - - class QualifiedName(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) + }*/ - class Platform(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) - - class AllTypes(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) - - class OverloadGroupNote(name: String, descriptor: DeclarationDescriptor?) : - DocumentationNode(name, descriptor) + class Parameter(name: String, descriptor: ParameterDescriptor?) : + DocumentationNode(name, descriptor) +/* + class Annotation(name: String, descriptor: AnnotationDescriptor?) : + DocumentationNode(name, descriptor)*/ } -abstract class DocumentationNode( +abstract class DocumentationNode( var name: String, - val descriptor: DeclarationDescriptor? = null + val descriptor: T? = null, + val parent: DocumentationNode<*>? = null ) { - private val references = LinkedHashSet() + private val children = mutableListOf>() open val classLike = false open val memberLike = false - val owner: DocumentationNode? - get() = references(RefKind.Owner).singleOrNull()?.to - val details: List - get() = references(RefKind.Detail).map { it.to } - val members: List - get() = references(RefKind.Member).map { it.to } - val origins: List - get() = references(RefKind.Origin).map { it.to } - - val inheritedMembers: List - get() = references(RefKind.InheritedMember).map { it.to } - val allInheritedMembers: List - get() = recursiveInheritedMembers() - val inheritedCompanionObjectMembers: List - get() = references(RefKind.InheritedCompanionObjectMember).map { it.to } - val extensions: List - get() = references(RefKind.Extension).map { it.to } - val inheritors: List - get() = references(RefKind.Inheritor).map { it.to } - val overrides: List - get() = references(RefKind.Override).map { it.to } - val links: List - get() = references(RefKind.Link).map { it.to } - val hiddenLinks: List - get() = references(RefKind.HiddenLink).map { it.to } - val annotations: List - get() = references(RefKind.Annotation).map { it.to } - val deprecation: DocumentationNode? - get() = references(RefKind.Deprecation).singleOrNull()?.to - val platforms: List - get() = references(RefKind.Platform).map { it.to.name } - val externalType: DocumentationNode? - get() = references(RefKind.ExternalType).map { it.to }.firstOrNull() - - open val superclassType: DocumentationNode? - get() = if (classLike) { - supertypes.firstOrNull { - (it.links + listOfNotNull(it.externalType)).any { it.isSuperclassFor(this) } - } - } else null - - val superclassTypeSequence: Sequence - get() = generateSequence(superclassType) { - it.superclassType - } - - // TODO: Should we allow node mutation? Model merge will copy by ref, so references are transparent, which could nice - fun addReferenceTo(to: DocumentationNode, kind: RefKind) { - references.add(DocumentationReference(this, to, kind)) - } - - fun addReference(reference: DocumentationReference) { - references.add(reference) - } - - fun dropReferences(predicate: (DocumentationReference) -> Boolean) { - references.removeAll(predicate) - } - - fun addAllReferencesFrom(other: DocumentationNode) { - references.addAll(other.references) - } - - private fun Collection.filterByKind(kind: KClass) = - filter { node: DocumentationNode -> node::class == kind } - - private fun Collection.singleByKind(kind: KClass) = - single { node: DocumentationNode -> node::class == kind } - - fun details(kind: KClass) = details.filterByKind(kind) - fun members(kind: KClass) = members.filterByKind(kind) - fun inheritedMembers(kind: KClass): List = - inheritedMembers.filterByKind(kind) - - fun inheritedCompanionObjectMembers(kind: KClass): List = - inheritedCompanionObjectMembers.filterByKind(kind) - - fun links(kind: KClass): List = links.filterByKind(kind) - - fun detail(kind: KClass): DocumentationNode = details.singleByKind(kind) - fun detailOrNull(kind: KClass): DocumentationNode? = - details.singleOrNull { it::class == kind } - - fun member(kind: KClass): DocumentationNode = members.singleByKind(kind) - fun link(kind: KClass): DocumentationNode = links.singleByKind(kind) - - - fun references(kind: RefKind): List = references.filter { it.kind == kind } - fun allReferences(): Set = references + fun addChild(child: DocumentationNode<*>) = + children.add(child) override fun toString(): String { return "${javaClass.name}:$name" } -} - -fun KClass.createNode(name: String, descriptor: DeclarationDescriptor? = null) = - primaryConstructor?.call(name, descriptor) - ?: throw IllegalArgumentException("Cannot create node of type ${this::class}: invalid primary constructor") - -val DocumentationNode.supertypes: List - get() = details(DocumentationNodes.Supertype::class) - -class DocumentationModule(name: String) : DocumentationNode(name) - -val DocumentationNode.path: List - get() { - val parent = owner ?: return listOf(this) - return parent.path + this - } - -fun DocumentationNode.append(child: DocumentationNode, kind: RefKind) { - addReferenceTo(child, kind) - when (kind) { - RefKind.Detail -> child.addReferenceTo(this, RefKind.Owner) - RefKind.Member -> child.addReferenceTo(this, RefKind.Owner) - RefKind.Owner -> child.addReferenceTo(this, RefKind.Member) - RefKind.Origin -> child.addReferenceTo(this, RefKind.Owner) - else -> { /* Do not add any links back for other types */ - } - } -} - -fun DocumentationNode.appendTextNode( - text: String, - kind: KClass, - descriptor: DeclarationDescriptor? = null, - refKind: RefKind = RefKind.Detail -) { - append(kind.createNode(text, descriptor), refKind) -} - -fun DocumentationNode.qualifiedName(): String { - if (this is DocumentationNodes.Type) { - return qualifiedNameFromType() - } else if (this is DocumentationNodes.Package) { - return name - } - return path.dropWhile { it is DocumentationNodes.Module }.map { it.name }.filter { it.isNotEmpty() } - .joinToString(".") -} - -fun DocumentationNode.simpleName() = name.substringAfterLast('.') - -private fun DocumentationNode.recursiveInheritedMembers(): List { - val allInheritedMembers = mutableListOf() - recursiveInheritedMembers(allInheritedMembers) - return allInheritedMembers -} - -private fun DocumentationNode.recursiveInheritedMembers(allInheritedMembers: MutableList) { - allInheritedMembers.addAll(inheritedMembers) - System.out.println(allInheritedMembers.size) - inheritedMembers.groupBy { it.owner!! }.forEach { (node, _) -> - node.recursiveInheritedMembers(allInheritedMembers) - } -} - -private fun DocumentationNode.isSuperclassFor(node: DocumentationNode): Boolean { - return when (node) { - is DocumentationNodes.Object, - is DocumentationNodes.Class, - is DocumentationNodes.Enum -> this is DocumentationNodes.Class - is DocumentationNodes.Exception -> this is DocumentationNodes.Class || this is DocumentationNodes.Exception - else -> false - } -} - -fun DocumentationNode.classNodeNameWithOuterClass(): String { - assert(classLike) - return path.dropWhile { it is DocumentationNodes.Package || it is DocumentationNodes.Module } - .joinToString(separator = ".") { it.name } } \ No newline at end of file diff --git a/core/src/main/kotlin/Model/DocumentationReference.kt b/core/src/main/kotlin/Model/DocumentationReference.kt deleted file mode 100644 index e46a4a21..00000000 --- a/core/src/main/kotlin/Model/DocumentationReference.kt +++ /dev/null @@ -1,115 +0,0 @@ -package org.jetbrains.dokka - -import com.google.inject.Singleton - -enum class RefKind { - Owner, - Member, - InheritedMember, - InheritedCompanionObjectMember, - Detail, - Link, - HiddenLink, - Extension, - Inheritor, - Superclass, - Override, - Annotation, - HiddenAnnotation, - Deprecation, - TopLevelPage, - Platform, - ExternalType, - Origin, - SinceKotlin -} - -data class DocumentationReference(val from: DocumentationNode, val to: DocumentationNode, val kind: RefKind) { -} - -sealed class NodeResolver { - abstract fun resolve(nodeRephGraph: NodeReferenceGraph): DocumentationNode? - class BySignature(var signature: String) : NodeResolver() { - override fun resolve(nodeRephGraph: NodeReferenceGraph): DocumentationNode? { - return nodeRephGraph.lookup(signature) - } - } - - class Exact(var exactNode: DocumentationNode) : NodeResolver() { - override fun resolve(nodeRephGraph: NodeReferenceGraph): DocumentationNode? { - return exactNode - } - } -} - -class PendingDocumentationReference(val lazyNodeFrom: NodeResolver, - val lazyNodeTo: NodeResolver, - val kind: RefKind) { - fun resolve(nodeRephGraph: NodeReferenceGraph) { - val fromNode = lazyNodeFrom.resolve(nodeRephGraph) - val toNode = lazyNodeTo.resolve(nodeRephGraph) - if (fromNode != null && toNode != null) { - fromNode.addReferenceTo(toNode, kind) - } - } -} - -class NodeReferenceGraph { - private val nodeMap = hashMapOf() - val nodeMapView: Map - get() = HashMap(nodeMap) - - val references = arrayListOf() - - fun register(signature: String, node: DocumentationNode) { - nodeMap[signature] = node - } - - fun link(fromNode: DocumentationNode, toSignature: String, kind: RefKind) { - references.add( - PendingDocumentationReference( - NodeResolver.Exact(fromNode), - NodeResolver.BySignature(toSignature), - kind - )) - } - - fun link(fromSignature: String, toNode: DocumentationNode, kind: RefKind) { - references.add( - PendingDocumentationReference( - NodeResolver.BySignature(fromSignature), - NodeResolver.Exact(toNode), - kind - ) - ) - } - - fun link(fromSignature: String, toSignature: String, kind: RefKind) { - references.add( - PendingDocumentationReference( - NodeResolver.BySignature(fromSignature), - NodeResolver.BySignature(toSignature), - kind - ) - ) - } - - fun addReference(reference: PendingDocumentationReference) { - references.add(reference) - } - - fun lookup(signature: String) = nodeMap[signature] - - fun lookupOrWarn(signature: String, logger: DokkaLogger): DocumentationNode? { - val result = nodeMap[signature] - if (result == null) { - logger.warn("Can't find node by signature `$signature`." + - "This is probably caused by invalid configuration of cross-module dependencies") - } - return result - } - - fun resolveReferences() { - references.forEach { it.resolve(this) } - } -} \ No newline at end of file -- cgit