Browse Source

Spacing around ternary operators, fixes #11.

pull/19/head
Fredrik Ekre 1 year ago
parent
commit
608439ba50
No known key found for this signature in database
GPG Key ID: DE82E6D5E364C0A2
  1. 1
      src/Runic.jl
  2. 8
      src/runestone.jl
  3. 11
      test/runtests.jl

1
src/Runic.jl

@ -303,6 +303,7 @@ function format_node!(ctx::Context, node::Node)::Union{Node, Nothing, NullNode} @@ -303,6 +303,7 @@ function format_node!(ctx::Context, node::Node)::Union{Node, Nothing, NullNode}
@return_something spaces_around_operators(ctx, node)
@return_something spaces_around_assignments(ctx, node)
@return_something spaces_around_anonymous_function(ctx, node)
@return_something spaces_around_ternary(ctx, node)
@return_something spaces_around_keywords(ctx, node)
@return_something no_spaces_around_colon_etc(ctx, node)
@return_something parens_around_op_calls_in_colon(ctx, node)

8
src/runestone.jl

@ -682,6 +682,14 @@ function spaces_around_anonymous_function(ctx::Context, node::Node) @@ -682,6 +682,14 @@ function spaces_around_anonymous_function(ctx::Context, node::Node)
return spaces_around_x(ctx, node, is_x)
end
function spaces_around_ternary(ctx::Context, node::Node)
if !(kind(node) === K"?" && !is_leaf(node))
return nothing
end
is_x = x -> is_leaf(x) && kind(x) in KSet"? :"
return spaces_around_x(ctx, node, is_x)
end
# Opposite of `spaces_around_x`: remove spaces around `x`
function no_spaces_around_x(ctx::Context, node::Node, is_x::F) where {F}
@assert !is_leaf(node)

11
test/runtests.jl

@ -277,6 +277,17 @@ end @@ -277,6 +277,17 @@ end
end
end
@testset "whitespace around ternary" begin
for sp in (" ", " ")
@test format_string("a$(sp)?$(sp)b$(sp):$(sp)c") == "a ? b : c"
@test format_string("a$(sp)?\nb$(sp):\nc") == "a ?\n b :\n c"
@test format_string("a$(sp)?$(sp)b$(sp):$(sp)c$(sp)?$(sp)d$(sp):$(sp)e") ==
"a ? b : c ? d : e"
@test format_string("a$(sp)?\nb$(sp):\nc$(sp)?\nd$(sp):\ne") ==
"a ?\n b :\n c ?\n d :\n e"
end
end
@testset "whitespace in comparison chains" begin
for sp in ("", " ", " ")
@test format_string("a$(sp)==$(sp)b") == "a == b"

Loading…
Cancel
Save