Browse Source

Fix spacing in K"parens" nodes

pull/19/head
Fredrik Ekre 1 year ago
parent
commit
f64fbfe99b
No known key found for this signature in database
GPG Key ID: DE82E6D5E364C0A2
  1. 6
      src/runestone.jl
  2. 10
      test/runtests.jl

6
src/runestone.jl

@ -293,7 +293,7 @@ end
# TODO: Why did this function become sooo complicated? # TODO: Why did this function become sooo complicated?
function spaces_in_listlike(ctx::Context, node::Node) function spaces_in_listlike(ctx::Context, node::Node)
if !( 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"call" && flags(node) == 0) || # Flag check rules out op-calls
(kind(node) === K"dotcall" && flags(node) == 0) || (kind(node) === K"dotcall" && flags(node) == 0) ||
is_paren_block(node) is_paren_block(node)
@ -316,7 +316,7 @@ function spaces_in_listlike(ctx::Context, node::Node)
# Find the opening and closing leafs # Find the opening and closing leafs
implicit_tuple = false 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) opening_leaf_idx = findfirst(x -> kind(x) === K"(", kids)
if opening_leaf_idx === nothing if opening_leaf_idx === nothing
# Implicit tuple without (), for example arguments in a do-block # 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" kind(kids[first_item_idx::Int]) !== K"parameters"
# TODO: May also have to check for K"where" and K"::" in the lineage above # TODO: May also have to check for K"where" and K"::" in the lineage above
require_trailing_comma = true 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 require_trailing_comma = false # Leads to parser error
elseif kind(node) === K"parameters" elseif kind(node) === K"parameters"
# For parameters the trailing comma is configured from the parent # For parameters the trailing comma is configured from the parent

10
test/runtests.jl

@ -156,9 +156,9 @@ end
# a op b other_op c # a op b other_op c
@test format_string("$(sp)a$(sp)$(op)$(sp)b$(sp)*$(sp)c$(sp)") == @test format_string("$(sp)a$(sp)$(op)$(sp)b$(sp)*$(sp)c$(sp)") ==
"$(sp)a $(op) b * 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)") == @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() # call() op call()
@test format_string("$(sp)sin(α)$(sp)$(op)$(sp)cos(β)$(sp)") == @test format_string("$(sp)sin(α)$(sp)$(op)$(sp)cos(β)$(sp)") ==
"$(sp)sin(α) $(op) cos(β)$(sp)" "$(sp)sin(α) $(op) cos(β)$(sp)"
@ -214,6 +214,8 @@ end
"$(o)$(a), #==# $(b)$(c)" "$(o)$(a), #==# $(b)$(c)"
@test format_string("$(o)$(sp)$(a) #==#,$(sp)$(b)$(sp)$(c)") == @test format_string("$(o)$(sp)$(a) #==#,$(sp)$(b)$(sp)$(c)") ==
"$(o)$(a) #==#, $(b)$(c)" "$(o)$(a) #==#, $(b)$(c)"
@test format_string("$(o)$(sp)$(a)#==# = 1$(sp)$(c)") ==
"$(o)$(a) #==# = 1$(c)"
# line break in between items # line break in between items
@test format_string("$(o)$(sp)$(a)$(sp),\n$(sp)$(b)$(sp)$(c)") == @test format_string("$(o)$(sp)$(a)$(sp),\n$(sp)$(b)$(sp)$(c)") ==
format_string("$(o)$(sp)$(a)$(sp),\n$(sp)$(b)$(sp),$(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)") == @test format_string("$(o)\n$(a)$(sp)\n,$(sp)$(b)\n$(c)") ==
"$(o)\n $(a)\n , $(b),\n$(c)" "$(o)\n $(a)\n , $(b),\n$(c)"
end 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) # Implicit tuple (no parens)
begin begin
@test format_string("$(a)$(sp),$(sp)$(b)") == "$(a), $(b)" @test format_string("$(a)$(sp),$(sp)$(b)") == "$(a), $(b)"

Loading…
Cancel
Save