diff --git a/src/chisels.jl b/src/chisels.jl index 92287fb..21943fd 100644 --- a/src/chisels.jl +++ b/src/chisels.jl @@ -336,6 +336,11 @@ function make_node(node::Node, kids′::Vector{Node}, tags = node.tags) return Node(head(node), span′, kids′, tags) end +function make_node(node::Node, span′::Integer, tags = node.tags) + @assert is_leaf(node) + return Node(head(node), span′, (), tags) +end + # TODO: Remove? first_leaf(node::Node) = nth_leaf(node, 1) diff --git a/src/runestone.jl b/src/runestone.jl index 03f6795..a30b5cb 100644 --- a/src/runestone.jl +++ b/src/runestone.jl @@ -21,8 +21,7 @@ function trim_trailing_whitespace(ctx::Context, node::Node) nb = replace_bytes!(ctx, str′, span(node)) @assert nb != span(node) # Create new node and return it - node′ = Node(head(node), nb) - return node′ + return make_node(node, nb) end function replace_tabs_with_four_spaces(ctx::Context, node::Node) @@ -39,7 +38,7 @@ function replace_tabs_with_four_spaces(ctx::Context, node::Node) tabidx = findnext(x -> x == UInt8('\t'), bytes, tabidx + 4) end nb = replace_bytes!(ctx, bytes, span(node)) - return Node(head(node), nb, tags(node)) + return make_node(node, nb) end function format_hex_literals(ctx::Context, node::Node) diff --git a/test/runtests.jl b/test/runtests.jl index b4b6599..8777279 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -61,6 +61,9 @@ end println(io, " ") str = String(take!(io)) @test format_string(str) == "a = 1\nb = 2\n\n\n" + # Trailing whitespace just before closing indent token + @test format_string("begin\n a = 1 \nend") == "begin\n a = 1\nend" + @test format_string("let\n a = 1 \nend") == "let\n a = 1\nend" end @testset "Hex/oct/bin literal integers" begin