Browse Source

Fix edgecases in spaces in operator call chains

This patch fixes a bug where, in an operator call chain, whitespace was
borrows from the next child, but the call chain continued after.
pull/19/head
Fredrik Ekre 2 years ago
parent
commit
4d54e37fcd
No known key found for this signature in database
GPG Key ID: DE82E6D5E364C0A2
  1. 3
      src/runestone.jl
  2. 3
      test/runtests.jl

3
src/runestone.jl

@ -203,6 +203,8 @@ function spaces_around_x(ctx::Context, node::JuliaSyntax.GreenNode, is_x::F) whe
# Whitespace found at the beginning of next child. # Whitespace found at the beginning of next child.
child_ws = first_leaf(child) child_ws = first_leaf(child)
looking_for_whitespace = JuliaSyntax.kind(last_leaf(child)) !== K"Whitespace" looking_for_whitespace = JuliaSyntax.kind(last_leaf(child)) !== K"Whitespace"
@assert !is_x(child)::Bool
looking_for_x = true
if JuliaSyntax.span(child_ws) == 1 if JuliaSyntax.span(child_ws) == 1
# Accept the node # Accept the node
accept_node!(ctx, child) accept_node!(ctx, child)
@ -216,6 +218,7 @@ function spaces_around_x(ctx::Context, node::JuliaSyntax.GreenNode, is_x::F) whe
remaining_bytes_inclusive = remaining_bytes_inclusive =
@view original_bytes[(span_sum + 1 + bytes_to_skip - JuliaSyntax.span(child)):end] @view original_bytes[(span_sum + 1 + bytes_to_skip - JuliaSyntax.span(child)):end]
write_and_reset(ctx, remaining_bytes_inclusive) write_and_reset(ctx, remaining_bytes_inclusive)
accept_node!(ctx, child′)
any_changes = true any_changes = true
if children′ === children if children′ === children
children′ = children[1:i - 1] children′ = children[1:i - 1]

3
test/runtests.jl

@ -143,6 +143,9 @@ end
# JuliaSyntax.jl decides to place the K"Whitespace" node. # JuliaSyntax.jl decides to place the K"Whitespace" node.
@test format_string("$(sp)a$(sp)+$(sp)b$(sp)*$(sp)c$(sp)/$(sp)d$(sp)") == @test format_string("$(sp)a$(sp)+$(sp)b$(sp)*$(sp)c$(sp)/$(sp)d$(sp)") ==
"$(sp)a + b * c / d$(sp)" "$(sp)a + b * c / d$(sp)"
# Edgecase when using whitespace from the next leaf but the call chain continues
# after with more children.
@test format_string("$(sp)z$(sp)+$(sp)2x$(sp)+$(sp)z$(sp)") == "$(sp)z + 2x + z$(sp)"
end end
end end

Loading…
Cancel
Save