diff options
author | Paweł Marks <pmarks@virtuslab.com> | 2020-02-17 10:55:44 +0100 |
---|---|---|
committer | Paweł Marks <Kordyjan@users.noreply.github.com> | 2020-02-18 13:28:23 +0100 |
commit | 2bfb7733dfef8da0271a01a7275ea42ecb69d93a (patch) | |
tree | c3e6369da1a795d1b2dcf8342629742ca0dfbde3 /plugins/base | |
parent | f625cef495d625d81ee22e950083f57cc4fab875 (diff) | |
download | dokka-2bfb7733dfef8da0271a01a7275ea42ecb69d93a.tar.gz dokka-2bfb7733dfef8da0271a01a7275ea42ecb69d93a.tar.bz2 dokka-2bfb7733dfef8da0271a01a7275ea42ecb69d93a.zip |
Cleans up page mergers and moves them to base plugin
Diffstat (limited to 'plugins/base')
6 files changed, 115 insertions, 8 deletions
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<PageMergerStrategy>() + 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<PageNode>, path: List<String>): List<PageNode> { + 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<PageNode>, path: List<String>): List<PageNode> + +}
\ 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<PageMergerStrategy>) : PageNodeTransformer { + override fun invoke(input: RootPageNode): RootPageNode = + input.modified(children = input.children.map { it.mergeChildren(emptyList()) }) + + private fun PageNode.mergeChildren(path: List<String>): 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<PageNode>, path: List<String>): List<PageNode> = + strategies.fold(pages) { acc, strategy -> tryMerge(strategy, acc, path) } + + private fun tryMerge(strategy: PageMergerStrategy, pages: List<PageNode>, path: List<String>) = + if (pages.size > 1) strategy.tryMerge(pages, path) else pages +} + +private fun <T> Iterable<T>.assertSingle(path: List<String>): 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<PageNode>, path: List<String>): List<PageNode> { + val name = pages.first().name + val members = pages.filterIsInstance<MemberPageNode>() + 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<ContentNode>): ContentGroup { + val n = nodes.first() + return ContentGroup(nodes, dci, n.platforms, n.style, n.extras) + } +}
\ No newline at end of file diff --git a/plugins/base/src/test/kotlin/pageMerger/PageNodeMergerTest.kt b/plugins/base/src/test/kotlin/pageMerger/PageNodeMergerTest.kt index 88e57ddb..c6076d54 100644 --- a/plugins/base/src/test/kotlin/pageMerger/PageNodeMergerTest.kt +++ b/plugins/base/src/test/kotlin/pageMerger/PageNodeMergerTest.kt @@ -4,15 +4,15 @@ import org.jetbrains.dokka.CoreExtensions import org.jetbrains.dokka.pages.ContentPage import org.jetbrains.dokka.pages.PageNode import org.jetbrains.dokka.plugability.DokkaPlugin -import org.jetbrains.dokka.transformers.pages.DefaultPageMergerStrategy -import org.jetbrains.dokka.transformers.pages.SameMethodNamePageMergerStrategy +import org.jetbrains.dokka.base.transformers.pages.merger.SameMethodNamePageMergerStrategy import org.jetbrains.dokka.utilities.DokkaLogger +import org.junit.Ignore import org.junit.Test import testApi.testRunner.AbstractCoreTest class PageNodeMergerTest : AbstractCoreTest() { - object SameNameStrategy : DokkaPlugin() { + /* object SameNameStrategy : DokkaPlugin() { val strategy by extending { CoreExtensions.pageMergerStrategy with SameMethodNamePageMergerStrategy } } @@ -36,6 +36,7 @@ class PageNodeMergerTest : AbstractCoreTest() { override fun report() = TODO() } + */ @Test fun sameNameStrategyTest() { @@ -61,8 +62,8 @@ class PageNodeMergerTest : AbstractCoreTest() { | fun test(str: String): String = str |} """.trimMargin(), - configuration, - pluginOverrides = listOf(SameNameStrategy) + configuration/*, + pluginOverrides = listOf(SameNameStrategy)*/ ) { pagesTransformationStage = { val allChildren = it.childrenRec().filterIsInstance<ContentPage>() @@ -78,6 +79,7 @@ class PageNodeMergerTest : AbstractCoreTest() { } } + @Ignore("TODO: reenable when we have infrastructure for turning off extensions") @Test fun defaultStrategyTest() { val strList: MutableList<String> = mutableListOf() @@ -103,8 +105,8 @@ class PageNodeMergerTest : AbstractCoreTest() { | fun test(str: String): String = str |} """.trimMargin(), - configuration, - pluginOverrides = listOf(DefaultStrategy(strList)) + configuration/*, + pluginOverrides = listOf(DefaultStrategy(strList)) */ ) { pagesTransformationStage = { root -> val allChildren = root.childrenRec().filterIsInstance<ContentPage>() |