Browse Source

Fix trailing semicolon in parens-block

This patch make sure to keep the trailing semicolon in parens-block if
there is just a single item. Without the trailing colon the text would
be parsed as parens but not as a block (compare `(1)` vs `(1,)` for
tuples). Closes #38.
pull/44/head
Fredrik Ekre 1 year ago
parent
commit
a41bc5dd81
No known key found for this signature in database
GPG Key ID: DE82E6D5E364C0A2
  1. 3
      src/runestone.jl
  2. 7
      test/runtests.jl

3
src/runestone.jl

@ -407,7 +407,7 @@ function spaces_in_listlike(ctx::Context, node::Node) @@ -407,7 +407,7 @@ function spaces_in_listlike(ctx::Context, node::Node)
require_trailing_comma = false # Leads to parser error
elseif kind(node) in KSet"block"
require_trailing_comma = false
allow_trailing_semi = n_items == 0
allow_trailing_semi = n_items < 2
elseif kind(node) === K"parameters"
# For parameters the trailing comma is configured from the parent
require_trailing_comma = has_tag(node, TAG_TRAILING_COMMA)
@ -756,6 +756,7 @@ function spaces_in_listlike(ctx::Context, node::Node) @@ -756,6 +756,7 @@ function spaces_in_listlike(ctx::Context, node::Node)
replace_bytes!(ctx, "", span(kid′))
elseif kind(node) === K"block" && kind(kid′) === K";" && allow_trailing_semi ||
(kind(kid′) === K"Whitespace" && peek(i) !== K"Comment")
allow_trailing_semi = n_items == 0 # Only one semicolon allowed
accept_node!(ctx, kid′)
any_kid_changed && push!(kids′, kid′)
elseif kind(kid′) === K"NewlineWs" ||

7
test/runtests.jl

@ -333,12 +333,13 @@ end @@ -333,12 +333,13 @@ end
"$(x){\n $(a), $(a); $(b),\n}"
end
# Trailing `;` in paren-block
@test format_string("(a = A;)") == "(a = A)"
@test format_string("cond && (a = A;)") == "cond && (a = A)"
@test format_string("(a = A;)") == "(a = A;)"
@test format_string("cond && (a = A;)") == "cond && (a = A;)"
@test format_string("(a = A; b = B;)") == "(a = A; b = B)"
@test format_string("(a = A;;)") == "(a = A)"
@test format_string("(a = A;;)") == "(a = A;)"
@test format_string("(;;)") == format_string("( ; ; )") == "(;;)"
@test format_string("(;)") == format_string("( ; )") == "(;)"
@test format_string("(@a b(c);)") == "(@a b(c);)"
# https://github.com/fredrikekre/Runic.jl/issues/16
@test format_string("(i for i in\nI)") == "(\n i for i in\n I\n)"
@test format_string("f(i for i in\nI)") == "f(\n i for i in\n I\n)"

Loading…
Cancel
Save