Skip to content

Commit

Permalink
Add a pretty-printer for Runic.Node.
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre committed Jun 1, 2024
1 parent 9440761 commit 5a9f8ad
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/Runic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ function Node(node::JuliaSyntax.GreenNode)
)
end

function Base.show(io::IO, node::Node)
print(io, "Node({head: {kind: ")
show(io, kind(node))
print(io, ", flags: \"$(flags(node))\"}, span: $(span(node))})")
return nothing
end

# Defining these allow using many duck-typed methods in JuliaSyntax directly without having
# to re-package a Node as a GreenNode.
JuliaSyntax.head(node::Node) = head(node)
Expand Down
13 changes: 10 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ using Test:
using JuliaSyntax:
JuliaSyntax

@testset "Chisels" begin
# Type stability of verified_kids
@testset "Node" begin
node = Runic.Node(JuliaSyntax.parseall(JuliaSyntax.GreenNode, "a = 1 + b\n"))
@test typeof(@inferred Runic.verified_kids(node)) === Vector{Runic.Node}

# Pretty-printing
@test sprint(show, node) == "Node({head: {kind: K\"toplevel\", flags: \"0\"}, span: 10})"

# JuliaSyntax duck-typing
for n in (node, Runic.verified_kids(node)...,)
Expand All @@ -19,6 +20,12 @@ using JuliaSyntax:
@test Runic.flags(n) === JuliaSyntax.flags(n) === n.head.flags
@test Runic.span(n) === JuliaSyntax.span(n) === n.span
end
end

@testset "Chisels" begin
# Type stability of verified_kids
node = Runic.Node(JuliaSyntax.parseall(JuliaSyntax.GreenNode, "a = 1 + b\n"))
@test typeof(@inferred Runic.verified_kids(node)) === Vector{Runic.Node}

# replace_bytes!: insert larger
io = IOBuffer(); write(io, "abc"); seek(io, 1)
Expand Down

0 comments on commit 5a9f8ad

Please sign in to comment.