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 +++++++++++++++++++++---- 1 file changed, 38 insertions(+), 6 deletions(-) (limited to 'core/src/main/kotlin/Model') 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 -- cgit