From a16e101fad10d024a84aa36069ec6032fcbaf3e6 Mon Sep 17 00:00:00 2001 From: "sebastian.sellmair" Date: Tue, 30 Jun 2020 08:03:38 +0200 Subject: Implement `AbstractJavadocTemplateMapTest` API --- core/src/main/kotlin/model/Documentable.kt | 14 +---- core/src/main/kotlin/model/WithChildren.kt | 64 ++++++++++++++++++++++ core/src/main/kotlin/model/doc/DocTag.kt | 4 +- .../src/main/kotlin/model/doc/DocumentationNode.kt | 2 +- core/src/main/kotlin/model/doc/TagWrapper.kt | 4 +- core/src/main/kotlin/model/documentableUtils.kt | 10 ---- 6 files changed, 71 insertions(+), 27 deletions(-) create mode 100644 core/src/main/kotlin/model/WithChildren.kt (limited to 'core/src/main/kotlin/model') diff --git a/core/src/main/kotlin/model/Documentable.kt b/core/src/main/kotlin/model/Documentable.kt index 35278302..21660f86 100644 --- a/core/src/main/kotlin/model/Documentable.kt +++ b/core/src/main/kotlin/model/Documentable.kt @@ -7,10 +7,9 @@ import org.jetbrains.dokka.model.properties.PropertyContainer import org.jetbrains.dokka.model.properties.WithExtraProperties -abstract class Documentable { +abstract class Documentable : WithChildren { abstract val name: String? abstract val dri: DRI - abstract val children: List abstract val documentation: SourceSetDependent abstract val sourceSets: Set abstract val expectPresentInSet: DokkaSourceSet? @@ -359,7 +358,7 @@ data class PrimitiveJavaType(val name: String) : Bound() object Void : Bound() object JavaObject : Bound() object Dynamic : Bound() -data class UnresolvedBound(val name: String): Bound() +data class UnresolvedBound(val name: String) : Bound() enum class FunctionModifiers { NONE, FUNCTION, EXTENSION @@ -376,15 +375,6 @@ fun Documentable.dfs(predicate: (Documentable) -> Boolean): Documentable? = this.children.asSequence().mapNotNull { it.dfs(predicate) }.firstOrNull() } -fun Documentable.withDescendants(): Sequence { - return sequence { - yield(this@withDescendants) - children.forEach { child -> - yieldAll(child.withDescendants()) - } - } -} - sealed class Visibility(val name: String) sealed class KotlinVisibility(name: String) : Visibility(name) { object Public : KotlinVisibility("public") diff --git a/core/src/main/kotlin/model/WithChildren.kt b/core/src/main/kotlin/model/WithChildren.kt new file mode 100644 index 00000000..589bcd2a --- /dev/null +++ b/core/src/main/kotlin/model/WithChildren.kt @@ -0,0 +1,64 @@ +package org.jetbrains.dokka.model + +interface WithChildren { + val children: List +} + +inline fun WithChildren<*>.firstChildOfTypeOrNull(): T? = + children.filterIsInstance().firstOrNull() + +inline fun WithChildren<*>.firstChildOfTypeOrNull(predicate: (T) -> Boolean): T? = + children.filterIsInstance().firstOrNull(predicate) + +inline fun WithChildren<*>.firstChildOfType(): T = + children.filterIsInstance().first() + +inline fun WithChildren<*>.firstChildOfType(predicate: (T) -> Boolean): T = + children.filterIsInstance().first(predicate) + +inline fun WithChildren>.firstMemberOfType(): T where T : WithChildren<*> { + return withDescendants().filterIsInstance().first() +} + +inline fun WithChildren>.firstMemberOfTypeOrNull(): T? where T : WithChildren<*> { + return withDescendants().filterIsInstance().firstOrNull() +} + +fun T.withDescendants(): Sequence where T : WithChildren { + return sequence { + yield(this@withDescendants) + children.forEach { child -> + yieldAll(child.withDescendants()) + } + } +} + +@JvmName("withDescendantsProjection") +fun WithChildren<*>.withDescendants(): Sequence { + return sequence { + yield(this@withDescendants) + children.forEach { child -> + if (child is WithChildren<*>) { + yieldAll(child.withDescendants()) + } + } + } +} + +@JvmName("withDescendantsAny") +fun WithChildren.withDescendants(): Sequence { + return sequence { + yield(this@withDescendants) + children.forEach { child -> + if (child is WithChildren<*>) { + yieldAll(child.withDescendants().filterNotNull()) + } + } + } +} + +fun T.dfs(predicate: (T) -> Boolean): T? where T : WithChildren = if (predicate(this)) { + this +} else { + children.asSequence().mapNotNull { it.dfs(predicate) }.firstOrNull() +} diff --git a/core/src/main/kotlin/model/doc/DocTag.kt b/core/src/main/kotlin/model/doc/DocTag.kt index 902dedec..42836d71 100644 --- a/core/src/main/kotlin/model/doc/DocTag.kt +++ b/core/src/main/kotlin/model/doc/DocTag.kt @@ -6,7 +6,7 @@ import org.jetbrains.dokka.model.WithChildren sealed class DocTag( override val children: List, val params: Map -) : WithChildren { +) : WithChildren { override fun equals(other: Any?): Boolean = ( other != null && @@ -90,4 +90,4 @@ class DocumentationLink(val dri: DRI, children: List = emptyList(), para override fun hashCode(): Int = super.hashCode() + dri.hashCode() } object HorizontalRule : DocTag(emptyList(), emptyMap()) -class CustomDocTag(children: List = emptyList(), params: Map = emptyMap()) : DocTag(children, params) \ No newline at end of file +class CustomDocTag(children: List = emptyList(), params: Map = emptyMap()) : DocTag(children, params) diff --git a/core/src/main/kotlin/model/doc/DocumentationNode.kt b/core/src/main/kotlin/model/doc/DocumentationNode.kt index cfac210a..6eb26a6a 100644 --- a/core/src/main/kotlin/model/doc/DocumentationNode.kt +++ b/core/src/main/kotlin/model/doc/DocumentationNode.kt @@ -2,4 +2,4 @@ package org.jetbrains.dokka.model.doc import org.jetbrains.dokka.model.WithChildren -data class DocumentationNode(override val children: List): WithChildren \ No newline at end of file +data class DocumentationNode(override val children: List): WithChildren diff --git a/core/src/main/kotlin/model/doc/TagWrapper.kt b/core/src/main/kotlin/model/doc/TagWrapper.kt index 458203b0..095f3eaf 100644 --- a/core/src/main/kotlin/model/doc/TagWrapper.kt +++ b/core/src/main/kotlin/model/doc/TagWrapper.kt @@ -3,7 +3,7 @@ package org.jetbrains.dokka.model.doc import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.model.WithChildren -sealed class TagWrapper(val root: DocTag) : WithChildren { +sealed class TagWrapper(val root: DocTag) : WithChildren { override val children: List get() = root.children @@ -36,4 +36,4 @@ class Sample(root: DocTag, name: String) : NamedTagWrapper(root, name) class Deprecated(root: DocTag) : TagWrapper(root) class Property(root: DocTag, name: String) : NamedTagWrapper(root, name) class Suppress(root: DocTag) : TagWrapper(root) -class CustomTagWrapper(root: DocTag, name: String) : NamedTagWrapper(root, name) \ No newline at end of file +class CustomTagWrapper(root: DocTag, name: String) : NamedTagWrapper(root, name) diff --git a/core/src/main/kotlin/model/documentableUtils.kt b/core/src/main/kotlin/model/documentableUtils.kt index f49c0967..287cf313 100644 --- a/core/src/main/kotlin/model/documentableUtils.kt +++ b/core/src/main/kotlin/model/documentableUtils.kt @@ -20,13 +20,3 @@ fun DTypeParameter.filter(filteredSet: Set) = extra ) } - -interface WithChildren { - val children: List<*> -} - -inline fun WithChildren.firstChildOfType() = - children.filterIsInstance().firstOrNull() - -inline fun WithChildren.firstChildOfType(predicate: (T) -> Boolean) = - children.filterIsInstance().firstOrNull(predicate) \ No newline at end of file -- cgit