From 562cce04558c0f6b9e266a8e39f0254d6594e84e Mon Sep 17 00:00:00 2001 From: Kamil Doległo Date: Tue, 19 Nov 2019 19:53:01 +0100 Subject: Add variable content to PageNodes and extras to DocumentationNodes I'm just sorry --- core/src/main/kotlin/Model/DocumentationNode.kt | 44 +++++++++++++++++++++---- core/src/main/kotlin/pages/ContentNodes.kt | 12 ++++++- core/src/main/kotlin/pages/PageNodes.kt | 10 +++--- 3 files changed, 54 insertions(+), 12 deletions(-) (limited to 'core') diff --git a/core/src/main/kotlin/Model/DocumentationNode.kt b/core/src/main/kotlin/Model/DocumentationNode.kt index fc81db6d..55ae5902 100644 --- a/core/src/main/kotlin/Model/DocumentationNode.kt +++ b/core/src/main/kotlin/Model/DocumentationNode.kt @@ -8,13 +8,15 @@ import org.jetbrains.kotlin.kdoc.psi.impl.KDocTag class Module(val packages: List) : DocumentationNode() { override val dri: DRI = DRI.topLevel override val children: List = packages + override val extra: MutableSet = mutableSetOf() } class Package( override val dri: DRI, override val functions: List, override val properties: List, - override val classes: List + override val classes: List, + override val extra: MutableSet = mutableSetOf() ) : ScopeNode() { val name = dri.packageName.orEmpty() } @@ -27,7 +29,8 @@ class Class( override val properties: List, override val classes: List, override val expectDescriptor: Descriptor?, - override val actualDescriptors: List> + override val actualDescriptors: List>, + override val extra: MutableSet = mutableSetOf() ) : ScopeNode() class Function( @@ -36,7 +39,8 @@ class Function( override val receiver: Parameter?, val parameters: List, override val expectDescriptor: Descriptor?, - override val actualDescriptors: List> + override val actualDescriptors: List>, + override val extra: MutableSet = mutableSetOf() ) : CallableNode() { override val children: List get() = listOfNotNull(receiver) + parameters @@ -47,7 +51,8 @@ class Property( val name: String, override val receiver: Parameter?, override val expectDescriptor: Descriptor?, - override val actualDescriptors: List> + override val actualDescriptors: List>, + override val extra: MutableSet = mutableSetOf() ) : CallableNode() { override val children: List get() = listOfNotNull(receiver) @@ -57,7 +62,8 @@ class Property( class Parameter( override val dri: DRI, val name: String?, - override val actualDescriptors: List> + override val actualDescriptors: List>, + override val extra: MutableSet = mutableSetOf() ) : DocumentationNode() { override val children: List> get() = emptyList() @@ -103,6 +109,8 @@ abstract class DocumentationNode { val briefDocstring: String get() = descriptors.firstOrNull()?.docTag?.getContent().orEmpty().shorten(40) + + open val extra: MutableSet = mutableSetOf() } abstract class ScopeNode : DocumentationNode() { @@ -120,4 +128,28 @@ abstract class CallableNode : DocumentationNode() private fun String.shorten(maxLength: Int) = lineSequence().first().let { if (it.length != length || it.length > maxLength) it.take(maxLength - 3) + "..." else it -} \ No newline at end of file +} + +fun DocumentationNode<*>.walk(process: DocumentationNode<*>.() -> Unit) { + this.process() + this.children.forEach { it.process() } +} + +fun DocumentationNode<*>.dfs(predicate: (DocumentationNode<*>) -> Boolean): DocumentationNode<*>? = + if (predicate(this)) { + this + } else { + this.children.asSequence().mapNotNull { it.dfs(predicate) }.firstOrNull() + } + +fun DocumentationNode<*>.findAll(predicate: (DocumentationNode<*>) -> Boolean): Set> { + val found = mutableSetOf>() + if (predicate(this)) { + found.add(this) + } else { + this.children.asSequence().mapNotNull { it.findAll(predicate) }.forEach { found.addAll(it) } + } + return found +} + +interface Extra \ No newline at end of file diff --git a/core/src/main/kotlin/pages/ContentNodes.kt b/core/src/main/kotlin/pages/ContentNodes.kt index 40b5e114..dcc511cc 100644 --- a/core/src/main/kotlin/pages/ContentNodes.kt +++ b/core/src/main/kotlin/pages/ContentNodes.kt @@ -132,4 +132,14 @@ interface HTMLMetadata: Extra { } data class HTMLSimpleAttr(override val key: String, override val value: String): HTMLMetadata -data class HTMLTableMetadata(val item: String, override val key: String, override val value: String): HTMLMetadata \ No newline at end of file +data class HTMLTableMetadata(val item: String, override val key: String, override val value: String): HTMLMetadata + +fun ContentNode.dfs(predicate: (ContentNode) -> Boolean): ContentNode? = if (predicate(this)) { + this +} else { + if (this is ContentComposite) { + this.children.asSequence().mapNotNull { it.dfs(predicate) }.firstOrNull() + } else { + null + } +} diff --git a/core/src/main/kotlin/pages/PageNodes.kt b/core/src/main/kotlin/pages/PageNodes.kt index 810cd776..6c56d5fe 100644 --- a/core/src/main/kotlin/pages/PageNodes.kt +++ b/core/src/main/kotlin/pages/PageNodes.kt @@ -6,7 +6,7 @@ import org.jetbrains.dokka.links.DRI interface PageNode { val name: String - val content: ContentNode + var content: ContentNode val parent: PageNode? val dri: DRI val documentationNode: DocumentationNode<*>? @@ -42,7 +42,7 @@ abstract class BasicPageNode(children: List) : PageNode { class ModulePageNode( override val name: String, - override val content: ContentNode, + override var content: ContentNode, override val documentationNode: DocumentationNode<*>?, children: List, override val embeddedResources: List = listOf() @@ -62,7 +62,7 @@ class ModulePageNode( class PackagePageNode( override val name: String, - override val content: ContentNode, + override var content: ContentNode, override val dri: DRI, override val documentationNode: DocumentationNode<*>?, children: List, @@ -81,7 +81,7 @@ class PackagePageNode( class ClassPageNode( override val name: String, - override val content: ContentNode, + override var content: ContentNode, override val dri: DRI, override val documentationNode: DocumentationNode<*>?, children: List, @@ -100,7 +100,7 @@ class ClassPageNode( class MemberPageNode( override val name: String, - override val content: ContentNode, + override var content: ContentNode, override val dri: DRI, override val documentationNode: DocumentationNode<*>?, children: List = emptyList(), -- cgit