From bf9e3b0409851f3c7216f7bf6d234ae296b9708a Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Fri, 2 Aug 2024 01:15:37 +0200 Subject: [PATCH] Skip trailing comma in list after macrocall items Fixes #32. --- src/chisels.jl | 9 +++++++++ src/runestone.jl | 4 ++++ test/runtests.jl | 6 ++++++ 3 files changed, 19 insertions(+) diff --git a/src/chisels.jl b/src/chisels.jl index 1b9bae4..431c1bd 100644 --- a/src/chisels.jl +++ b/src/chisels.jl @@ -537,6 +537,15 @@ function contains_multiline_triple_string(ctx, node::Node) return false end +function is_string_macro(node) + kind(node) === K"macrocall" || return false + @assert !is_leaf(node) + kids = verified_kids(node) + return length(kids) >= 2 && + kind(kids[1]) in KSet"StringMacroName CmdMacroName core_@cmd" && + kind(kids[2]) in KSet"string cmdstring" +end + function is_triple_string(node) return kind(node) in KSet"string cmdstring" && JuliaSyntax.has_flags(node, JuliaSyntax.TRIPLE_STRING_FLAG) diff --git a/src/runestone.jl b/src/runestone.jl index d3dbb52..e1124fa 100644 --- a/src/runestone.jl +++ b/src/runestone.jl @@ -415,6 +415,10 @@ function spaces_in_listlike(ctx::Context, node::Node) elseif kind(node) === K"parameters" # For parameters the trailing comma is configured from the parent require_trailing_comma = has_tag(node, TAG_TRAILING_COMMA) + 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]) + require_trailing_comma = false elseif multiline require_trailing_comma = true elseif n_items > 0 diff --git a/test/runtests.jl b/test/runtests.jl index 5d0d50c..e45685f 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -348,6 +348,12 @@ end @test format_string("@f($(sp)a$(sp);$(sp)b = 1$(sp))") == "@f(a; b = 1)" @test format_string("@f($(sp);$(sp)b$(sp))") == "@f(; b)" end + # https://github.com/fredrikekre/Runic.jl/issues/32 + @test format_string("f(@m begin\nend)") == "f(\n @m begin\n end\n)" + @test format_string("f(@m(begin\nend))") == "f(\n @m(\n begin\n end,\n ),\n)" + @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)" end @testset "whitespace around ->" begin