diff options
author | sebastian.sellmair <sebastian.sellmair@jetbrains.com> | 2020-08-11 12:52:43 +0200 |
---|---|---|
committer | Paweł Marks <Kordyjan@users.noreply.github.com> | 2020-08-12 09:38:06 +0200 |
commit | bbe5b00387da8fb8a54f8a8ecd80f2f2dea06830 (patch) | |
tree | 193349416ab229be3aafe6b9801c1afe66b8b116 | |
parent | 6a7d1c9aedfb12f309a410c404a830cb9d3972d3 (diff) | |
download | dokka-bbe5b00387da8fb8a54f8a8ecd80f2f2dea06830.tar.gz dokka-bbe5b00387da8fb8a54f8a8ecd80f2f2dea06830.tar.bz2 dokka-bbe5b00387da8fb8a54f8a8ecd80f2f2dea06830.zip |
Move utility APIs into `PreMergeDocumentationTransformer`
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<DPackage> { override val name = dri.packageName.orEmpty() override val children: List<Documentable> - get() = (properties + functions + classlikes) + get() = (properties + functions + classlikes + typealiases) override fun withNewExtras(newExtras: PropertyContainer<DPackage>) = 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<DModule>): List<DModule> -}
\ 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> 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<DModule>): List<DModule> = modules.map { original -> - original.let { - EmptyPackagesFilter(original.sourceSets.single()).processModule(it) - } + override fun invoke(modules: List<DModule>): List<DModule> { + 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> 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() + ) + } } } } |