Skip to content

Commit

Permalink
Fix whitespace before comment in operator formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre committed Oct 31, 2024
1 parent e395af1 commit 2308250
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/runestone.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2308250

Please sign in to comment.