Skip to content

Commit

Permalink
Format macrocalls with parens as function calls
Browse files Browse the repository at this point in the history
This patch formats macrocalls with parens like function calls. This
means that indenting and list-spacing is the same.
  • Loading branch information
fredrikekre committed Jul 20, 2024
1 parent d09e6af commit 7604ae7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/runestone.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)") ==
Expand Down Expand Up @@ -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)") ==
Expand Down

0 comments on commit 7604ae7

Please sign in to comment.