Browse Source

Filter out K":" and K"^" from space_around_operators

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.
pull/19/head
Fredrik Ekre 2 years ago
parent
commit
1bb9480718
No known key found for this signature in database
GPG Key ID: DE82E6D5E364C0A2
  1. 7
      src/chisel.jl
  2. 2
      src/runestone.jl
  3. 63
      test/runtests.jl

7
src/chisel.jl

@ -27,6 +27,13 @@ function is_infix_op_call(node::JuliaSyntax.GreenNode) @@ -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

2
src/runestone.jl

@ -269,7 +269,7 @@ end @@ -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))
)

63
test/runtests.jl

@ -91,18 +91,49 @@ end @@ -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
@ -137,10 +168,10 @@ end @@ -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

Loading…
Cancel
Save