Skip to content

Commit

Permalink
Spacing around ternary operators, fixes #11.
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre committed Jun 23, 2024
1 parent 1ddcfac commit ae9ed30
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Runic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions src/runestone.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit ae9ed30

Please sign in to comment.