Skip to content

Commit

Permalink
Merge pull request #42 from square/zachklipp/multiline-nodes
Browse files Browse the repository at this point in the history
Correctly render nodes whose descriptions are more than one line.
  • Loading branch information
zach-klippenstein authored Aug 17, 2020
2 parents 9fd4688 + 5ee1209 commit 340077e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
26 changes: 18 additions & 8 deletions radiography/src/main/java/radiography/RenderTreeString.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,21 @@ private fun <N> StringBuilder.renderRecursively(
depth: Int,
lastChildMask: Long
) {
appendLinePrefix(depth, lastChildMask)

// Collect the children before actually visiting them. This ensures we know the full list of
// children before we start iterating, which we need in order to be able to render the correct
// line prefix for the last child.
val children = mutableListOf<Pair<Any?, TreeRenderingVisitor<Any?>>>()
val scope = RenderingScope(this@renderRecursively, children)

// Render node into a separate buffer to append a prefix to every line.
val nodeDescription = StringBuilder()
val scope = RenderingScope(nodeDescription, children)
with(visitor) { scope.visitNode(node) }
@Suppress("DEPRECATION")
appendln()

nodeDescription.lineSequence().forEachIndexed { index, line ->
appendLinePrefix(depth, continuePreviousLine = index > 0, lastChildMask = lastChildMask)
@Suppress("DEPRECATION")
appendln(line)
}

if (children.isEmpty()) return

Expand All @@ -87,6 +92,7 @@ private fun <N> StringBuilder.renderRecursively(

private fun StringBuilder.appendLinePrefix(
depth: Int,
continuePreviousLine: Boolean,
lastChildMask: Long
) {
val lastDepth = depth - 1
Expand All @@ -98,20 +104,24 @@ private fun StringBuilder.appendLinePrefix(
}
val lastChild = lastChildMask and (1 shl parentDepth).toLong() != 0L
if (lastChild) {
if (parentDepth == lastDepth) {
if (parentDepth == lastDepth && !continuePreviousLine) {
append('`')
} else {
append(' ')
}
} else {
if (parentDepth == lastDepth) {
if (parentDepth == lastDepth && !continuePreviousLine) {
append('+')
} else {
append('|')
}
}
}
if (depth > 0) {
append("-")
if (continuePreviousLine) {
append(" ")
} else {
append("-")
}
}
}
24 changes: 24 additions & 0 deletions radiography/src/test/java/radiography/RenderTreeStringTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,30 @@ class RenderTreeStringTest {
)
}

@Test fun `renderTreeString handles multiline nodes`() {
val tree = Node(
"root1\nroot2",
Node("1\n1", Node("11\n11")),
Node("2\n2")
)

val rendering = buildString { renderTreeString(tree, NodeVisitor) }

assertThat(rendering).isEqualTo(
"""
|${BLANK}root1
|${BLANK}root2
|$BLANK+-1
|$BLANK| 1
|$BLANK| `-11
|$BLANK| 11
|$BLANK`-2
|$BLANK 2
|
""".trimMargin()
)
}

@Test fun `addChildToVisit with different visitor`() {
val tree = Node(
"root",
Expand Down

0 comments on commit 340077e

Please sign in to comment.