From 2bfb7733dfef8da0271a01a7275ea42ecb69d93a Mon Sep 17 00:00:00 2001 From: Paweł Marks Date: Mon, 17 Feb 2020 10:55:44 +0100 Subject: Cleans up page mergers and moves them to base plugin --- plugins/base/src/main/kotlin/DokkaBase.kt | 22 +++++++++++++- .../pages/merger/FallbackPageMergerStrategy.kt | 12 ++++++++ .../pages/merger/PageMergerStrategy.kt | 9 ++++++ .../transformers/pages/merger/PageNodeMerger.kt | 29 ++++++++++++++++++ .../merger/SameMethodNamePageMergerStrategy.kt | 35 ++++++++++++++++++++++ 5 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 plugins/base/src/main/kotlin/transformers/pages/merger/FallbackPageMergerStrategy.kt create mode 100644 plugins/base/src/main/kotlin/transformers/pages/merger/PageMergerStrategy.kt create mode 100644 plugins/base/src/main/kotlin/transformers/pages/merger/PageNodeMerger.kt create mode 100644 plugins/base/src/main/kotlin/transformers/pages/merger/SameMethodNamePageMergerStrategy.kt (limited to 'plugins/base/src/main') diff --git a/plugins/base/src/main/kotlin/DokkaBase.kt b/plugins/base/src/main/kotlin/DokkaBase.kt index bafd30ff..ed23d77e 100644 --- a/plugins/base/src/main/kotlin/DokkaBase.kt +++ b/plugins/base/src/main/kotlin/DokkaBase.kt @@ -4,11 +4,17 @@ import org.jetbrains.dokka.CoreExtensions import org.jetbrains.dokka.base.transformers.descriptors.DefaultDescriptorToDocumentationTranslator import org.jetbrains.dokka.base.transformers.documentables.DefaultDocumentableMerger import org.jetbrains.dokka.base.transformers.documentables.DefaultDocumentablesToPageTranslator +import org.jetbrains.dokka.base.transformers.pages.merger.FallbackPageMergerStrategy +import org.jetbrains.dokka.base.transformers.pages.merger.PageMergerStrategy +import org.jetbrains.dokka.base.transformers.pages.merger.PageNodeMerger +import org.jetbrains.dokka.base.transformers.pages.merger.SameMethodNamePageMergerStrategy import org.jetbrains.dokka.base.transformers.psi.DefaultPsiToDocumentationTranslator import org.jetbrains.dokka.plugability.DokkaPlugin import org.jetbrains.dokka.renderers.html.HtmlRenderer class DokkaBase: DokkaPlugin() { + val pageMergerStrategy by extensionPoint() + val descriptorToDocumentationTranslator by extending(isFallback = true) { CoreExtensions.descriptorToDocumentationTranslator providing ::DefaultDescriptorToDocumentationTranslator } @@ -25,7 +31,21 @@ class DokkaBase: DokkaPlugin() { CoreExtensions.documentablesToPageTranslator with DefaultDocumentablesToPageTranslator } + val pageMerger by extending { + CoreExtensions.pageTransformer providing { ctx -> PageNodeMerger(ctx[pageMergerStrategy]) } + } + + val fallbackMerger by extending { + pageMergerStrategy providing { ctx -> FallbackPageMergerStrategy(ctx.logger) } + } + + val sameMethodNameMerger by extending { + pageMergerStrategy with SameMethodNamePageMergerStrategy order { + before(fallbackMerger) + } + } + val htmlRenderer by extending { - CoreExtensions.renderer providing ::HtmlRenderer + CoreExtensions.renderer providing ::HtmlRenderer applyIf { format == "html" } } } \ No newline at end of file diff --git a/plugins/base/src/main/kotlin/transformers/pages/merger/FallbackPageMergerStrategy.kt b/plugins/base/src/main/kotlin/transformers/pages/merger/FallbackPageMergerStrategy.kt new file mode 100644 index 00000000..df0c27ee --- /dev/null +++ b/plugins/base/src/main/kotlin/transformers/pages/merger/FallbackPageMergerStrategy.kt @@ -0,0 +1,12 @@ +package org.jetbrains.dokka.base.transformers.pages.merger + +import org.jetbrains.dokka.pages.PageNode +import org.jetbrains.dokka.utilities.DokkaLogger + +class FallbackPageMergerStrategy(private val logger: DokkaLogger) : PageMergerStrategy { + override fun tryMerge(pages: List, path: List): List { + val renderedPath = path.joinToString(separator = "/") + if (pages.size != 1) logger.warn("For $renderedPath: expected 1 page, but got ${pages.size}") + return listOf(pages.first()) + } +} \ No newline at end of file diff --git a/plugins/base/src/main/kotlin/transformers/pages/merger/PageMergerStrategy.kt b/plugins/base/src/main/kotlin/transformers/pages/merger/PageMergerStrategy.kt new file mode 100644 index 00000000..b73b17e0 --- /dev/null +++ b/plugins/base/src/main/kotlin/transformers/pages/merger/PageMergerStrategy.kt @@ -0,0 +1,9 @@ +package org.jetbrains.dokka.base.transformers.pages.merger + +import org.jetbrains.dokka.pages.PageNode + +interface PageMergerStrategy { + + fun tryMerge(pages: List, path: List): List + +} \ No newline at end of file diff --git a/plugins/base/src/main/kotlin/transformers/pages/merger/PageNodeMerger.kt b/plugins/base/src/main/kotlin/transformers/pages/merger/PageNodeMerger.kt new file mode 100644 index 00000000..5ecf8d9b --- /dev/null +++ b/plugins/base/src/main/kotlin/transformers/pages/merger/PageNodeMerger.kt @@ -0,0 +1,29 @@ +package org.jetbrains.dokka.base.transformers.pages.merger + +import org.jetbrains.dokka.pages.PageNode +import org.jetbrains.dokka.pages.RootPageNode +import org.jetbrains.dokka.transformers.pages.PageNodeTransformer + +class PageNodeMerger(private val strategies: Iterable) : PageNodeTransformer { + override fun invoke(input: RootPageNode): RootPageNode = + input.modified(children = input.children.map { it.mergeChildren(emptyList()) }) + + private fun PageNode.mergeChildren(path: List): PageNode = children.groupBy { it.name } + .map { (n, v) -> mergePageNodes(v, path + n) } + .let { pages -> + modified(children = pages.map { it.assertSingle(path) }.map { it.mergeChildren(path + it.name) }) + } + + private fun mergePageNodes(pages: List, path: List): List = + strategies.fold(pages) { acc, strategy -> tryMerge(strategy, acc, path) } + + private fun tryMerge(strategy: PageMergerStrategy, pages: List, path: List) = + if (pages.size > 1) strategy.tryMerge(pages, path) else pages +} + +private fun Iterable.assertSingle(path: List): T = try { + single() + } catch (e: Exception) { + val renderedPath = path.joinToString(separator = "/") + throw IllegalStateException("Page merger is missconfigured. Error for $renderedPath: ${e.message}") + } \ No newline at end of file diff --git a/plugins/base/src/main/kotlin/transformers/pages/merger/SameMethodNamePageMergerStrategy.kt b/plugins/base/src/main/kotlin/transformers/pages/merger/SameMethodNamePageMergerStrategy.kt new file mode 100644 index 00000000..fef55dd2 --- /dev/null +++ b/plugins/base/src/main/kotlin/transformers/pages/merger/SameMethodNamePageMergerStrategy.kt @@ -0,0 +1,35 @@ +package org.jetbrains.dokka.base.transformers.pages.merger + +import org.jetbrains.dokka.pages.* + +object SameMethodNamePageMergerStrategy : PageMergerStrategy { + override fun tryMerge(pages: List, path: List): List { + val name = pages.first().name + val members = pages.filterIsInstance() + val others = pages.filterNot { it is MemberPageNode } + + val resChildren = members.flatMap { it.children }.distinct() + val dri = members.flatMap { it.dri }.toSet() + val dci = DCI( + dri = dri, + kind = members.first().content.dci.kind + ) + + val merged = MemberPageNode( + dri = dri, + name = name, + children = resChildren, + content = asGroup( + dci, + members.map { it.content }), + documentable = null + ) + + return others + listOf(merged) + } + + fun asGroup(dci: DCI, nodes: List): ContentGroup { + val n = nodes.first() + return ContentGroup(nodes, dci, n.platforms, n.style, n.extras) + } +} \ No newline at end of file -- cgit