From 3931a4ffe3e46220e72829de367a9366de1684d2 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Fri, 23 Aug 2024 02:01:22 +0200 Subject: [PATCH] Ignore paren-blocks in trailing semicolon trimming --- src/runestone.jl | 9 ++++++--- test/runtests.jl | 3 +++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/runestone.jl b/src/runestone.jl index 48f5363..f61cd7d 100644 --- a/src/runestone.jl +++ b/src/runestone.jl @@ -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 @@ -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) diff --git a/test/runtests.jl b/test/runtests.jl index 01c454e..5838aba 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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 = """