diff --git a/src/runestone.jl b/src/runestone.jl index c48e0dc..3f88835 100644 --- a/src/runestone.jl +++ b/src/runestone.jl @@ -149,6 +149,8 @@ function spaces_around_x(ctx::Context, node::Node, is_x::F, n_leaves_per_x::Int looking_for_x = false n_x_leaves_visited = 0 + peek(kids, i) = i < length(kids) ? kind(kids[i + 1]) : nothing + for (i, kid) in pairs(kids) if kind(kid) === K"NewlineWs" || (i == 1 && kind(kid) === K"Whitespace") @@ -160,7 +162,8 @@ function spaces_around_x(ctx::Context, node::Node, is_x::F, n_leaves_per_x::Int any_changes && push!(kids′, kid) looking_for_whitespace = false elseif looking_for_whitespace - if kind(kid) === K"Whitespace" && span(kid) == 1 + if (kind(kid) === K"Whitespace" && span(kid) == 1) || + (kind(kid) === K"Whitespace" && peek(kids, i) === K"Comment" && peek(kids, i + 1) === K"NewlineWs") # All good, just advance the IO accept_node!(ctx, kid) any_changes && push!(kids′, kid) diff --git a/test/runtests.jl b/test/runtests.jl index 75e74da..981cf26 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -182,8 +182,9 @@ end @test format_string("$(sp)a$(sp)$(op)$(sp)\nb$(sp)") == "$(sp)a $(op)\n b$(sp)" # a op # comment \n b + minspace = sp == "" ? " " : sp @test format_string("$(sp)a$(sp)$(op)$(sp)# comment\nb$(sp)") == - "$(sp)a $(op) # comment\n b$(sp)" + "$(sp)a $(op)$(minspace)# comment\n b$(sp)" end # Exceptions to the rule: `:`, `..`, and `^` # a:b