From 8baaf8344f38c310c2528b5a39d58c29a86fb0b7 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Fri, 18 Oct 2024 16:26:43 +0200 Subject: [PATCH] Fix commas after trailing macrocalls hidden inside of list items (#67) --- src/chisels.jl | 14 ++++++++++++++ src/runestone.jl | 8 +++++--- test/runtests.jl | 3 +++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/chisels.jl b/src/chisels.jl index 5bd8046..ef8f759 100644 --- a/src/chisels.jl +++ b/src/chisels.jl @@ -630,6 +630,20 @@ function last_leaf_predicate(node::Node, pred::F) where {F} end end +function predicate_contains(pred::F, node::Node) where {F} + if pred(node)::Bool + return true + elseif is_leaf(node) + return false + else + for k in verified_kids(node) + r = predicate_contains(pred, k) + r && return r + end + return false + end +end + function contains_outer_newline(kids::Vector{Node}, oidx::Int, cidx::Int; recurse = true) pred = x -> kind(x) === K"NewlineWs" || !JuliaSyntax.is_whitespace(x) for i in (oidx + 1):(cidx - 1) diff --git a/src/runestone.jl b/src/runestone.jl index 56ab3df..fe77730 100644 --- a/src/runestone.jl +++ b/src/runestone.jl @@ -387,9 +387,11 @@ function spaces_in_listlike(ctx::Context, node::Node) # For parameters the trailing comma is configured from the parent require_trailing_comma = has_tag(node, TAG_TRAILING_COMMA) allow_trailing_comma = has_tag(node, TAG_TRAILING_COMMA_OPT) - elseif n_items > 0 && kind(kids[last_item_idx]) === K"macrocall" && - !JuliaSyntax.has_flags(kids[last_item_idx], JuliaSyntax.PARENS_FLAG) && - !is_string_macro(kids[last_item_idx]) + elseif n_items > 0 && predicate_contains(kids[last_item_idx]) do nd + return kind(nd) === K"macrocall" && + !JuliaSyntax.has_flags(nd, JuliaSyntax.PARENS_FLAG) && !is_string_macro(nd) + end + # Unparenthesized macrocalls are scary even if hidden deep in the tree require_trailing_comma = false elseif multiline require_trailing_comma = true diff --git a/test/runtests.jl b/test/runtests.jl index b7ca4f2..219b137 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -379,6 +379,9 @@ end @test format_string("f(r\"\"\"\nf\n\"\"\")") == "f(\n r\"\"\"\n f\n \"\"\"\n)" @test format_string("f(```\nf\n```)") == "f(\n ```\n f\n ```\n)" @test format_string("f(x```\nf\n```)") == "f(\n x```\n f\n ```\n)" + @test format_string("(a, @m begin\nend)") == "(\n a, @m begin\n end\n)" + @test format_string("(\na, x -> @m x[i]\n)") == "(\n a, x -> @m x[i]\n)" + @test format_string("(\na, x -> @m(x[i])\n)") == "(\n a, x -> @m(x[i]),\n)" # Weird cornercase where a trailing comma messes some cases up (don't recall...) @test format_string("{\n@f\n}") == "{\n @f\n}" end