diff --git a/src/Runic.jl b/src/Runic.jl index ef3a708..7b5c1df 100644 --- a/src/Runic.jl +++ b/src/Runic.jl @@ -69,10 +69,11 @@ function next_sibling_kind(ctx::Context)::Union{JuliaSyntax.Kind, Nothing} end # Read the bytes of the current node from the output io -function node_bytes(ctx, node) - pos = mark(ctx.fmt_io) +function read_bytes(ctx, node) + pos = position(ctx.fmt_io) 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 return bytes end diff --git a/src/runestone.jl b/src/runestone.jl index 4b79b25..a1e0f64 100644 --- a/src/runestone.jl +++ b/src/runestone.jl @@ -5,7 +5,7 @@ function trim_trailing_whitespace(ctx::Context, node::JuliaSyntax.GreenNode) JuliaSyntax.kind(node) === K"NewlineWs" || return nothing @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') # If the next sibling is also a NewlineWs we can trim trailing # whitespace from this node too @@ -39,7 +39,7 @@ function format_hex_literals(ctx::Context, node::JuliaSyntax.GreenNode) end # Insert leading zeros i = findfirst(x -> x > span, target_spans)::Int - bytes = node_bytes(ctx, node) + bytes = read_bytes(ctx, node) while length(bytes) < target_spans[i] insert!(bytes, 3, '0') end @@ -57,7 +57,7 @@ function format_oct_literals(ctx::Context, node::JuliaSyntax.GreenNode) span = JuliaSyntax.span(node) @assert span > 2 # 0o prefix + something more # Padding depends on the value of the literal... - str = String(node_bytes(ctx, node)) + str = String(read_bytes(ctx, node)) n = tryparse(UInt128, str) if n === nothing # Do nothing: BigInt oct literal @@ -77,7 +77,7 @@ function format_oct_literals(ctx::Context, node::JuliaSyntax.GreenNode) return nothing end # Insert leading zeros - bytes = node_bytes(ctx, node) + bytes = read_bytes(ctx, node) while length(bytes) < target_span insert!(bytes, 3, '0') end @@ -92,7 +92,7 @@ function format_float_literals(ctx::Context, node::JuliaSyntax.GreenNode) JuliaSyntax.kind(node) in KSet"Float Float32" || return nothing @assert JuliaSyntax.flags(node) == 0 @assert !JuliaSyntax.haschildren(node) - str = String(node_bytes(ctx, node)) + str = String(read_bytes(ctx, node)) # Check and shortcut the happy path first r = r""" ^