Skip to content

Commit cc18f83

Browse files
authored
Abbreviated non-MIME show() for GreenNode (#581)
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 cc18f83

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

src/porcelain/green_node.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,25 @@ 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+
p = position
127+
for n in children(node)
128+
print(io, ' ')
129+
_show_green_node_sexpr(io, n, p)
130+
p += n.span
131+
end
132+
print(io, ')')
133+
end
134+
end
135+
136+
function Base.show(io::IO, node::GreenNode)
137+
_show_green_node_sexpr(io, node, 1)
138+
end
139+
121140
function GreenNode(cursor::GreenTreeCursor)
122141
chead = head(cursor)
123142
T = typeof(chead)

src/porcelain/syntax_tree.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,9 @@ function _show_syntax_node_sexpr(io, node::AbstractSyntaxNode, show_kind)
250250
end
251251
else
252252
print(io, "(", untokenize(head(node)))
253-
first = true
254253
for n in children(node)
255254
print(io, ' ')
256255
_show_syntax_node_sexpr(io, n, show_kind)
257-
first = false
258256
end
259257
print(io, ')')
260258
end

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)