Skip to content

Commit

Permalink
Filter out K":" and K"^" from space_around_operators
Browse files Browse the repository at this point in the history
It is a bit weird to not be consistent and put spaces around all
operators, but most people would agree that using no spaces for `:` and
`^` look better.
  • Loading branch information
fredrikekre committed May 26, 2024
1 parent d076ca8 commit 1bb9480
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 17 deletions.
7 changes: 7 additions & 0 deletions src/chisel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ function is_infix_op_call(node::JuliaSyntax.GreenNode)
return JuliaSyntax.kind(node) === K"call" &&
JuliaSyntax.is_infix_op_call(node)
end
function infix_op_call_op(node::JuliaSyntax.GreenNode)
@assert is_infix_op_call(node)
children = JuliaSyntax.children(node)::AbstractVector
first_operand_index = findfirst(!JuliaSyntax.is_whitespace, children)
op_index = findnext(JuliaSyntax.is_operator, children, first_operand_index + 1)
return children[op_index]
end
function is_comparison_leaf(node::JuliaSyntax.GreenNode)
return is_leaf(node) && JuliaSyntax.is_prec_comparison(node)
end
Expand Down
2 changes: 1 addition & 1 deletion src/runestone.jl
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ end
# This pass handles spaces around infix operator calls and comparison chains
function spaces_around_operators(ctx::Context, node::JuliaSyntax.GreenNode)
if !(
is_infix_op_call(node) ||
(is_infix_op_call(node) && !(JuliaSyntax.kind(infix_op_call_op(node)) in KSet": ^")) ||
(JuliaSyntax.kind(node) in KSet"<: >:" && !is_leaf(node)) ||
(JuliaSyntax.kind(node) === K"comparison" && !JuliaSyntax.is_trivia(node))
)
Expand Down
63 changes: 47 additions & 16 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,49 @@ end
end

@testset "whitespace between operators" begin
for op in ("+", "-", "==", "!=", "===", "!==", "<", "<=")
@test format_string("a$(op)b") == "a $(op) b"
@test format_string("a $(op)b") == "a $(op) b"
@test format_string("a$(op) b") == "a $(op) b"
@test format_string(" a$(op) b") == " a $(op) b"
@test format_string(" a$(op) b ") == " a $(op) b "
@test format_string("x=a$(op) b ") == "x = a $(op) b "
@test format_string("a$(op) b") == "a $(op) b"
@test format_string("a$(op) b $(op) x") == "a $(op) b $(op) x"
@test format_string("a$(op) b * x") == "a $(op) b * x"
@test format_string("a$(op)( b * x)") == "a $(op) ( b * x)"
@test format_string("sin(π)$(op)cos(pi)") == "sin(π) $(op) cos(pi)"
for sp in ("", " ", " ")
for op in ("+", "-", "==", "!=", "===", "!==", "<", "<=")
# a op b
@test format_string("$(sp)a$(sp)$(op)$(sp)b$(sp)") ==
"$(sp)a $(op) b$(sp)"
# x = a op b
@test format_string("$(sp)x$(sp)=$(sp)a$(sp)$(op)$(sp)b$(sp)") ==
"$(sp)x = a $(op) b$(sp)"
# a op b op c
@test format_string("$(sp)a$(sp)$(op)$(sp)b$(sp)$(op)$(sp)c$(sp)") ==
"$(sp)a $(op) b $(op) c$(sp)"
# a op b other_op c
@test format_string("$(sp)a$(sp)$(op)$(sp)b$(sp)*$(sp)c$(sp)") ==
"$(sp)a $(op) b * c$(sp)"
# a op (b other_op c) (TODO: leading and trailing spaces should be removed in ()
@test format_string("$(sp)a$(sp)$(op)$(sp)($(sp)b$(sp)*$(sp)c$(sp))$(sp)") ==
"$(sp)a $(op) ($(sp)b * c$(sp))$(sp)"
# call() op call()
@test format_string("$(sp)sin(α)$(sp)$(op)$(sp)cos(β)$(sp)") ==
"$(sp)sin(α) $(op) cos(β)$(sp)"
# call() op call() op call()
@test format_string("$(sp)sin(α)$(sp)$(op)$(sp)cos(β)$(sp)$(op)$(sp)tan(γ)$(sp)") ==
"$(sp)sin(α) $(op) cos(β) $(op) tan(γ)$(sp)"
end
# Exceptions to the rule: `:` and `^`
if sp == ""
# a:b
@test format_string("$(sp)a$(sp):$(sp)b$(sp)") == "a:b"
@test format_string("$(sp)(1 + 2)$(sp):$(sp)(1 + 3)$(sp)") == "(1 + 2):(1 + 3)"
# a:b:c
@test format_string("$(sp)a$(sp):$(sp)b$(sp):$(sp)c$(sp)") == "a:b:c"
@test format_string("$(sp)(1 + 2)$(sp):$(sp)(1 + 3)$(sp):$(sp)(1 + 4)$(sp)") ==
"(1 + 2):(1 + 3):(1 + 4)"
# a^b
@test format_string("$(sp)a$(sp)^$(sp)b$(sp)") == "a^b"
else
@test_broken format_string("$(sp)a$(sp):$(sp)b$(sp)") == "a:b"
@test_broken format_string("$(sp)(1 + 2)$(sp):$(sp)(1 + 3)$(sp)") == "(1 + 2):(1 + 3)"
@test_broken format_string("$(sp)a$(sp):$(sp)b$(sp):$(sp)c$(sp)") == "a:b:c"
@test_broken format_string("$(sp)(1 + 2)$(sp):$(sp)(1 + 3)$(sp):$(sp)(1 + 4)$(sp)") ==
"(1 + 2):(1 + 3):(1 + 4)"
@test_broken format_string("$(sp)a$(sp)^$(sp)b$(sp)") == "a^b"
end
end
end

Expand Down Expand Up @@ -137,10 +168,10 @@ end
# Short form function definitions
@test format_string("sin(π)=cos(pi)") == "sin(π) = cos(pi)"
# For loop nodes are assignment, even when using `in`
@test format_string("for i=1:10\nend\n") == "for i = 1 : 10\nend\n"
@test format_string("for i =1:10\nend\n") == "for i = 1 : 10\nend\n"
@test format_string("for i = 1:10\nend\n") == "for i = 1 : 10\nend\n"
@test format_string("for i in 1:10\nend\n") == "for i in 1 : 10\nend\n"
@test format_string("for i=1:10\nend\n") == "for i = 1:10\nend\n"
@test format_string("for i =1:10\nend\n") == "for i = 1:10\nend\n"
@test format_string("for i = 1:10\nend\n") == "for i = 1:10\nend\n"
@test format_string("for i in 1:10\nend\n") == "for i in 1:10\nend\n"
end

@testset "whitespace around <: and >:, no whitespace around ::" begin
Expand Down

0 comments on commit 1bb9480

Please sign in to comment.