diff options
author | Paweł Marks <pmarks@virtuslab.com> | 2020-07-17 16:36:09 +0200 |
---|---|---|
committer | Paweł Marks <pmarks@virtuslab.com> | 2020-07-17 16:36:09 +0200 |
commit | 6996b1135f61c7d2cb60b0652c6a2691dda31990 (patch) | |
tree | d568096c25e31c28d14d518a63458b5a7526b896 /core/src/test/kotlin/NodeSelect.kt | |
parent | de56cab76f556e5b4af0b8c8cb08d8b482b86d0a (diff) | |
parent | 1c3530dcbb50c347f80bef694829dbefe89eca77 (diff) | |
download | dokka-6996b1135f61c7d2cb60b0652c6a2691dda31990.tar.gz dokka-6996b1135f61c7d2cb60b0652c6a2691dda31990.tar.bz2 dokka-6996b1135f61c7d2cb60b0652c6a2691dda31990.zip |
Merge branch 'dev-0.11.0'
Diffstat (limited to 'core/src/test/kotlin/NodeSelect.kt')
-rw-r--r-- | core/src/test/kotlin/NodeSelect.kt | 90 |
1 files changed, 0 insertions, 90 deletions
diff --git a/core/src/test/kotlin/NodeSelect.kt b/core/src/test/kotlin/NodeSelect.kt deleted file mode 100644 index fe0394f9..00000000 --- a/core/src/test/kotlin/NodeSelect.kt +++ /dev/null @@ -1,90 +0,0 @@ -package org.jetbrains.dokka.tests - -import org.jetbrains.dokka.DocumentationNode -import org.jetbrains.dokka.NodeKind -import org.jetbrains.dokka.RefKind - -class SelectBuilder { - private val root = ChainFilterNode(SubgraphTraverseFilter(), null) - private var activeNode = root - private val chainEnds = mutableListOf<SelectFilter>() - - fun withName(name: String) = matching { it.name == name } - - fun withKind(kind: NodeKind) = matching{ it.kind == kind } - - fun matching(block: (DocumentationNode) -> Boolean) { - attachFilterAndMakeActive(PredicateFilter(block)) - } - - fun subgraph() { - attachFilterAndMakeActive(SubgraphTraverseFilter()) - } - - fun subgraphOf(kind: RefKind) { - attachFilterAndMakeActive(DirectEdgeFilter(kind)) - } - - private fun attachFilterAndMakeActive(next: SelectFilter) { - activeNode = ChainFilterNode(next, activeNode) - } - - private fun endChain() { - chainEnds += activeNode - } - - fun build(): SelectFilter { - endChain() - return CombineFilterNode(chainEnds) - } -} - -private class ChainFilterNode(val filter: SelectFilter, val previous: SelectFilter?): SelectFilter() { - override fun select(roots: Sequence<DocumentationNode>): Sequence<DocumentationNode> { - return filter.select(previous?.select(roots) ?: roots) - } -} - -private class CombineFilterNode(val previous: List<SelectFilter>): SelectFilter() { - override fun select(roots: Sequence<DocumentationNode>): Sequence<DocumentationNode> { - return previous.asSequence().flatMap { it.select(roots) } - } -} - -abstract class SelectFilter { - abstract fun select(roots: Sequence<DocumentationNode>): Sequence<DocumentationNode> -} - -private class SubgraphTraverseFilter: SelectFilter() { - override fun select(roots: Sequence<DocumentationNode>): Sequence<DocumentationNode> { - val visited = mutableSetOf<DocumentationNode>() - return roots.flatMap { - generateSequence(listOf(it)) { nodes -> - nodes.flatMap { it.allReferences() } - .map { it.to } - .filter { visited.add(it) } - .takeUnless { it.isEmpty() } - } - }.flatten() - } - -} - -private class PredicateFilter(val condition: (DocumentationNode) -> Boolean): SelectFilter() { - override fun select(roots: Sequence<DocumentationNode>): Sequence<DocumentationNode> { - return roots.filter(condition) - } -} - -private class DirectEdgeFilter(val kind: RefKind): SelectFilter() { - override fun select(roots: Sequence<DocumentationNode>): Sequence<DocumentationNode> { - return roots.flatMap { it.references(kind).asSequence() }.map { it.to } - } -} - - -fun selectNodes(root: DocumentationNode, block: SelectBuilder.() -> Unit): List<DocumentationNode> { - val builder = SelectBuilder() - builder.apply(block) - return builder.build().select(sequenceOf(root)).toMutableSet().toList() -}
\ No newline at end of file |