Browse Source

Fix commas after trailing macrocalls hidden inside of list items

pull/67/head
Fredrik Ekre 1 year ago
parent
commit
64e836621e
No known key found for this signature in database
GPG Key ID: DE82E6D5E364C0A2
  1. 14
      src/chisels.jl
  2. 8
      src/runestone.jl
  3. 3
      test/runtests.jl

14
src/chisels.jl

@ -630,6 +630,20 @@ function last_leaf_predicate(node::Node, pred::F) where {F}
end end
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) function contains_outer_newline(kids::Vector{Node}, oidx::Int, cidx::Int; recurse = true)
pred = x -> kind(x) === K"NewlineWs" || !JuliaSyntax.is_whitespace(x) pred = x -> kind(x) === K"NewlineWs" || !JuliaSyntax.is_whitespace(x)
for i in (oidx + 1):(cidx - 1) for i in (oidx + 1):(cidx - 1)

8
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 # For parameters the trailing comma is configured from the parent
require_trailing_comma = has_tag(node, TAG_TRAILING_COMMA) require_trailing_comma = has_tag(node, TAG_TRAILING_COMMA)
allow_trailing_comma = has_tag(node, TAG_TRAILING_COMMA_OPT) allow_trailing_comma = has_tag(node, TAG_TRAILING_COMMA_OPT)
elseif n_items > 0 && kind(kids[last_item_idx]) === K"macrocall" && elseif n_items > 0 && predicate_contains(kids[last_item_idx]) do nd
!JuliaSyntax.has_flags(kids[last_item_idx], JuliaSyntax.PARENS_FLAG) && return kind(nd) === K"macrocall" &&
!is_string_macro(kids[last_item_idx]) !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 require_trailing_comma = false
elseif multiline elseif multiline
require_trailing_comma = true require_trailing_comma = true

3
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(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(```\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("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...) # Weird cornercase where a trailing comma messes some cases up (don't recall...)
@test format_string("{\n@f\n}") == "{\n @f\n}" @test format_string("{\n@f\n}") == "{\n @f\n}"
end end

Loading…
Cancel
Save