Browse Source

Remove leading whitespace in the beginning of a file

This is needed to "bootstrap" the indent process since there isn't a
leading newline to indent to level 0 when there is leading whitespace.
pull/23/head
Fredrik Ekre 1 year ago
parent
commit
0e214b6c69
No known key found for this signature in database
GPG Key ID: DE82E6D5E364C0A2
  1. 4
      src/runestone.jl
  2. 1
      test/runtests.jl

4
src/runestone.jl

@ -1357,12 +1357,14 @@ function no_leading_and_single_trailing_newline(ctx::Context, node::Node)
@assert !is_leaf(node) @assert !is_leaf(node)
@assert position(ctx.fmt_io) == 0 @assert position(ctx.fmt_io) == 0
changed = false changed = false
while (l = first_leaf(node); l !== nothing && kind(l) === K"NewlineWs" && length(verified_kids(node)) > 1) # Remove leading newlines and whitespace
while (l = first_leaf(node); l !== nothing && kind(l) in KSet"NewlineWs Whitespace" && length(verified_kids(node)) > 1)
changed = true changed = true
replace_bytes!(ctx, "", span(l)) replace_bytes!(ctx, "", span(l))
node = replace_first_leaf(node, nullnode) node = replace_first_leaf(node, nullnode)
end end
accept_node!(ctx, node) accept_node!(ctx, node)
# Remove trailing newlines
l = last_leaf(node) l = last_leaf(node)
if l === nothing || kind(l) !== K"NewlineWs" if l === nothing || kind(l) !== K"NewlineWs"
kids′ = copy(verified_kids(node)) kids′ = copy(verified_kids(node))

1
test/runtests.jl

@ -775,6 +775,7 @@ end
@test format_string("$(nl)f()$(nl)"; filemode = true) == "f()\n" @test format_string("$(nl)f()$(nl)"; filemode = true) == "f()\n"
@test format_string("$(nl)"; filemode = true) == "\n" @test format_string("$(nl)"; filemode = true) == "\n"
end end
@test format_string(" x\n"; filemode = true) == "x\n"
end end
@testset "https://youtu.be/SsoOG6ZeyUI?si=xpKpnczuqsOThtFP" begin @testset "https://youtu.be/SsoOG6ZeyUI?si=xpKpnczuqsOThtFP" begin

Loading…
Cancel
Save