From f64fbfe99bc805958cb089ef8aaaa8e2574f0f3b Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Mon, 8 Jul 2024 20:54:57 +0200 Subject: [PATCH] Fix spacing in K"parens" nodes --- src/runestone.jl | 8 ++++---- test/runtests.jl | 12 +++++++++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/runestone.jl b/src/runestone.jl index 9ee883b..676b2e0 100644 --- a/src/runestone.jl +++ b/src/runestone.jl @@ -293,7 +293,7 @@ end # TODO: Why did this function become sooo complicated? function spaces_in_listlike(ctx::Context, node::Node) if !( - kind(node) in KSet"tuple parameters curly braces bracescat vect ref" || + kind(node) in KSet"tuple parameters curly braces bracescat vect ref parens" || (kind(node) === K"call" && flags(node) == 0) || # Flag check rules out op-calls (kind(node) === K"dotcall" && flags(node) == 0) || is_paren_block(node) @@ -316,7 +316,7 @@ function spaces_in_listlike(ctx::Context, node::Node) # Find the opening and closing leafs implicit_tuple = false - if kind(node) in KSet"tuple call dotcall" || is_paren_block(node) + if kind(node) in KSet"tuple call dotcall parens" || is_paren_block(node) opening_leaf_idx = findfirst(x -> kind(x) === K"(", kids) if opening_leaf_idx === nothing # Implicit tuple without (), for example arguments in a do-block @@ -383,7 +383,7 @@ function spaces_in_listlike(ctx::Context, node::Node) kind(kids[first_item_idx::Int]) !== K"parameters" # TODO: May also have to check for K"where" and K"::" in the lineage above require_trailing_comma = true - elseif kind(node) in KSet"bracescat block" + elseif kind(node) in KSet"bracescat parens block" require_trailing_comma = false # Leads to parser error elseif kind(node) === K"parameters" # For parameters the trailing comma is configured from the parent @@ -770,7 +770,7 @@ function spaces_around_operators(ctx::Context, node::Node) end function spaces_around_assignments(ctx::Context, node::Node) - if !(is_assignment(node) && !is_leaf(node) ) + if !(is_assignment(node) && !is_leaf(node)) return nothing end # for-loop nodes are of kind K"=" even when `in` or `∈` is used so we need to diff --git a/test/runtests.jl b/test/runtests.jl index 7b69d4b..e830018 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -156,9 +156,9 @@ end # 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 () + # a op (b other_op c) @test format_string("$(sp)a$(sp)$(op)$(sp)($(sp)b$(sp)*$(sp)c$(sp))$(sp)") == - "$(sp)a $(op) ($(sp)b * c$(sp))$(sp)" + "$(sp)a $(op) (b * c)$(sp)" # call() op call() @test format_string("$(sp)sin(α)$(sp)$(op)$(sp)cos(β)$(sp)") == "$(sp)sin(α) $(op) cos(β)$(sp)" @@ -214,6 +214,8 @@ end "$(o)$(a), #==# $(b)$(c)" @test format_string("$(o)$(sp)$(a) #==#,$(sp)$(b)$(sp)$(c)") == "$(o)$(a) #==#, $(b)$(c)" + @test format_string("$(o)$(sp)$(a)#==# = 1$(sp)$(c)") == + "$(o)$(a) #==# = 1$(c)" # line break in between items @test format_string("$(o)$(sp)$(a)$(sp),\n$(sp)$(b)$(sp)$(c)") == format_string("$(o)$(sp)$(a)$(sp),\n$(sp)$(b)$(sp),$(sp)$(c)") == @@ -246,6 +248,10 @@ end @test format_string("$(o)\n$(a)$(sp)\n,$(sp)$(b)\n$(c)") == "$(o)\n $(a)\n , $(b),\n$(c)" end + # parens (but not block) + @test format_string("($(sp)$(a)$(sp))") == "($(a))" + @test format_string("($(sp)\n$(sp)$(a)$(sp)\n$(sp))") == "(\n $(a)\n)" + @test format_string("($(sp)\n$(sp)$(a)$(sp);$(sp)$(b)\n$(sp))") == "(\n $(a); $(b)\n)" # Implicit tuple (no parens) begin @test format_string("$(a)$(sp),$(sp)$(b)") == "$(a), $(b)" @@ -348,7 +354,7 @@ end @test format_string(" a$(a) b ") == " a $(a) b " @test format_string("a$(a) b") == "a $(a) b" @test format_string("a$(a) b * x") == "a $(a) b * x" - @test format_string("a$(a)( b * x)") == "a $(a) ( b * x)" + @test format_string("a$(a)( b * x)") == "a $(a) (b * x)" end # Chained assignments @test format_string("x=a= b ") == "x = a = b "