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
function Context(src_str; debug::Bool = false, verbose::Bool = debug) function Context(src_str; debug::Bool = false, verbose::Bool = debug)
src_io = IOBuffer(src_str) 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_io = IOBuffer()
fmt_tree = nothing fmt_tree = nothing
return Context( return Context(
@ -152,7 +152,7 @@ function format_node_with_children!(ctx::Context, node::JuliaSyntax.GreenNode)
if this_child_changed if this_child_changed
# If the node changed we have to re-write the original bytes for the next # If the node changed we have to re-write the original bytes for the next
# children to the output stream and then reset # 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) nb = write_and_reset(ctx, remaining_bytes)
@assert nb == length(remaining_bytes) @assert nb == length(remaining_bytes)
end end
@ -160,7 +160,7 @@ function format_node_with_children!(ctx::Context, node::JuliaSyntax.GreenNode)
if any_child_changed if any_child_changed
# De-alias the children if not already done # De-alias the children if not already done
if children′ === children 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 end
push!(children′, child′) push!(children′, child′)
end end
@ -170,7 +170,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) span′ = mapreduce(JuliaSyntax.span, +, children′; init = 0)
return JuliaSyntax.GreenNode(head′, span′, children′) return JuliaSyntax.GreenNode(head′, span′, children′)
else else
return nothing return nothing
@ -435,7 +435,7 @@ end
Format a file. 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 # Argument handling
inputfile = normpath(abspath(inputfile)) inputfile = normpath(abspath(inputfile))
outputfile = normpath(abspath(outputfile)) 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
# Whitespace node but replace since not single space # Whitespace node but replace since not single space
any_changes = true any_changes = true
if children′ === children if children′ === children
children′ = children[1:i-1] children′ = children[1:i - 1]
end end
push!(children′, ws) push!(children′, ws)
write_and_reset(ctx, " ") write_and_reset(ctx, " ")
accept_node!(ctx, ws) accept_node!(ctx, ws)
# Re-write bytes for remaining children # 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) write_and_reset(ctx, remaining_bytes)
looking_for_whitespace = false looking_for_whitespace = false
elseif JuliaSyntax.haschildren(child) && elseif JuliaSyntax.haschildren(child) &&
@ -206,19 +206,19 @@ function spaces_around_x(ctx::Context, node::JuliaSyntax.GreenNode, is_x::F) whe
# Replace the whitespace node of the child # Replace the whitespace node of the child
grand_children = JuliaSyntax.children(child)[2:end] grand_children = JuliaSyntax.children(child)[2:end]
pushfirst!(grand_children, ws) 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 @assert span′ == JuliaSyntax.span(child) - JuliaSyntax.span(child_ws) + 1
bytes_to_skip = JuliaSyntax.span(child) - span′ bytes_to_skip = JuliaSyntax.span(child) - span′
@assert bytes_to_skip > 0 @assert bytes_to_skip > 0
remaining_bytes_inclusive = 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) write_and_reset(ctx, remaining_bytes_inclusive)
child′ = JuliaSyntax.GreenNode( child′ = JuliaSyntax.GreenNode(
JuliaSyntax.head(child), span′, grand_children, JuliaSyntax.head(child), span′, grand_children,
) )
any_changes = true any_changes = true
if children′ === children if children′ === children
children′ = children[1:i-1] children′ = children[1:i - 1]
end end
push!(children′, child′) push!(children′, child′)
end end
@ -226,7 +226,7 @@ function spaces_around_x(ctx::Context, node::JuliaSyntax.GreenNode, is_x::F) whe
# Not a whitespace node, insert one # Not a whitespace node, insert one
any_changes = true any_changes = true
if children′ === children if children′ === children
children′ = children[1:i-1] children′ = children[1:i - 1]
end end
push!(children′, ws) push!(children′, ws)
write_and_reset(ctx, " ") write_and_reset(ctx, " ")
@ -234,7 +234,7 @@ function spaces_around_x(ctx::Context, node::JuliaSyntax.GreenNode, is_x::F) whe
# Write and accept the node # Write and accept the node
push!(children′, child) push!(children′, child)
remaining_bytes_inclusive = 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) write_and_reset(ctx, remaining_bytes_inclusive)
accept_node!(ctx, child) accept_node!(ctx, child)
looking_for_whitespace = JuliaSyntax.kind(last_leaf(child)) !== K"Whitespace" 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
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) span′ = mapreduce(JuliaSyntax.span, +, children′; init = 0)
node′ = JuliaSyntax.GreenNode(JuliaSyntax.head(node), span′, children′) node′ = JuliaSyntax.GreenNode(JuliaSyntax.head(node), span′, children′)
return node′ return node′
else else

Loading…
Cancel
Save