Skip to content

Commit

Permalink
Fix trailing semicolon in parens-block
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fredrikekre committed Aug 9, 2024
1 parent 9edf7d7 commit a41bc5d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/runestone.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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" ||
Expand Down
7 changes: 4 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down

0 comments on commit a41bc5d

Please sign in to comment.