Skip to content

Commit

Permalink
Format files with Runic :^)
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre committed May 26, 2024
1 parent 0098719 commit d076ca8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
10 changes: 5 additions & 5 deletions src/Runic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ end

function Context(src_str; debug::Bool = false, verbose::Bool = debug)
src_io = IOBuffer(src_str)
src_tree = JuliaSyntax.parseall(JuliaSyntax.GreenNode, src_str; ignore_warnings=true)
src_tree = JuliaSyntax.parseall(JuliaSyntax.GreenNode, src_str; ignore_warnings = true)
fmt_io = IOBuffer()
fmt_tree = nothing
return Context(
Expand Down Expand Up @@ -152,15 +152,15 @@ function format_node_with_children!(ctx::Context, node::JuliaSyntax.GreenNode)
if this_child_changed
# If the node changed we have to re-write the original bytes for the next
# children to the output stream and then reset
remaining_bytes = @view original_bytes[(span_sum+1):end]
remaining_bytes = @view original_bytes[(span_sum + 1):end]
nb = write_and_reset(ctx, remaining_bytes)
@assert nb == length(remaining_bytes)
end
any_child_changed |= this_child_changed
if any_child_changed
# De-alias the children if not already done
if children′ === children
children′ = eltype(children)[children[j] for j in 1:(i-1)]
children′ = eltype(children)[children[j] for j in 1:(i - 1)]
end
push!(children′, child′)
end
Expand All @@ -170,7 +170,7 @@ function format_node_with_children!(ctx::Context, node::JuliaSyntax.GreenNode)
ctx.next_sibling = next_sibling
# Return a new node if any of the children changed
if any_child_changed
span′ = mapreduce(JuliaSyntax.span, +, children′; init=0)
span′ = mapreduce(JuliaSyntax.span, +, children′; init = 0)
return JuliaSyntax.GreenNode(head′, span′, children′)
else
return nothing
Expand Down Expand Up @@ -435,7 +435,7 @@ end
Format a file.
"""
function format_file(inputfile::AbstractString, outputfile::AbstractString = inputfile; inplace::Bool=false)
function format_file(inputfile::AbstractString, outputfile::AbstractString = inputfile; inplace::Bool = false)
# Argument handling
inputfile = normpath(abspath(inputfile))
outputfile = normpath(abspath(outputfile))
Expand Down
16 changes: 8 additions & 8 deletions src/runestone.jl
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,13 @@ function spaces_around_x(ctx::Context, node::JuliaSyntax.GreenNode, is_x::F) whe
# Whitespace node but replace since not single space
any_changes = true
if children′ === children
children′ = children[1:i-1]
children′ = children[1:i - 1]
end
push!(children′, ws)
write_and_reset(ctx, " ")
accept_node!(ctx, ws)
# Re-write bytes for remaining children
remaining_bytes = @view original_bytes[(span_sum+1):end]
remaining_bytes = @view original_bytes[(span_sum + 1):end]
write_and_reset(ctx, remaining_bytes)
looking_for_whitespace = false
elseif JuliaSyntax.haschildren(child) &&
Expand All @@ -206,35 +206,35 @@ function spaces_around_x(ctx::Context, node::JuliaSyntax.GreenNode, is_x::F) whe
# Replace the whitespace node of the child
grand_children = JuliaSyntax.children(child)[2:end]
pushfirst!(grand_children, ws)
span′ = mapreduce(JuliaSyntax.span, +, grand_children; init=0)
span′ = mapreduce(JuliaSyntax.span, +, grand_children; init = 0)
@assert span′ == JuliaSyntax.span(child) - JuliaSyntax.span(child_ws) + 1
bytes_to_skip = JuliaSyntax.span(child) - span′
@assert bytes_to_skip > 0
remaining_bytes_inclusive =
@view original_bytes[(span_sum+1+bytes_to_skip-JuliaSyntax.span(child)):end]
@view original_bytes[(span_sum + 1 + bytes_to_skip - JuliaSyntax.span(child)):end]
write_and_reset(ctx, remaining_bytes_inclusive)
child′ = JuliaSyntax.GreenNode(
JuliaSyntax.head(child), span′, grand_children,
)
any_changes = true
if children′ === children
children′ = children[1:i-1]
children′ = children[1:i - 1]
end
push!(children′, child′)
end
else
# Not a whitespace node, insert one
any_changes = true
if children′ === children
children′ = children[1:i-1]
children′ = children[1:i - 1]
end
push!(children′, ws)
write_and_reset(ctx, " ")
accept_node!(ctx, ws)
# Write and accept the node
push!(children′, child)
remaining_bytes_inclusive =
@view original_bytes[(span_sum+1-JuliaSyntax.span(child)):end]
@view original_bytes[(span_sum + 1 - JuliaSyntax.span(child)):end]
write_and_reset(ctx, remaining_bytes_inclusive)
accept_node!(ctx, child)
looking_for_whitespace = JuliaSyntax.kind(last_leaf(child)) !== K"Whitespace"
Expand All @@ -258,7 +258,7 @@ function spaces_around_x(ctx::Context, node::JuliaSyntax.GreenNode, is_x::F) whe
seek(ctx.fmt_io, pos)
if any_changes
# Create new node and return it
span′ = mapreduce(JuliaSyntax.span, +, children′; init=0)
span′ = mapreduce(JuliaSyntax.span, +, children′; init = 0)
node′ = JuliaSyntax.GreenNode(JuliaSyntax.head(node), span′, children′)
return node′
else
Expand Down

0 comments on commit d076ca8

Please sign in to comment.