aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorPaweł Marks <pmarks@virtuslab.com>2020-08-05 15:37:54 +0200
committerSebastian Sellmair <34319766+sellmair@users.noreply.github.com>2020-08-14 14:16:41 +0200
commitdd6bbdb88b0e4aaefde6598fee605714a3a516d8 (patch)
tree3eecaede6ea45afb38447e197645f418c573c751 /core
parent8af7e4cade0a872ed638acf42d8c474415af290f (diff)
downloaddokka-dd6bbdb88b0e4aaefde6598fee605714a3a516d8.tar.gz
dokka-dd6bbdb88b0e4aaefde6598fee605714a3a516d8.tar.bz2
dokka-dd6bbdb88b0e4aaefde6598fee605714a3a516d8.zip
Add tools for easier content test debugging
Diffstat (limited to 'core')
-rw-r--r--core/src/main/kotlin/model/WithChildren.kt22
1 files changed, 22 insertions, 0 deletions
diff --git a/core/src/main/kotlin/model/WithChildren.kt b/core/src/main/kotlin/model/WithChildren.kt
index 06ff55b0..4aba51c6 100644
--- a/core/src/main/kotlin/model/WithChildren.kt
+++ b/core/src/main/kotlin/model/WithChildren.kt
@@ -65,3 +65,25 @@ fun <T> T.dfs(predicate: (T) -> Boolean): T? where T : WithChildren<T> = if (pre
} else {
children.asSequence().mapNotNull { it.dfs(predicate) }.firstOrNull()
}
+
+fun <T: WithChildren<T>> T.asPrintableTree(
+ nodeNameBuilder: Appendable.(T) -> Unit = { append(it.toString()) }
+): String {
+ fun Appendable.append(element: T, ownPrefix: String, childPrefix: String) {
+ append(ownPrefix)
+ nodeNameBuilder(element)
+ appendLine()
+ element.children.takeIf(Collection<*>::isNotEmpty)?.also { children ->
+ val newOwnPrefix = childPrefix + '\u251c' + '\u2500' + ' '
+ val lastOwnPrefix = childPrefix + '\u2514' + '\u2500' + ' '
+ val newChildPrefix = childPrefix + '\u2502' + ' ' + ' '
+ val lastChildPrefix = childPrefix + ' ' + ' ' + ' '
+ children.forEachIndexed { n, e ->
+ if (n != children.lastIndex) append(e, newOwnPrefix, newChildPrefix)
+ else append(e, lastOwnPrefix, lastChildPrefix)
+ }
+ }
+ }
+
+ return buildString { append(this@asPrintableTree, "", "") }
+} \ No newline at end of file