Skip to content

Commit 6c5e113

Browse files
committed
Abbreviated non-MIME show() for GreenNode
Non-MIME `show()` for `GreenNode` was using the default implementation which is extremely verbose. Here we print it as an S-expression instead. I've made a choice to show absolute position within the parent node rather than node span so that it directly relates to indices in the string it was derived from.
1 parent 41fdd65 commit 6c5e113

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/porcelain/green_node.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,27 @@ function Base.show(io::IO, ::MIME"text/plain", node::GreenNode, str::AbstractStr
118118
_show_green_node(io, node, "", 1, str, show_trivia)
119119
end
120120

121+
function _show_green_node_sexpr(io, node::GreenNode, position)
122+
if is_leaf(node)
123+
print(io, position, "-", position+node.span-1, "::", untokenize(head(node); unique=false))
124+
else
125+
print(io, "(", untokenize(head(node); unique=false))
126+
first = true
127+
p = position
128+
for n in children(node)
129+
print(io, ' ')
130+
_show_green_node_sexpr(io, n, p)
131+
p += n.span
132+
first = false
133+
end
134+
print(io, ')')
135+
end
136+
end
137+
138+
function Base.show(io::IO, node::GreenNode)
139+
_show_green_node_sexpr(io, node, 1)
140+
end
141+
121142
function GreenNode(cursor::GreenTreeCursor)
122143
chead = head(cursor)
123144
T = typeof(chead)

test/green_node.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,7 @@
6262
10:10 │ Identifier ✔ "z"
6363
11:11 │ ) ")"
6464
"""
65+
66+
@test sprint(show, parsestmt(GreenNode, "a + bb - f(ccc)")) ==
67+
"(call-i (call-i 1-1::Identifier 2-2::Whitespace-t 3-3::Identifier 4-4::Whitespace-t 5-6::Identifier) 7-7::Whitespace-t 8-8::Identifier 9-9::Whitespace-t (call 10-10::Identifier 11-11::(-t 12-14::Identifier 15-15::)-t))"
6568
end

0 commit comments

Comments
 (0)