diff --git a/src/runestone.jl b/src/runestone.jl index 48d2881..f5d5e44 100644 --- a/src/runestone.jl +++ b/src/runestone.jl @@ -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)) diff --git a/test/runtests.jl b/test/runtests.jl index dddc044..d544c59 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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