Browse Source

Rename `node_bytes` to `read_bytes`

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

7
src/Runic.jl

@ -69,10 +69,11 @@ function next_sibling_kind(ctx::Context)::Union{JuliaSyntax.Kind, Nothing}
end end
# Read the bytes of the current node from the output io # Read the bytes of the current node from the output io
function node_bytes(ctx, node) function read_bytes(ctx, node)
pos = mark(ctx.fmt_io) pos = position(ctx.fmt_io)
bytes = read(ctx.fmt_io, JuliaSyntax.span(node)) bytes = read(ctx.fmt_io, JuliaSyntax.span(node))
reset(ctx.fmt_io) @assert length(bytes) == JuliaSyntax.span(node)
seek(ctx.fmt_io, pos)
@assert position(ctx.fmt_io) == pos @assert position(ctx.fmt_io) == pos
return bytes return bytes
end end

10
src/runestone.jl

@ -5,7 +5,7 @@
function trim_trailing_whitespace(ctx::Context, node::JuliaSyntax.GreenNode) function trim_trailing_whitespace(ctx::Context, node::JuliaSyntax.GreenNode)
JuliaSyntax.kind(node) === K"NewlineWs" || return nothing JuliaSyntax.kind(node) === K"NewlineWs" || return nothing
@assert !JuliaSyntax.haschildren(node) @assert !JuliaSyntax.haschildren(node)
str = String(node_bytes(ctx, node)) str = String(read_bytes(ctx, node))
str′ = replace(str, r"\h*(\r\n|\r|\n)" => '\n') str′ = replace(str, r"\h*(\r\n|\r|\n)" => '\n')
# If the next sibling is also a NewlineWs we can trim trailing # If the next sibling is also a NewlineWs we can trim trailing
# whitespace from this node too # whitespace from this node too
@ -39,7 +39,7 @@ function format_hex_literals(ctx::Context, node::JuliaSyntax.GreenNode)
end end
# Insert leading zeros # Insert leading zeros
i = findfirst(x -> x > span, target_spans)::Int i = findfirst(x -> x > span, target_spans)::Int
bytes = node_bytes(ctx, node) bytes = read_bytes(ctx, node)
while length(bytes) < target_spans[i] while length(bytes) < target_spans[i]
insert!(bytes, 3, '0') insert!(bytes, 3, '0')
end end
@ -57,7 +57,7 @@ function format_oct_literals(ctx::Context, node::JuliaSyntax.GreenNode)
span = JuliaSyntax.span(node) span = JuliaSyntax.span(node)
@assert span > 2 # 0o prefix + something more @assert span > 2 # 0o prefix + something more
# Padding depends on the value of the literal... # Padding depends on the value of the literal...
str = String(node_bytes(ctx, node)) str = String(read_bytes(ctx, node))
n = tryparse(UInt128, str) n = tryparse(UInt128, str)
if n === nothing if n === nothing
# Do nothing: BigInt oct literal # Do nothing: BigInt oct literal
@ -77,7 +77,7 @@ function format_oct_literals(ctx::Context, node::JuliaSyntax.GreenNode)
return nothing return nothing
end end
# Insert leading zeros # Insert leading zeros
bytes = node_bytes(ctx, node) bytes = read_bytes(ctx, node)
while length(bytes) < target_span while length(bytes) < target_span
insert!(bytes, 3, '0') insert!(bytes, 3, '0')
end end
@ -92,7 +92,7 @@ function format_float_literals(ctx::Context, node::JuliaSyntax.GreenNode)
JuliaSyntax.kind(node) in KSet"Float Float32" || return nothing JuliaSyntax.kind(node) in KSet"Float Float32" || return nothing
@assert JuliaSyntax.flags(node) == 0 @assert JuliaSyntax.flags(node) == 0
@assert !JuliaSyntax.haschildren(node) @assert !JuliaSyntax.haschildren(node)
str = String(node_bytes(ctx, node)) str = String(read_bytes(ctx, node))
# Check and shortcut the happy path first # Check and shortcut the happy path first
r = r""" r = r"""
^ ^

Loading…
Cancel
Save