From 1bb9480718bb3791445fab9c8c519b8ffbfe5577 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Mon, 27 May 2024 00:48:43 +0200 Subject: [PATCH] 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. --- src/chisel.jl | 7 ++++++ src/runestone.jl | 2 +- test/runtests.jl | 63 ++++++++++++++++++++++++++++++++++++------------ 3 files changed, 55 insertions(+), 17 deletions(-) diff --git a/src/chisel.jl b/src/chisel.jl index 07b3d68..c114947 100644 --- a/src/chisel.jl +++ b/src/chisel.jl @@ -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 diff --git a/src/runestone.jl b/src/runestone.jl index 4918888..496b55c 100644 --- a/src/runestone.jl +++ b/src/runestone.jl @@ -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)) ) diff --git a/test/runtests.jl b/test/runtests.jl index 9502282..50d9f69 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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 # 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