Browse Source

Fix trailing comma in parameters

pull/19/head
Fredrik Ekre 1 year ago
parent
commit
2dfe195f86
No known key found for this signature in database
GPG Key ID: DE82E6D5E364C0A2
  1. 3
      src/chisels.jl
  2. 16
      src/runestone.jl

3
src/chisels.jl

@ -177,6 +177,9 @@ function stringify_tags(node::Node)
if has_tag(node, TAG_LINE_CONT) if has_tag(node, TAG_LINE_CONT)
write(io, "line-cont.,") write(io, "line-cont.,")
end end
if has_tag(node, TAG_TRAILING_COMMA)
write(io, "trail-comma.,")
end
truncate(io, max(0, position(io) - 1)) # Remove trailing comma truncate(io, max(0, position(io) - 1)) # Remove trailing comma
return String(take!(io)) return String(take!(io))
end end

16
src/runestone.jl

@ -410,7 +410,12 @@ function spaces_in_listlike(ctx::Context, node::Node)
if i < last_item_idx if i < last_item_idx
return :expect_comma return :expect_comma
elseif i == last_item_idx && require_trailing_comma elseif i == last_item_idx && require_trailing_comma
return :expect_comma if kind(kids[last_item_idx]) === K"parameters"
# If the last kid is K"parameters" it will handle a trailing comma
return :expect_closing
else
return :expect_comma
end
else else
return :expect_closing return :expect_closing
end end
@ -484,6 +489,12 @@ function spaces_in_listlike(ctx::Context, node::Node)
kid′ = replace_last_leaf(kid′, nullnode) kid′ = replace_last_leaf(kid′, nullnode)
this_kid_changed = true this_kid_changed = true
end end
if kind(kid′) === K"parameters" && require_trailing_comma &&
i == last_item_idx && !has_tag(kid′, TAG_TRAILING_COMMA)
# Tag the node to require a trailing comma
kid′ = add_tag(kid′, TAG_TRAILING_COMMA)
this_kid_changed = true
end
if kind(kid′) === K"parameters" && !require_trailing_comma && !is_named_tuple && if kind(kid′) === K"parameters" && !require_trailing_comma && !is_named_tuple &&
count( count(
x -> !(JuliaSyntax.is_whitespace(x) || kind(x) in KSet", ;"), x -> !(JuliaSyntax.is_whitespace(x) || kind(x) in KSet", ;"),
@ -742,6 +753,9 @@ function spaces_in_listlike(ctx::Context, node::Node)
end end
if state !== :expect_closing if state !== :expect_closing
if state === :expect_comma if state === :expect_comma
# K"parameters" should aleays handle the trailing comma and got to
# :expect_closing directly
@assert kind(kids[last_item_idx]) !== K"parameters"
# Need to add a trailing comma if it is expected # Need to add a trailing comma if it is expected
@assert require_trailing_comma @assert require_trailing_comma
any_kid_changed = true any_kid_changed = true

Loading…
Cancel
Save