Browse Source

Add `make_node` utility function

This patch adds the `make_node(node, children)` method which creates a
new node with the same head as `node` but with replaced children.
pull/19/head
Fredrik Ekre 2 years ago
parent
commit
b9fbbca5b8
No known key found for this signature in database
GPG Key ID: DE82E6D5E364C0A2
  1. 4
      src/Runic.jl
  2. 9
      src/chisels.jl
  3. 22
      src/runestone.jl

4
src/Runic.jl

@ -106,7 +106,6 @@ function format_node_with_children!(ctx::Context, node::JuliaSyntax.GreenNode)
# The new node parts. `children′` aliases `children` and only copied below if any of the # The new node parts. `children′` aliases `children` and only copied below if any of the
# nodes change ("copy-on-write"). # nodes change ("copy-on-write").
head′ = JuliaSyntax.head(node)
children = verified_children(node) children = verified_children(node)
children′ = children children′ = children
any_child_changed = false any_child_changed = false
@ -161,8 +160,7 @@ function format_node_with_children!(ctx::Context, node::JuliaSyntax.GreenNode)
ctx.next_sibling = next_sibling ctx.next_sibling = next_sibling
# Return a new node if any of the children changed # Return a new node if any of the children changed
if any_child_changed if any_child_changed
span′ = mapreduce(JuliaSyntax.span, +, children′; init = 0) return make_node(node, children′)
return JuliaSyntax.GreenNode(head′, span′, children′)
else else
return nothing return nothing
end end

9
src/chisels.jl

@ -37,6 +37,12 @@ end
# JuliaSyntax extensions # # JuliaSyntax extensions #
########################## ##########################
# Create a new node with the same head but new children
function make_node(node::JuliaSyntax.GreenNode, children′::AbstractVector{<:JuliaSyntax.GreenNode})
span′ = mapreduce(JuliaSyntax.span, +, children′; init = 0)
return JuliaSyntax.GreenNode(JuliaSyntax.head(node), span′, children′)
end
function is_leaf(node::JuliaSyntax.GreenNode) function is_leaf(node::JuliaSyntax.GreenNode)
return !JuliaSyntax.haschildren(node) return !JuliaSyntax.haschildren(node)
end end
@ -69,8 +75,7 @@ function replace_first_leaf(node::JuliaSyntax.GreenNode, child′::JuliaSyntax.G
children′ = copy(verified_children(node)) children′ = copy(verified_children(node))
children′[1] = replace_first_leaf(children′[1], child′) children′[1] = replace_first_leaf(children′[1], child′)
@assert length(children′) > 0 @assert length(children′) > 0
span′ = mapreduce(JuliaSyntax.span, +, children′; init = 0) return make_node(node, children′)
return JuliaSyntax.GreenNode(JuliaSyntax.head(node), span′, children′)
end end
end end

22
src/runestone.jl

@ -260,9 +260,7 @@ function spaces_around_x(ctx::Context, node::JuliaSyntax.GreenNode, is_x::F) whe
seek(ctx.fmt_io, pos) seek(ctx.fmt_io, pos)
if any_changes if any_changes
# Create new node and return it # Create new node and return it
span′ = mapreduce(JuliaSyntax.span, +, children′; init = 0) return make_node(node, children′)
node′ = JuliaSyntax.GreenNode(JuliaSyntax.head(node), span′, children′)
return node′
else else
return nothing return nothing
end end
@ -339,9 +337,8 @@ function no_spaces_around_x(ctx::Context, node::JuliaSyntax.GreenNode, is_x::F)
seek(ctx.fmt_io, pos) seek(ctx.fmt_io, pos)
if any_changes if any_changes
# Create new node and return it # Create new node and return it
span′ = mapreduce(JuliaSyntax.span, +, children′; init = 0) node′ = make_node(node, children′)
@assert span′ < JuliaSyntax.span(node) @assert JuliaSyntax.span(node′) < JuliaSyntax.span(node)
node′ = JuliaSyntax.GreenNode(JuliaSyntax.head(node), span′, children′)
return node′ return node′
else else
return nothing return nothing
@ -397,8 +394,7 @@ function replace_with_in(ctx::Context, node::JuliaSyntax.GreenNode)
for i in (in_index + 1):length(children′) for i in (in_index + 1):length(children′)
accept_node!(ctx, children′[i]) accept_node!(ctx, children′[i])
end end
node′ = JuliaSyntax.GreenNode(JuliaSyntax.head(node), mapreduce(JuliaSyntax.span, +, children′; init = 0), children′) return make_node(node, children′)
return node′
end end
function replace_with_in_cartesian(ctx::Context, node::JuliaSyntax.GreenNode) function replace_with_in_cartesian(ctx::Context, node::JuliaSyntax.GreenNode)
@ -422,13 +418,10 @@ function replace_with_in_cartesian(ctx::Context, node::JuliaSyntax.GreenNode)
accept_node!(ctx, child) accept_node!(ctx, child)
end end
end end
span′ = mapreduce(JuliaSyntax.span, +, children′; init = 0)
if children === children′ if children === children′
# No changes
return nothing return nothing
end end
node′ = JuliaSyntax.GreenNode(JuliaSyntax.head(node), span′, children′) return make_node(node, children′)
return node′
end end
# replace `=` and `∈` with `in` in for-loops # replace `=` and `∈` with `in` in for-loops
@ -472,9 +465,8 @@ function for_loop_use_in(ctx::Context, node::JuliaSyntax.GreenNode)
accept_node!(ctx, children′[i]) accept_node!(ctx, children′[i])
end end
# Construct the full node and return # Construct the full node and return
span′ = mapreduce(JuliaSyntax.span, +, children′; init = 0) node′ = make_node(node, children′)
node′ = JuliaSyntax.GreenNode(JuliaSyntax.head(node), span′, children′) @assert position(ctx.fmt_io) == pos + JuliaSyntax.span(node′)
@assert position(ctx.fmt_io) == pos + span′
seek(ctx.fmt_io, pos) # reset seek(ctx.fmt_io, pos) # reset
return node′ return node′
end end

Loading…
Cancel
Save