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. 8
      src/runestone.jl
  2. 12
      test/runtests.jl

8
src/runestone.jl

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

12
test/runtests.jl

@ -156,9 +156,9 @@ end @@ -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 @@ -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 @@ -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 @@ -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 "

Loading…
Cancel
Save