Skip to content

Commit

Permalink
Remove leading whitespace in the beginning of a file
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fredrikekre committed Jul 22, 2024
1 parent e7229cb commit 0e214b6
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/runestone.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1357,12 +1357,14 @@ function no_leading_and_single_trailing_newline(ctx::Context, node::Node)
@assert !is_leaf(node)
@assert position(ctx.fmt_io) == 0
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
replace_bytes!(ctx, "", span(l))
node = replace_first_leaf(node, nullnode)
end
accept_node!(ctx, node)
# Remove trailing newlines
l = last_leaf(node)
if l === nothing || kind(l) !== K"NewlineWs"
kids′ = copy(verified_kids(node))
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,7 @@ end
@test format_string("$(nl)f()$(nl)"; filemode = true) == "f()\n"
@test format_string("$(nl)"; filemode = true) == "\n"
end
@test format_string(" x\n"; filemode = true) == "x\n"
end

@testset "https://youtu.be/SsoOG6ZeyUI?si=xpKpnczuqsOThtFP" begin
Expand Down

0 comments on commit 0e214b6

Please sign in to comment.