From bbe5b00387da8fb8a54f8a8ecd80f2f2dea06830 Mon Sep 17 00:00:00 2001 From: "sebastian.sellmair" Date: Tue, 11 Aug 2020 12:52:43 +0200 Subject: Move utility APIs into `PreMergeDocumentationTransformer` --- core/src/main/kotlin/model/Documentable.kt | 2 +- .../PreMergeDocumentableTransformer.kt | 26 +++++++++++++++++++-- .../EmptyPackagesFilterTransformer.kt | 27 +++++++++------------- .../SuppressedDocumentableFilterTransformer.kt | 17 -------------- .../test/kotlin/filter/EmptyPackagesFilterTest.kt | 10 ++++---- 5 files changed, 42 insertions(+), 40 deletions(-) diff --git a/core/src/main/kotlin/model/Documentable.kt b/core/src/main/kotlin/model/Documentable.kt index 4f9732c0..3f91e3cb 100644 --- a/core/src/main/kotlin/model/Documentable.kt +++ b/core/src/main/kotlin/model/Documentable.kt @@ -113,7 +113,7 @@ data class DPackage( ) : Documentable(), WithScope, WithExtraProperties { override val name = dri.packageName.orEmpty() override val children: List - get() = (properties + functions + classlikes) + get() = (properties + functions + classlikes + typealiases) override fun withNewExtras(newExtras: PropertyContainer) = copy(extra = newExtras) } diff --git a/core/src/main/kotlin/transformers/documentation/PreMergeDocumentableTransformer.kt b/core/src/main/kotlin/transformers/documentation/PreMergeDocumentableTransformer.kt index b67a1d57..dfc7319f 100644 --- a/core/src/main/kotlin/transformers/documentation/PreMergeDocumentableTransformer.kt +++ b/core/src/main/kotlin/transformers/documentation/PreMergeDocumentableTransformer.kt @@ -1,8 +1,30 @@ package org.jetbrains.dokka.transformers.documentation +import org.jetbrains.dokka.DokkaConfiguration +import org.jetbrains.dokka.DokkaConfiguration.DokkaSourceSet import org.jetbrains.dokka.model.DModule -import org.jetbrains.dokka.plugability.DokkaContext +import org.jetbrains.dokka.model.Documentable +import org.jetbrains.dokka.model.DocumentableSource +import org.jetbrains.dokka.model.WithExpectActual interface PreMergeDocumentableTransformer { operator fun invoke(modules: List): List -} \ No newline at end of file + + /* Convenience functions */ + /** + * A [PreMergeDocumentableTransformer] can safely assume that documentables are not merged and therefore + * only belong to a single source set + */ + val Documentable.sourceSet: DokkaSourceSet get() = sourceSets.single() + + val Documentable.perPackageOptions: DokkaConfiguration.PackageOptions? + get() { + val packageName = dri.packageName ?: return null + return sourceSet.perPackageOptions + .sortedByDescending { packageOptions -> packageOptions.prefix.length } + .firstOrNull { packageOptions -> packageName.startsWith(packageOptions.prefix) } + } + + val T.source: DocumentableSource where T : Documentable, T : WithExpectActual + get() = checkNotNull(sources[sourceSet]) +} diff --git a/plugins/base/src/main/kotlin/transformers/documentables/EmptyPackagesFilterTransformer.kt b/plugins/base/src/main/kotlin/transformers/documentables/EmptyPackagesFilterTransformer.kt index 61abfbd7..2528b5f0 100644 --- a/plugins/base/src/main/kotlin/transformers/documentables/EmptyPackagesFilterTransformer.kt +++ b/plugins/base/src/main/kotlin/transformers/documentables/EmptyPackagesFilterTransformer.kt @@ -1,28 +1,23 @@ package org.jetbrains.dokka.base.transformers.documentables -import org.jetbrains.dokka.DokkaConfiguration import org.jetbrains.dokka.model.DModule -import org.jetbrains.dokka.model.DPackage import org.jetbrains.dokka.plugability.DokkaContext import org.jetbrains.dokka.transformers.documentation.PreMergeDocumentableTransformer class EmptyPackagesFilterTransformer(val context: DokkaContext) : PreMergeDocumentableTransformer { - override fun invoke(modules: List): List = modules.map { original -> - original.let { - EmptyPackagesFilter(original.sourceSets.single()).processModule(it) - } + override fun invoke(modules: List): List { + return modules.mapNotNull { module -> filterModule(module) } } - private class EmptyPackagesFilter( - val sourceSet: DokkaConfiguration.DokkaSourceSet - ) { - fun DPackage.shouldBeSkipped() = sourceSet.skipEmptyPackages && - functions.isEmpty() && - properties.isEmpty() && - classlikes.isEmpty() + private fun filterModule(module: DModule): DModule? { + val nonEmptyPackages = module.packages.filterNot { pkg -> + pkg.sourceSet.skipEmptyPackages && pkg.children.isEmpty() + } - fun processModule(module: DModule) = module.copy( - packages = module.packages.filter { !it.shouldBeSkipped() } - ) + return when { + nonEmptyPackages == module.packages -> module + nonEmptyPackages.isEmpty() -> null + else -> module.copy(packages = nonEmptyPackages) + } } } diff --git a/plugins/base/src/main/kotlin/transformers/documentables/SuppressedDocumentableFilterTransformer.kt b/plugins/base/src/main/kotlin/transformers/documentables/SuppressedDocumentableFilterTransformer.kt index e7a2bf25..f329e9ce 100644 --- a/plugins/base/src/main/kotlin/transformers/documentables/SuppressedDocumentableFilterTransformer.kt +++ b/plugins/base/src/main/kotlin/transformers/documentables/SuppressedDocumentableFilterTransformer.kt @@ -48,21 +48,4 @@ class SuppressedDocumentableFilterTransformer(val context: DokkaContext) : PreMe } } - /** - * A [PreMergeDocumentableTransformer] can safely assume that documentables are not merged and therefore - * only belong to a single source set - */ - private val Documentable.sourceSet: DokkaSourceSet get() = sourceSets.single() - - private val Documentable.perPackageOptions: DokkaConfiguration.PackageOptions? - get() { - val packageName = dri.packageName ?: return null - return sourceSet.perPackageOptions - .sortedByDescending { packageOptions -> packageOptions.prefix.length } - .firstOrNull { packageOptions -> packageName.startsWith(packageOptions.prefix) } - } - - private val T.source: DocumentableSource where T : Documentable, T : WithExpectActual - get() = checkNotNull(sources[sourceSet]) - } diff --git a/plugins/base/src/test/kotlin/filter/EmptyPackagesFilterTest.kt b/plugins/base/src/test/kotlin/filter/EmptyPackagesFilterTest.kt index e5b9e9c2..19bbc164 100644 --- a/plugins/base/src/test/kotlin/filter/EmptyPackagesFilterTest.kt +++ b/plugins/base/src/test/kotlin/filter/EmptyPackagesFilterTest.kt @@ -53,10 +53,12 @@ class EmptyPackagesFilterTest : AbstractCoreTest() { """.trimMargin(), configuration ) { - documentablesFirstTransformationStep = { - Assertions.assertTrue( - it.component2().packages.isEmpty() - ) + documentablesFirstTransformationStep = { modules -> + modules.forEach { module -> + Assertions.assertTrue( + module.packages.isEmpty() + ) + } } } } -- cgit