Skip to content

Commit

Permalink
Fix trailing comma in parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre committed Jul 19, 2024
1 parent afa4252 commit 2dfe195
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/chisels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ function stringify_tags(node::Node)
if has_tag(node, TAG_LINE_CONT)
write(io, "line-cont.,")
end
if has_tag(node, TAG_TRAILING_COMMA)
write(io, "trail-comma.,")
end
truncate(io, max(0, position(io) - 1)) # Remove trailing comma
return String(take!(io))
end
Expand Down
16 changes: 15 additions & 1 deletion src/runestone.jl
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,12 @@ function spaces_in_listlike(ctx::Context, node::Node)
if i < last_item_idx
return :expect_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
return :expect_closing
end
Expand Down Expand Up @@ -484,6 +489,12 @@ function spaces_in_listlike(ctx::Context, node::Node)
kid′ = replace_last_leaf(kid′, nullnode)
this_kid_changed = true
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 &&
count(
x -> !(JuliaSyntax.is_whitespace(x) || kind(x) in KSet", ;"),
Expand Down Expand Up @@ -742,6 +753,9 @@ function spaces_in_listlike(ctx::Context, node::Node)
end
if state !== :expect_closing
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
@assert require_trailing_comma
any_kid_changed = true
Expand Down

0 comments on commit 2dfe195

Please sign in to comment.