Skip to content

Commit

Permalink
Ignore paren-blocks in trailing semicolon trimming
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre committed Aug 23, 2024
1 parent c9e2b6c commit 3931a4f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/runestone.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3302,10 +3302,13 @@ function remove_trailing_semicolon(ctx::Context, node::Node)
pos = position(ctx.fmt_io)
kids = verified_kids(node)
kids′ = kids
block_idx = findfirst(x -> kind(x) === K"block", kids′)
block_predicate = function(x)
return kind(x) === K"block" && !JuliaSyntax.has_flags(x, JuliaSyntax.PARENS_FLAG)
end
block_idx = findfirst(block_predicate, kids′)
if kind(node) === K"let"
# The first block of let is the variables
block_idx = findnext(x -> kind(x) === K"block", kids′, block_idx + 1)
block_idx = findnext(block_predicate, kids′, block_idx + 1)
end
any_changed = false
while block_idx !== nothing
Expand All @@ -3323,7 +3326,7 @@ function remove_trailing_semicolon(ctx::Context, node::Node)
end
seek(ctx.fmt_io, p)
end
block_idx = findnext(x -> kind(x) === K"block", kids′, block_idx + 1)
block_idx = findnext(block_predicate, kids′, block_idx + 1)
end
# Reset the stream and return
seek(ctx.fmt_io, pos)
Expand Down
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,9 @@ end
@test format_string("$(mut)struct A\na::Int;\nend") ==
"$(mut)struct A\n a::Int\nend"
end
# Paren-blocks should be skipped
@test format_string("if (a;\nb)\nend") == "if (\n a;\n b\n )\nend"
@test format_string("if begin a;\nb; end\nend") == "if begin\n a\n b\n end\nend"
# Top-level semicolons are kept (useful if you want to supress output in various
# contexts)
let str = """
Expand Down

0 comments on commit 3931a4f

Please sign in to comment.