diff options
author | Filip ZybaĆa <fzybala@virtuslab.com> | 2020-06-25 14:53:24 +0200 |
---|---|---|
committer | Sebastian Sellmair <34319766+sellmair@users.noreply.github.com> | 2020-06-25 16:19:29 +0200 |
commit | cf2e842da89f0effa6bdb5eb942b250c94360b5c (patch) | |
tree | dc8721de34d2c1c8a8c17395cc4a19aae08f05af /plugins/base/src/main/kotlin/transformers | |
parent | fff8bb5ffe629bebd2d5e071dd12966a7d89f885 (diff) | |
download | dokka-cf2e842da89f0effa6bdb5eb942b250c94360b5c.tar.gz dokka-cf2e842da89f0effa6bdb5eb942b250c94360b5c.tar.bz2 dokka-cf2e842da89f0effa6bdb5eb942b250c94360b5c.zip |
Refactor for documentable filters.
Diffstat (limited to 'plugins/base/src/main/kotlin/transformers')
-rw-r--r-- | plugins/base/src/main/kotlin/transformers/documentables/DeprecatedDocumentableFilterTransformer.kt | 260 | ||||
-rw-r--r-- | plugins/base/src/main/kotlin/transformers/documentables/DocumentableVisibilityFilterTransformer.kt (renamed from plugins/base/src/main/kotlin/transformers/documentables/DocumentableFilter.kt) | 282 | ||||
-rw-r--r-- | plugins/base/src/main/kotlin/transformers/documentables/EmptyPackagesFilterTransformer.kt | 32 |
3 files changed, 298 insertions, 276 deletions
diff --git a/plugins/base/src/main/kotlin/transformers/documentables/DeprecatedDocumentableFilterTransformer.kt b/plugins/base/src/main/kotlin/transformers/documentables/DeprecatedDocumentableFilterTransformer.kt new file mode 100644 index 00000000..27557107 --- /dev/null +++ b/plugins/base/src/main/kotlin/transformers/documentables/DeprecatedDocumentableFilterTransformer.kt @@ -0,0 +1,260 @@ +package org.jetbrains.dokka.base.transformers.documentables + +import org.jetbrains.dokka.DokkaConfiguration +import org.jetbrains.dokka.model.* +import org.jetbrains.dokka.model.properties.WithExtraProperties +import org.jetbrains.dokka.plugability.DokkaContext +import org.jetbrains.dokka.transformers.documentation.PreMergeDocumentableTransformer + +class DeprecatedDocumentableFilterTransformer(val context: DokkaContext) : PreMergeDocumentableTransformer { + override fun invoke(modules: List<DModule>) = modules.map { original -> + val passOptions = context.configuration.passesConfigurations.first { + original.sourceSets.contains(context.sourceSet(it)) + } + val packageOptions = + passOptions.perPackageOptions + original.let { + DeprecatedDocumentableFilter(passOptions, packageOptions).processModule(it) + } + } + + private class DeprecatedDocumentableFilter( + val globalOptions: DokkaConfiguration.PassConfiguration, + val packageOptions: List<DokkaConfiguration.PackageOptions> + ) { + + fun <T> T.isAllowedInPackage(): Boolean where T : WithExtraProperties<T>, T : Documentable { + val packageName = this.dri.packageName + val condition = packageName != null && packageOptions.firstOrNull { + packageName.startsWith(it.prefix) + }?.skipDeprecated + ?: globalOptions.skipDeprecated + + fun T.isDeprecated() = extra[Annotations]?.let { annotations -> + annotations.content.values.flatten().any { + it.dri.toString() == "kotlin/Deprecated///PointingToDeclaration/" + } + } ?: false + + return !(condition && this.isDeprecated()) + } + + fun processModule(original: DModule) = + filterPackages(original.packages).let { (modified, packages) -> + if (!modified) original + else + DModule( + original.name, + packages = packages, + documentation = original.documentation, + sourceSets = original.sourceSets, + extra = original.extra + ) + } + + + private fun filterPackages(packages: List<DPackage>): Pair<Boolean, List<DPackage>> { + var packagesListChanged = false + val filteredPackages = packages.mapNotNull { pckg -> + var modified = false + val functions = filterFunctions(pckg.functions).let { (listModified, list) -> + modified = modified || listModified + list + } + val properties = filterProperties(pckg.properties).let { (listModified, list) -> + modified = modified || listModified + list + } + val classlikes = filterClasslikes(pckg.classlikes).let { (listModified, list) -> + modified = modified || listModified + list + } + when { + !modified -> pckg + else -> { + packagesListChanged = true + DPackage( + pckg.dri, + functions, + properties, + classlikes, + pckg.typealiases, + pckg.documentation, + pckg.expectPresentInSet, + pckg.sourceSets, + pckg.extra + ) + } + } + } + return Pair(packagesListChanged, filteredPackages) + } + + private fun filterFunctions( + functions: List<DFunction> + ) = functions.filter { it.isAllowedInPackage() }.let { + Pair(it.size != functions.size, it) + } + + private fun filterProperties( + properties: List<DProperty> + ): Pair<Boolean, List<DProperty>> = properties.filter { + it.isAllowedInPackage() + }.let { + Pair(properties.size != it.size, it) + } + + private fun filterEnumEntries(entries: List<DEnumEntry>) = + entries.filter { it.isAllowedInPackage() }.map { entry -> + DEnumEntry( + entry.dri, + entry.name, + entry.documentation, + entry.expectPresentInSet, + filterFunctions(entry.functions).second, + filterProperties(entry.properties).second, + filterClasslikes(entry.classlikes).second, + entry.sourceSets, + entry.extra + ) + } + + private fun filterClasslikes( + classlikeList: List<DClasslike> + ): Pair<Boolean, List<DClasslike>> { + var modified = false + return classlikeList.filter { classlike -> + when (classlike) { + is DClass -> classlike.isAllowedInPackage() + is DInterface -> classlike.isAllowedInPackage() + is DEnum -> classlike.isAllowedInPackage() + is DObject -> classlike.isAllowedInPackage() + is DAnnotation -> classlike.isAllowedInPackage() + } + }.map { classlike -> + fun helper(): DClasslike = when (classlike) { + is DClass -> DClass( + classlike.dri, + classlike.name, + filterFunctions(classlike.constructors).let { + modified = modified || it.first; it.second + }, + filterFunctions(classlike.functions).let { + modified = modified || it.first; it.second + }, + filterProperties(classlike.properties).let { + modified = modified || it.first; it.second + }, + filterClasslikes(classlike.classlikes).let { + modified = modified || it.first; it.second + }, + classlike.sources, + classlike.visibility, + classlike.companion, + classlike.generics, + classlike.supertypes, + classlike.documentation, + classlike.expectPresentInSet, + classlike.modifier, + classlike.sourceSets, + classlike.extra + ) + is DAnnotation -> DAnnotation( + classlike.name, + classlike.dri, + classlike.documentation, + classlike.expectPresentInSet, + classlike.sources, + filterFunctions(classlike.functions).let { + modified = modified || it.first; it.second + }, + filterProperties(classlike.properties).let { + modified = modified || it.first; it.second + }, + filterClasslikes(classlike.classlikes).let { + modified = modified || it.first; it.second + }, + classlike.visibility, + classlike.companion, + filterFunctions(classlike.constructors).let { + modified = modified || it.first; it.second + }, + classlike.generics, + classlike.sourceSets, + classlike.extra + ) + is DEnum -> DEnum( + classlike.dri, + classlike.name, + filterEnumEntries(classlike.entries), + classlike.documentation, + classlike.expectPresentInSet, + classlike.sources, + filterFunctions(classlike.functions).let { + modified = modified || it.first; it.second + }, + filterProperties(classlike.properties).let { + modified = modified || it.first; it.second + }, + filterClasslikes(classlike.classlikes).let { + modified = modified || it.first; it.second + }, + classlike.visibility, + classlike.companion, + filterFunctions(classlike.constructors).let { + modified = modified || it.first; it.second + }, + classlike.supertypes, + classlike.sourceSets, + classlike.extra + ) + is DInterface -> DInterface( + classlike.dri, + classlike.name, + classlike.documentation, + classlike.expectPresentInSet, + classlike.sources, + filterFunctions(classlike.functions).let { + modified = modified || it.first; it.second + }, + filterProperties(classlike.properties).let { + modified = modified || it.first; it.second + }, + filterClasslikes(classlike.classlikes).let { + modified = modified || it.first; it.second + }, + classlike.visibility, + classlike.companion, + classlike.generics, + classlike.supertypes, + classlike.sourceSets, + classlike.extra + ) + is DObject -> DObject( + classlike.name, + classlike.dri, + classlike.documentation, + classlike.expectPresentInSet, + classlike.sources, + filterFunctions(classlike.functions).let { + modified = modified || it.first; it.second + }, + filterProperties(classlike.properties).let { + modified = modified || it.first; it.second + }, + filterClasslikes(classlike.classlikes).let { + modified = modified || it.first; it.second + }, + classlike.visibility, + classlike.supertypes, + classlike.sourceSets, + classlike.extra + ) + } + helper() + }.let { + Pair(it.size != classlikeList.size || modified, it) + } + } + } +}
\ No newline at end of file diff --git a/plugins/base/src/main/kotlin/transformers/documentables/DocumentableFilter.kt b/plugins/base/src/main/kotlin/transformers/documentables/DocumentableVisibilityFilterTransformer.kt index fcead846..0a01680f 100644 --- a/plugins/base/src/main/kotlin/transformers/documentables/DocumentableFilter.kt +++ b/plugins/base/src/main/kotlin/transformers/documentables/DocumentableVisibilityFilterTransformer.kt @@ -2,37 +2,26 @@ package org.jetbrains.dokka.base.transformers.documentables import org.jetbrains.dokka.DokkaConfiguration import org.jetbrains.dokka.model.* -import org.jetbrains.dokka.model.DAnnotation -import org.jetbrains.dokka.model.DEnum -import org.jetbrains.dokka.model.DFunction -import org.jetbrains.dokka.model.properties.WithExtraProperties import org.jetbrains.dokka.plugability.DokkaContext import org.jetbrains.dokka.transformers.documentation.PreMergeDocumentableTransformer -import org.jetbrains.kotlin.utils.addToStdlib.safeAs -internal class DocumentableFilter(val context: DokkaContext) : PreMergeDocumentableTransformer { +class DocumentableVisibilityFilterTransformer(val context: DokkaContext) : PreMergeDocumentableTransformer { - override fun invoke(modules: List<DModule>): List<DModule> = modules.map { original -> - val packageOptions = - context.configuration.passesConfigurations.first { original.sourceSets.contains(context.sourceSet(it)) } - .perPackageOptions + override fun invoke(modules: List<DModule>) = modules.map { original -> val passOptions = context.configuration.passesConfigurations.first { original.sourceSets.contains(context.sourceSet(it)) } + val packageOptions = + passOptions.perPackageOptions original.let { - DeprecationFilter(passOptions,packageOptions).processModule(it) - }.let { - VisibilityFilter(packageOptions, passOptions).processModule(it) - }.let { - EmptyPackagesFilter(passOptions).processModule(it) + DocumentableVisibilityFilter(packageOptions, passOptions).processModule(it) } } - private class VisibilityFilter( + private class DocumentableVisibilityFilter( val packageOptions: List<DokkaConfiguration.PackageOptions>, val globalOptions: DokkaConfiguration.PassConfiguration ) { - fun Visibility.isAllowedInPackage(packageName: String?) = when (this) { is JavaVisibility.Public, is JavaVisibility.Default, @@ -56,7 +45,6 @@ internal class DocumentableFilter(val context: DokkaContext) : PreMergeDocumenta } - private fun filterPackages(packages: List<DPackage>): Pair<Boolean, List<DPackage>> { var packagesListChanged = false val filteredPackages = packages.mapNotNull { @@ -339,263 +327,5 @@ internal class DocumentableFilter(val context: DokkaContext) : PreMergeDocumenta } return Pair(classlikesListChanged, filteredClasslikes) } - - - } - - private class DeprecationFilter( - val globalOptions: DokkaConfiguration.PassConfiguration, - val packageOptions: List<DokkaConfiguration.PackageOptions> - ) { - - fun <T> T.isAllowedInPackage(): Boolean where T : WithExtraProperties<T>, T : Documentable { - val packageName = this.dri.packageName - val condition = packageName != null && packageOptions.firstOrNull { - packageName.startsWith(it.prefix) - }?.skipDeprecated - ?: globalOptions.skipDeprecated - - fun T.isDeprecated() = extra[Annotations]?.let { annotations -> - annotations.content.values.flatten().any { - it.dri.toString() == "kotlin/Deprecated///PointingToDeclaration/" - } - } ?: false - - return !(condition && this.isDeprecated()) - } - - fun processModule(original: DModule) = - filterPackages(original.packages).let { (modified, packages) -> - if (!modified) original - else - DModule( - original.name, - packages = packages, - documentation = original.documentation, - sourceSets = original.sourceSets, - extra = original.extra - ) - } - - - private fun filterPackages(packages: List<DPackage>): Pair<Boolean, List<DPackage>> { - var packagesListChanged = false - val filteredPackages = packages.mapNotNull { - var modified = false - val functions = filterFunctions(it.functions).let { (listModified, list) -> - modified = modified || listModified - list - } - val properties = filterProperties(it.properties).let { (listModified, list) -> - modified = modified || listModified - list - } - val classlikes = filterClasslikes(it.classlikes).let { (listModified, list) -> - modified = modified || listModified - list - } - when { - !modified -> it - else -> { - packagesListChanged = true - DPackage( - it.dri, - functions, - properties, - classlikes, - it.typealiases, - it.documentation, - it.expectPresentInSet, - it.sourceSets, - it.extra - ) - } - } - } - return Pair(packagesListChanged, filteredPackages) - } - - private fun filterFunctions( - functions: List<DFunction> - ) = functions.filter { it.isAllowedInPackage() }.let { - Pair(it.size != functions.size, it) - } - - private fun filterProperties( - properties: List<DProperty> - ): Pair<Boolean, List<DProperty>> = properties.filter { - it.isAllowedInPackage() - }.let { - Pair(properties.size != it.size, it) - } - - private fun filterEnumEntries(entries: List<DEnumEntry>) = - entries.filter { it.isAllowedInPackage() }.map { entry -> - DEnumEntry( - entry.dri, - entry.name, - entry.documentation, - entry.expectPresentInSet, - filterFunctions(entry.functions).second, - filterProperties(entry.properties).second, - filterClasslikes(entry.classlikes).second, - entry.sourceSets, - entry.extra - ) - } - - private fun filterClasslikes( - classlikeList: List<DClasslike> - ): Pair<Boolean, List<DClasslike>> { - var modified = false - return classlikeList.filter { - when (it) { - is DClass -> it.isAllowedInPackage() - is DInterface -> it.isAllowedInPackage() - is DEnum -> it.isAllowedInPackage() - is DObject -> it.isAllowedInPackage() - is DAnnotation -> it.isAllowedInPackage() - } - }.map { - fun helper(): DClasslike = when (it) { - is DClass -> DClass( - it.dri, - it.name, - filterFunctions(it.constructors).let { - modified = modified || it.first; it.second - }, - filterFunctions(it.functions).let { - modified = modified || it.first; it.second - }, - filterProperties(it.properties).let { - modified = modified || it.first; it.second - }, - filterClasslikes(it.classlikes).let { - modified = modified || it.first; it.second - }, - it.sources, - it.visibility, - it.companion, - it.generics, - it.supertypes, - it.documentation, - it.expectPresentInSet, - it.modifier, - it.sourceSets, - it.extra - ) - is DAnnotation -> DAnnotation( - it.name, - it.dri, - it.documentation, - it.expectPresentInSet, - it.sources, - filterFunctions(it.functions).let { - modified = modified || it.first; it.second - }, - filterProperties(it.properties).let { - modified = modified || it.first; it.second - }, - filterClasslikes(it.classlikes).let { - modified = modified || it.first; it.second - }, - it.visibility, - it.companion, - filterFunctions(it.constructors).let { - modified = modified || it.first; it.second - }, - it.generics, - it.sourceSets, - it.extra - ) - is DEnum -> DEnum( - it.dri, - it.name, - filterEnumEntries(it.entries), - it.documentation, - it.expectPresentInSet, - it.sources, - filterFunctions(it.functions).let { - modified = modified || it.first; it.second - }, - filterProperties(it.properties).let { - modified = modified || it.first; it.second - }, - filterClasslikes(it.classlikes).let { - modified = modified || it.first; it.second - }, - it.visibility, - it.companion, - filterFunctions(it.constructors).let { - modified = modified || it.first; it.second - }, - it.supertypes, - it.sourceSets, - it.extra - ) - is DInterface -> DInterface( - it.dri, - it.name, - it.documentation, - it.expectPresentInSet, - it.sources, - filterFunctions(it.functions).let { - modified = modified || it.first; it.second - }, - filterProperties(it.properties).let { - modified = modified || it.first; it.second - }, - filterClasslikes(it.classlikes).let { - modified = modified || it.first; it.second - }, - it.visibility, - it.companion, - it.generics, - it.supertypes, - it.sourceSets, - it.extra - ) - is DObject -> DObject( - it.name, - it.dri, - it.documentation, - it.expectPresentInSet, - it.sources, - filterFunctions(it.functions).let { - modified = modified || it.first; it.second - }, - filterProperties(it.properties).let { - modified = modified || it.first; it.second - }, - filterClasslikes(it.classlikes).let { - modified = modified || it.first; it.second - }, - it.visibility, - it.supertypes, - it.sourceSets, - it.extra - ) - } - helper() - }.let { - Pair(it.size != classlikeList.size || modified, it) - } - } - } - - private class EmptyPackagesFilter( - val passOptions: DokkaConfiguration.PassConfiguration - ) { - fun DPackage.shouldBeSkipped() = passOptions.skipEmptyPackages && - functions.isEmpty() && - properties.isEmpty() && - classlikes.isEmpty() - - fun processModule(module: DModule) = module.copy( - packages = module.packages.mapNotNull { pckg -> - if (pckg.shouldBeSkipped()) null - else pckg - } - ) } }
\ No newline at end of file diff --git a/plugins/base/src/main/kotlin/transformers/documentables/EmptyPackagesFilterTransformer.kt b/plugins/base/src/main/kotlin/transformers/documentables/EmptyPackagesFilterTransformer.kt new file mode 100644 index 00000000..6b29531c --- /dev/null +++ b/plugins/base/src/main/kotlin/transformers/documentables/EmptyPackagesFilterTransformer.kt @@ -0,0 +1,32 @@ +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.model.sourceSet +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 -> + val passOptions = context.configuration.passesConfigurations.first { + original.sourceSets.contains(context.sourceSet(it)) + } + original.let { + EmptyPackagesFilter(passOptions).processModule(it) + } + } + + private class EmptyPackagesFilter( + val passOptions: DokkaConfiguration.PassConfiguration + ) { + fun DPackage.shouldBeSkipped() = passOptions.skipEmptyPackages && + functions.isEmpty() && + properties.isEmpty() && + classlikes.isEmpty() + + fun processModule(module: DModule) = module.copy( + packages = module.packages.filter { !it.shouldBeSkipped() } + ) + } +}
\ No newline at end of file |