diff options
author | Vadim Mishenev <vad-mishenev@yandex.ru> | 2021-12-23 12:05:59 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-23 12:05:59 +0300 |
commit | 5c98d42ec08ca1413f920e4f5dde28d330e8837a (patch) | |
tree | 9661f86facd24e5bf0655cfb21bdb5a846d9e5d4 /plugins/base/src/main | |
parent | 35f53062b10c2db3e536a9153b22a8a87e3f255d (diff) | |
download | dokka-5c98d42ec08ca1413f920e4f5dde28d330e8837a.tar.gz dokka-5c98d42ec08ca1413f920e4f5dde28d330e8837a.tar.bz2 dokka-5c98d42ec08ca1413f920e4f5dde28d330e8837a.zip |
Allow filtering of property setter (#2220)
* Allow filtering of property setter
* Improve code style
Diffstat (limited to 'plugins/base/src/main')
2 files changed, 59 insertions, 8 deletions
diff --git a/plugins/base/src/main/kotlin/transformers/documentables/DocumentableVisibilityFilterTransformer.kt b/plugins/base/src/main/kotlin/transformers/documentables/DocumentableVisibilityFilterTransformer.kt index 8b3b4304..bd39b040 100644 --- a/plugins/base/src/main/kotlin/transformers/documentables/DocumentableVisibilityFilterTransformer.kt +++ b/plugins/base/src/main/kotlin/transformers/documentables/DocumentableVisibilityFilterTransformer.kt @@ -81,8 +81,12 @@ class DocumentableVisibilityFilterTransformer(val context: DokkaContext) : PreMe return Pair(packagesListChanged, filteredPackages) } + @Suppress("UNUSED_PARAMETER") private fun <T : WithVisibility> alwaysTrue(a: T, p: DokkaSourceSet) = true + @Suppress("UNUSED_PARAMETER") private fun <T : WithVisibility> alwaysFalse(a: T, p: DokkaSourceSet) = false + @Suppress("UNUSED_PARAMETER") + private fun <T> alwaysNoModify(a: T, sourceSets: Set<DokkaSourceSet>) = false to a private fun WithVisibility.visibilityForPlatform(data: DokkaSourceSet): Visibility? = visibility[data] @@ -99,13 +103,18 @@ class DocumentableVisibilityFilterTransformer(val context: DokkaContext) : PreMe private fun <T> List<T>.transform( additionalCondition: (T, DokkaSourceSet) -> Boolean = ::alwaysTrue, alternativeCondition: (T, DokkaSourceSet) -> Boolean = ::alwaysFalse, - recreate: (T, Set<DokkaSourceSet>) -> T + modify: (T, Set<DokkaSourceSet>) -> Pair<Boolean, T> = ::alwaysNoModify, + recreate: (T, Set<DokkaSourceSet>) -> T, ): Pair<Boolean, List<T>> where T : Documentable, T : WithVisibility { var changed = false val values = mapNotNull { t -> val filteredPlatforms = t.filterPlatforms(additionalCondition, alternativeCondition) when (filteredPlatforms.size) { - t.visibility.size -> t + t.visibility.size -> { + val (wasChanged, element) = modify(t, filteredPlatforms) + changed = changed || wasChanged + element + } 0 -> { changed = true null @@ -142,9 +151,35 @@ class DocumentableVisibilityFilterTransformer(val context: DokkaContext) : PreMe private fun filterProperties( properties: List<DProperty>, - additionalCondition: (DProperty, DokkaSourceSet) -> Boolean = ::alwaysTrue - ): Pair<Boolean, List<DProperty>> = - properties.transform(additionalCondition, ::hasVisibleAccessorsForPlatform) { original, filteredPlatforms -> + additionalCondition: (DProperty, DokkaSourceSet) -> Boolean = ::alwaysTrue, + additionalConditionAccessors: (DFunction, DokkaSourceSet) -> Boolean = ::alwaysTrue + ): Pair<Boolean, List<DProperty>> { + + val modifier: (DProperty, Set<DokkaSourceSet>) -> Pair<Boolean, DProperty> = + { original, filteredPlatforms -> + val setter = original.setter?.let { filterFunctions(listOf(it), additionalConditionAccessors) } + val getter = original.getter?.let { filterFunctions(listOf(it), additionalConditionAccessors) } + + val modified = setter?.first == true || getter?.first == true + + val property = + if (modified) + original.copy( + setter = setter?.second?.firstOrNull(), + getter = getter?.second?.firstOrNull() + ) + else original + modified to property + } + + return properties.transform( + additionalCondition, + ::hasVisibleAccessorsForPlatform, + modifier + ) { original, filteredPlatforms -> + val setter = original.setter?.let { filterFunctions(listOf(it), additionalConditionAccessors) } + val getter = original.getter?.let { filterFunctions(listOf(it), additionalConditionAccessors) } + with(original) { copy( documentation = documentation.filtered(filteredPlatforms), @@ -153,9 +188,12 @@ class DocumentableVisibilityFilterTransformer(val context: DokkaContext) : PreMe visibility = visibility.filtered(filteredPlatforms), sourceSets = filteredPlatforms, generics = generics.mapNotNull { it.filter(filteredPlatforms) }, + setter = setter?.second?.firstOrNull(), + getter = getter?.second?.firstOrNull() ) } } + } private fun filterEnumEntries(entries: List<DEnumEntry>, filteredPlatforms: Set<DokkaSourceSet>): Pair<Boolean, List<DEnumEntry>> = entries.foldRight(Pair(false, emptyList())) { entry, acc -> diff --git a/plugins/base/src/main/kotlin/transformers/documentables/SuppressedByConditionDocumentableFilterTransformer.kt b/plugins/base/src/main/kotlin/transformers/documentables/SuppressedByConditionDocumentableFilterTransformer.kt index 0bcb6fd5..918ff1bf 100644 --- a/plugins/base/src/main/kotlin/transformers/documentables/SuppressedByConditionDocumentableFilterTransformer.kt +++ b/plugins/base/src/main/kotlin/transformers/documentables/SuppressedByConditionDocumentableFilterTransformer.kt @@ -27,7 +27,7 @@ abstract class SuppressedByConditionDocumentableFilterTransformer(val context: D val classlikes = dPackage.classlikes.map { processClassLike(it) } val typeAliases = dPackage.typealiases.map { processMember(it) } val functions = dPackage.functions.map { processMember(it) } - val properies = dPackage.properties.map { processMember(it) } + val properies = dPackage.properties.map { processProperty(it) } val wasChanged = (classlikes + typeAliases + functions + properies).any { it.changed } return (dPackage.takeIf { !wasChanged } ?: dPackage.copy( @@ -43,7 +43,7 @@ abstract class SuppressedByConditionDocumentableFilterTransformer(val context: D val functions = classlike.functions.map { processMember(it) } val classlikes = classlike.classlikes.map { processClassLike(it) } - val properties = classlike.properties.map { processMember(it) } + val properties = classlike.properties.map { processProperty(it) } val companion = (classlike as? WithCompanion)?.companion?.let { processClassLike(it) } val wasClasslikeChanged = (functions + classlikes + properties).any { it.changed } || companion?.changed == true @@ -104,7 +104,7 @@ abstract class SuppressedByConditionDocumentableFilterTransformer(val context: D if (shouldBeSuppressed(dEnumEntry)) return DocumentableWithChanges.filteredDocumentable() val functions = dEnumEntry.functions.map { processMember(it) } - val properties = dEnumEntry.properties.map { processMember(it) } + val properties = dEnumEntry.properties.map { processProperty(it) } val classlikes = dEnumEntry.classlikes.map { processClassLike(it) } val wasChanged = (functions + properties + classlikes).any { it.changed } @@ -115,6 +115,19 @@ abstract class SuppressedByConditionDocumentableFilterTransformer(val context: D )).let { DocumentableWithChanges(it, wasChanged) } } + private fun processProperty(dProperty: DProperty): DocumentableWithChanges<DProperty> { + if (shouldBeSuppressed(dProperty)) return DocumentableWithChanges.filteredDocumentable() + + val getter = dProperty.getter?.let { processMember(it) } ?: DocumentableWithChanges(null, false) + val setter = dProperty.setter?.let { processMember(it) } ?: DocumentableWithChanges(null, false) + + val wasChanged = getter.changed || setter.changed + return (dProperty.takeIf { !wasChanged } ?: dProperty.copy( + getter = getter.documentable, + setter = setter.documentable + )).let { DocumentableWithChanges(it, wasChanged) } + } + private fun <T : Documentable> processMember(member: T): DocumentableWithChanges<T> = if (shouldBeSuppressed(member)) DocumentableWithChanges.filteredDocumentable() else DocumentableWithChanges(member, false) |