From 7604ae749a67ba842384a030e7918514fb08cc28 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Sat, 20 Jul 2024 13:39:55 +0200 Subject: [PATCH] Format macrocalls with parens as function calls This patch formats macrocalls with parens like function calls. This means that indenting and list-spacing is the same. --- src/runestone.jl | 8 ++++++-- test/runtests.jl | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/runestone.jl b/src/runestone.jl index 268f00d..2ed175e 100644 --- a/src/runestone.jl +++ b/src/runestone.jl @@ -296,6 +296,7 @@ function spaces_in_listlike(ctx::Context, node::Node) kind(node) in KSet"tuple parameters curly braces bracescat vect ref parens" || (kind(node) === K"call" && flags(node) == 0) || # Flag check rules out op-calls (kind(node) === K"dotcall" && flags(node) == 0) || + (kind(node) === K"macrocall" && JuliaSyntax.has_flags(node, JuliaSyntax.PARENS_FLAG)) || is_paren_block(node) ) return nothing @@ -316,7 +317,7 @@ function spaces_in_listlike(ctx::Context, node::Node) # Find the opening and closing leafs implicit_tuple = false - if kind(node) in KSet"tuple call dotcall parens" || is_paren_block(node) + if kind(node) in KSet"tuple call dotcall parens macrocall" || is_paren_block(node) opening_leaf_idx = findfirst(x -> kind(x) === K"(", kids) if opening_leaf_idx === nothing # Implicit tuple without (), for example arguments in a do-block @@ -2045,7 +2046,7 @@ end # Mark opening and closing parentheses, in a call or a tuple, with indent and dedent tags. function indent_paren(ctx::Context, node::Node) - @assert kind(node) in KSet"call dotcall tuple parens" + @assert kind(node) in KSet"call dotcall tuple parens macrocall" kids = verified_kids(node) opening_paren_idx = findfirst(x -> kind(x) === K"(", kids)::Int closing_paren_idx = findnext(x -> kind(x) === K")", kids, opening_paren_idx + 1)::Int @@ -2378,6 +2379,9 @@ function insert_delete_mark_newlines(ctx::Context, node::Node) elseif kind(node) in KSet"call dotcall" && flags(node) == 0 # Flag check rules out op-calls return indent_call(ctx, node) + elseif kind(node) === K"macrocall" && + JuliaSyntax.has_flags(node, JuliaSyntax.PARENS_FLAG) + return indent_paren(ctx, node) elseif is_infix_op_call(node) return indent_op_call(ctx, node) elseif kind(node) in KSet"for while" diff --git a/test/runtests.jl b/test/runtests.jl index 7ec6632..406d8cc 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -203,7 +203,7 @@ end @testset "spaces in listlike" begin for sp in ("", " ", " "), a in ("a", "a + a", "a(x)"), b in ("b", "b + b", "b(y)") # tuple, call, dotcall, vect, ref - for (o, c) in (("(", ")"), ("f(", ")"), ("f.(", ")"), ("[", "]"), ("T[", "]")) + for (o, c) in (("(", ")"), ("f(", ")"), ("@f(", ")"), ("f.(", ")"), ("[", "]"), ("T[", "]")) # single line @test format_string("$(o)$(sp)$(c)") == "$(o)$(c)" @test format_string("$(o)$(sp)$(a)$(sp),$(sp)$(b)$(sp)$(c)") == @@ -737,7 +737,7 @@ end end @testset "leading and trailing newline in multiline listlike" begin - for (o, c) in (("f(", ")"), ("(", ")"), ("{", "}")) + for (o, c) in (("f(", ")"), ("@f(", ")"), ("(", ")"), ("{", "}")) @test format_string("$(o)a,\nb$(c)") == format_string("$(o)\na,\nb$(c)") == format_string("$(o)\na,\nb\n$(c)") ==