Browse Source

Format files with Runic :^)

pull/19/head
Fredrik Ekre 2 years ago
parent
commit
d076ca8546
No known key found for this signature in database
GPG Key ID: DE82E6D5E364C0A2
  1. 10
      src/Runic.jl
  2. 16
      src/runestone.jl

10
src/Runic.jl

@ -43,7 +43,7 @@ end @@ -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(
@ -152,7 +152,7 @@ function format_node_with_children!(ctx::Context, node::JuliaSyntax.GreenNode) @@ -152,7 +152,7 @@ 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
@ -160,7 +160,7 @@ function format_node_with_children!(ctx::Context, node::JuliaSyntax.GreenNode) @@ -160,7 +160,7 @@ function format_node_with_children!(ctx::Context, node::JuliaSyntax.GreenNode)
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
@ -170,7 +170,7 @@ function format_node_with_children!(ctx::Context, node::JuliaSyntax.GreenNode) @@ -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
@ -435,7 +435,7 @@ end @@ -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))

16
src/runestone.jl

@ -184,13 +184,13 @@ function spaces_around_x(ctx::Context, node::JuliaSyntax.GreenNode, is_x::F) whe @@ -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) &&
@ -206,19 +206,19 @@ function spaces_around_x(ctx::Context, node::JuliaSyntax.GreenNode, is_x::F) whe @@ -206,19 +206,19 @@ 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
@ -226,7 +226,7 @@ function spaces_around_x(ctx::Context, node::JuliaSyntax.GreenNode, is_x::F) whe @@ -226,7 +226,7 @@ function spaces_around_x(ctx::Context, node::JuliaSyntax.GreenNode, is_x::F) whe
# 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, " ")
@ -234,7 +234,7 @@ function spaces_around_x(ctx::Context, node::JuliaSyntax.GreenNode, is_x::F) whe @@ -234,7 +234,7 @@ function spaces_around_x(ctx::Context, node::JuliaSyntax.GreenNode, is_x::F) whe
# 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"
@ -258,7 +258,7 @@ function spaces_around_x(ctx::Context, node::JuliaSyntax.GreenNode, is_x::F) whe @@ -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

Loading…
Cancel
Save