Browse Source

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.
pull/19/head
Fredrik Ekre 1 year ago
parent
commit
7604ae749a
No known key found for this signature in database
GPG Key ID: DE82E6D5E364C0A2
  1. 8
      src/runestone.jl
  2. 4
      test/runtests.jl

8
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) 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"call" && flags(node) == 0) || # Flag check rules out op-calls
(kind(node) === K"dotcall" && flags(node) == 0) || (kind(node) === K"dotcall" && flags(node) == 0) ||
(kind(node) === K"macrocall" && JuliaSyntax.has_flags(node, JuliaSyntax.PARENS_FLAG)) ||
is_paren_block(node) is_paren_block(node)
) )
return nothing return nothing
@ -316,7 +317,7 @@ function spaces_in_listlike(ctx::Context, node::Node)
# Find the opening and closing leafs # Find the opening and closing leafs
implicit_tuple = false 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) opening_leaf_idx = findfirst(x -> kind(x) === K"(", kids)
if opening_leaf_idx === nothing if opening_leaf_idx === nothing
# Implicit tuple without (), for example arguments in a do-block # 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. # Mark opening and closing parentheses, in a call or a tuple, with indent and dedent tags.
function indent_paren(ctx::Context, node::Node) 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) kids = verified_kids(node)
opening_paren_idx = findfirst(x -> kind(x) === K"(", kids)::Int opening_paren_idx = findfirst(x -> kind(x) === K"(", kids)::Int
closing_paren_idx = findnext(x -> kind(x) === K")", kids, opening_paren_idx + 1)::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" && elseif kind(node) in KSet"call dotcall" &&
flags(node) == 0 # Flag check rules out op-calls flags(node) == 0 # Flag check rules out op-calls
return indent_call(ctx, node) 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) elseif is_infix_op_call(node)
return indent_op_call(ctx, node) return indent_op_call(ctx, node)
elseif kind(node) in KSet"for while" elseif kind(node) in KSet"for while"

4
test/runtests.jl

@ -203,7 +203,7 @@ end
@testset "spaces in listlike" begin @testset "spaces in listlike" begin
for sp in ("", " ", " "), a in ("a", "a + a", "a(x)"), b in ("b", "b + b", "b(y)") for sp in ("", " ", " "), a in ("a", "a + a", "a(x)"), b in ("b", "b + b", "b(y)")
# tuple, call, dotcall, vect, ref # tuple, call, dotcall, vect, ref
for (o, c) in (("(", ")"), ("f(", ")"), ("f.(", ")"), ("[", "]"), ("T[", "]")) for (o, c) in (("(", ")"), ("f(", ")"), ("@f(", ")"), ("f.(", ")"), ("[", "]"), ("T[", "]"))
# single line # single line
@test format_string("$(o)$(sp)$(c)") == "$(o)$(c)" @test format_string("$(o)$(sp)$(c)") == "$(o)$(c)"
@test format_string("$(o)$(sp)$(a)$(sp),$(sp)$(b)$(sp)$(c)") == @test format_string("$(o)$(sp)$(a)$(sp),$(sp)$(b)$(sp)$(c)") ==
@ -737,7 +737,7 @@ end
end end
@testset "leading and trailing newline in multiline listlike" begin @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)") == @test format_string("$(o)a,\nb$(c)") ==
format_string("$(o)\na,\nb$(c)") == format_string("$(o)\na,\nb$(c)") ==
format_string("$(o)\na,\nb\n$(c)") == format_string("$(o)\na,\nb\n$(c)") ==

Loading…
Cancel
Save