From 8c534f14296a92fedc3410fb48de51836cb92859 Mon Sep 17 00:00:00 2001 From: Szymon Świstun Date: Wed, 5 Feb 2020 12:07:21 +0100 Subject: merger for functions --- core/src/main/kotlin/pages/PageNodes.kt | 10 +++-- .../main/kotlin/plugability/DefaultExtensions.kt | 2 + .../transformers/pages/DefaultPageNodeMerger.kt | 45 ++++++++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 core/src/main/kotlin/transformers/pages/DefaultPageNodeMerger.kt (limited to 'core/src/main/kotlin') diff --git a/core/src/main/kotlin/pages/PageNodes.kt b/core/src/main/kotlin/pages/PageNodes.kt index 6b67a734..244d951e 100644 --- a/core/src/main/kotlin/pages/PageNodes.kt +++ b/core/src/main/kotlin/pages/PageNodes.kt @@ -24,6 +24,7 @@ interface ContentPage: PageNode { fun modified( name: String = this.name, content: ContentNode = this.content, + dri: Set = this.dri, embeddedResources: List = this.embeddedResources, children: List = this.children ): ContentPage @@ -63,7 +64,6 @@ abstract class RootPageNode: PageNode { class ModulePageNode( override val name: String, override val content: ContentNode, - override val documentable: Documentable?, override val children: List, override val embeddedResources: List = listOf() @@ -71,11 +71,12 @@ class ModulePageNode( override val dri: Set = setOf(DRI.topLevel) override fun modified(name: String, children: List): ModulePageNode = - modified(name = name, content = this.content, children = children) + modified(name = name, content = this.content, dri = dri, children = children) override fun modified( name: String, content: ContentNode, + dri: Set, embeddedResources: List, children: List ): ModulePageNode = @@ -98,6 +99,7 @@ class PackagePageNode( override fun modified( name: String, content: ContentNode, + dri: Set, embeddedResources: List, children: List ): PackagePageNode = @@ -119,6 +121,7 @@ class ClasslikePageNode( override fun modified( name: String, content: ContentNode, + dri: Set, embeddedResources: List, children: List ): ClasslikePageNode = @@ -135,11 +138,12 @@ class MemberPageNode( override val embeddedResources: List = listOf() ) : ContentPage { override fun modified(name: String, children: List): MemberPageNode = - modified(name = name, content = this.content, children = children) + modified(name = name, content = this.content, children = children) as MemberPageNode override fun modified( name: String, content: ContentNode, + dri: Set, embeddedResources: List, children: List ): MemberPageNode = diff --git a/core/src/main/kotlin/plugability/DefaultExtensions.kt b/core/src/main/kotlin/plugability/DefaultExtensions.kt index d6331b86..b7ab1af1 100644 --- a/core/src/main/kotlin/plugability/DefaultExtensions.kt +++ b/core/src/main/kotlin/plugability/DefaultExtensions.kt @@ -10,6 +10,7 @@ import org.jetbrains.dokka.transformers.descriptors.DefaultDescriptorToDocumenta import org.jetbrains.dokka.transformers.documentation.DefaultDocumentationNodeMerger import org.jetbrains.dokka.transformers.documentation.DefaultDocumentationToPageTranslator import org.jetbrains.dokka.transformers.psi.DefaultPsiToDocumentationTranslator +import org.jetbrains.dokka.transformers.pages.DefaultPageNodeMerger internal object DefaultExtensions { @@ -31,6 +32,7 @@ internal object DefaultExtensions { CoreExtensions.documentationMerger -> DefaultDocumentationNodeMerger CoreExtensions.commentsToContentConverter -> converter.get(fullContext) CoreExtensions.documentationToPageTranslator -> DefaultDocumentationToPageTranslator + CoreExtensions.pageTransformer -> DefaultPageNodeMerger CoreExtensions.renderer -> renderer.get(fullContext) CoreExtensions.locationProviderFactory -> providerFactory.get(fullContext) CoreExtensions.outputWriter -> outputWriter.get(fullContext) diff --git a/core/src/main/kotlin/transformers/pages/DefaultPageNodeMerger.kt b/core/src/main/kotlin/transformers/pages/DefaultPageNodeMerger.kt new file mode 100644 index 00000000..7cd4aba8 --- /dev/null +++ b/core/src/main/kotlin/transformers/pages/DefaultPageNodeMerger.kt @@ -0,0 +1,45 @@ +package org.jetbrains.dokka.transformers.pages + +import org.jetbrains.dokka.pages.* + +object DefaultPageNodeMerger : PageNodeTransformer { + override fun invoke(input: RootPageNode): RootPageNode = + input.modified(children = input.children.map { it.mergeChildren() }) + + fun asGroup(dci: DCI, nodes: List): ContentGroup { + val n = nodes.first() + return ContentGroup(nodes, dci, n.platforms, n.style, n.extras) + } + + fun PageNode.mergeChildren(): PageNode = if (children.isNotEmpty()) { + children.groupBy { it.name } + .map { (k, v) -> + if (v.size > 1) { + v.mergePageNodes(k) + } else v.first() + } + .map { it.mergeChildren() } + .let { + modified(children = it) + } + } else this + + private fun List.mergePageNodes( + k: String + ): ContentPage { + val resChildren = this.flatMap { it.children }.distinct() + val contentChildren = this.filterIsInstance() + val dri = contentChildren.flatMap { it.dri }.toSet() + val dci = DCI( + dri = dri, + kind = contentChildren.first().content.dci.kind + ) + return contentChildren.first() + .modified( + dri = dri, + name = k, + children = resChildren, + content = asGroup(dci, contentChildren.map { it.content }) + ) + } +} \ No newline at end of file -- cgit