From 8f1bc3565e85281ee242a5620496aa3aedefa9c7 Mon Sep 17 00:00:00 2001 From: Filip ZybaƂa Date: Fri, 5 Jun 2020 16:42:50 +0200 Subject: Fixed javadoc crash on PSI sources. Allowed class hierarchy to have two roots: kotlin.Any and java.lang.Object --- .../main/kotlin/javadoc/KorteJavadocRenderer.kt | 11 ++-- .../main/kotlin/javadoc/pages/JavadocPageNodes.kt | 61 +++++++++++++++------- .../main/kotlin/javadoc/pages/htmlPreprocessors.kt | 2 +- 3 files changed, 50 insertions(+), 24 deletions(-) (limited to 'plugins/javadoc/src/main') diff --git a/plugins/javadoc/src/main/kotlin/javadoc/KorteJavadocRenderer.kt b/plugins/javadoc/src/main/kotlin/javadoc/KorteJavadocRenderer.kt index ad83968e..a16812a8 100644 --- a/plugins/javadoc/src/main/kotlin/javadoc/KorteJavadocRenderer.kt +++ b/plugins/javadoc/src/main/kotlin/javadoc/KorteJavadocRenderer.kt @@ -150,10 +150,10 @@ class KorteJavadocRenderer(val outputWriter: OutputWriter, val context: DokkaCon }.joinToString("\n") }, TeFunction("renderInheritanceGraph") { args -> - val node = args.first() as TreeViewPage.InheritanceNode + val rootNodes = args.first() as List - fun drawRec(node: TreeViewPage.InheritanceNode): String = - "
  • " + node.dri.let { dri -> + fun drawRec(node: TreeViewPage.InheritanceNode) : String { + val returnValue = "
  • " + node.dri.let { dri -> listOfNotNull( dri.packageName, dri.classNames @@ -169,8 +169,9 @@ class KorteJavadocRenderer(val outputWriter: OutputWriter, val context: DokkaCon } + node.children.filterNot { it.isInterface }.takeUnless { it.isEmpty() }?.let { "
      " + it.joinToString("\n", transform = ::drawRec) + "
    " }.orEmpty() + "
  • " - - drawRec(node) + return returnValue + } + rootNodes.joinToString{ drawRec(it) } }, Filter("length") { subject.dynamicLength() } ).forEach { diff --git a/plugins/javadoc/src/main/kotlin/javadoc/pages/JavadocPageNodes.kt b/plugins/javadoc/src/main/kotlin/javadoc/pages/JavadocPageNodes.kt index b73b2b36..a1bffdc2 100644 --- a/plugins/javadoc/src/main/kotlin/javadoc/pages/JavadocPageNodes.kt +++ b/plugins/javadoc/src/main/kotlin/javadoc/pages/JavadocPageNodes.kt @@ -146,7 +146,7 @@ class TreeViewPage( val classes: List?, override val dri: Set, override val documentable: Documentable?, - val root: RootPageNode + val root: PageNode ) : JavadocPageNode { init { assert(packages == null || classes == null) @@ -157,7 +157,7 @@ class TreeViewPage( getDocumentableEntries(node) }.groupBy({ it.first }) { it.second }.map { (l, r) -> l to r.first() }.toMap() - private val descriptorMap = getDescriptorMap(root) + private val descriptorMap = getDescriptorMap() private val inheritanceTuple = generateInheritanceTree() private val classGraph = inheritanceTuple.first private val interfaceGraph = inheritanceTuple.second @@ -213,7 +213,7 @@ class TreeViewPage( emptySet() ) - private fun generateInheritanceTree(): Pair { + private fun generateInheritanceTree(): Pair, List> { val mergeMap = mutableMapOf() fun addToMap(info: InheritanceNode, map: MutableMap) { @@ -250,35 +250,60 @@ class TreeViewPage( fun interfaceTree(node: InheritanceNode) = interfaceTreeRec(node).first() // TODO.single() - fun gatherPsiClasses(psi: PsiType): List>> = psi.superTypes.toList().let { l -> + fun gatherPsiClasses(psi: PsiClass): List>> = psi.supers.toList().let { l -> listOf(psi to l) + l.flatMap { gatherPsiClasses(it) } } - fun gatherPsiClasses(psi: PsiClass): List>> = - psi.superTypes.flatMap(::gatherPsiClasses) - val psiClasses = documentables.flatMap { (_, v) -> (v as? WithExpectActual)?.sources?.values.orEmpty() } + val psiInheritanceTree = documentables.flatMap { (_, v) -> (v as? WithExpectActual)?.sources?.values.orEmpty() } .filterIsInstance().mapNotNull { it.psi as? PsiClass }.flatMap(::gatherPsiClasses) - .groupBy({ it.first }) { it.second } - .map { (k, v) -> k to v.flatten().distinct() }.toMap() + .flatMap { entry -> entry.second.map { it to entry.first } } + .let { + it + it.map { it.second to null } + } + .groupBy({it.first}) {it.second} + .map { it.key to it.value.filterNotNull().distinct() } + .map { (k,v) -> + InheritanceNode( + DRI.from(k), + v.map { InheritanceNode(DRI.from(it)) }, + k.supers.filter { it.isInterface }.map { DRI.from(it) }, + k.isInterface + ) + + } - val s1 = descriptorMap.flatMap { (k, v) -> - v.typeConstructor.supertypes.map { getClassDescriptorForType(it) }.map { it to listOf(v) } - }.let { it + it.flatMap { e -> e.second.map { it to emptyList() } } } - .groupBy({ it.first }) { it.second } + val descriptorInheritanceTree = descriptorMap.flatMap { + (_,v) -> v.typeConstructor.supertypes + .map { getClassDescriptorForType(it) to v } + } + .let { + it + it.map { it.second to null } + } + .groupBy ({ it.first }) { it.second } + .map { it.key to it.value.filterNotNull().distinct() } .map { (k, v) -> InheritanceNode( DRI.from(k), - v.flatten().map { InheritanceNode(DRI.from(it)) }, + v.map { InheritanceNode(DRI.from(it)) }, k.typeConstructor.supertypes.map { getClassDescriptorForType(it) } .mapNotNull { cd -> cd.takeIf { it.kind == ClassKind.INTERFACE }?.let { DRI.from(it) } }, isInterface = k.kind == ClassKind.INTERFACE ) } - s1.forEach { addToMap(it, mergeMap) } - val g = mergeMap.entries.find { it.key.classNames == "Any" }?.value?.dri?.let(::collect) - return g?.let{classTree(it) to interfaceTree(it)} ?: run { throw IllegalStateException("Building inheritance tree failed") } + descriptorInheritanceTree.forEach { addToMap(it, mergeMap) } + psiInheritanceTree.forEach { addToMap(it, mergeMap) } + + val g = mergeMap.entries.find { it.key.classNames == "Any" || it.key.classNames == "Object" }?.value?.dri?.let(::collect) + val rootClasses = listOf("Any", "Object") + val rootNodes = mergeMap.entries.filter { + it.key.classNames in rootClasses //TODO: Probably should be matched by DRI, not just className + }.map { + collect(it.value.dri) + } + + return rootNodes.let { Pair(it.map(::classTree), it.map(::interfaceTree)) } } private fun generateInterfaceGraph() { @@ -289,7 +314,7 @@ class TreeViewPage( listOfNotNull(node.documentable?.let { it.dri to it }) + node.children.filterIsInstance().flatMap(::getDocumentableEntries) - private fun getDescriptorMap(root: RootPageNode): Map { + private fun getDescriptorMap(): Map { val map: MutableMap = mutableMapOf() documentables .mapNotNull { (k, v) -> diff --git a/plugins/javadoc/src/main/kotlin/javadoc/pages/htmlPreprocessors.kt b/plugins/javadoc/src/main/kotlin/javadoc/pages/htmlPreprocessors.kt index fc0771e7..8795f359 100644 --- a/plugins/javadoc/src/main/kotlin/javadoc/pages/htmlPreprocessors.kt +++ b/plugins/javadoc/src/main/kotlin/javadoc/pages/htmlPreprocessors.kt @@ -31,7 +31,7 @@ object TreeViewInstaller : PageTransformer { packages = node.children().map { installPackageTreeNode(it, root) }, classes = null, dri = node.dri, - documentable = null, + documentable = node.documentable, root = root ) -- cgit