Skip to content

Commit 147403a

Browse files
fredrikekreclaude
andauthored
Some fixes after JuliaSyntax upgrade (#188)
* Fix indent_listlike for K"call" nodes with a trailing K"do" child in JuliaSyntax v1 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * Fix removal of trailing comma in single-tuple-item lambda parameters (e.g. ((a, b),) -> a) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent c89356c commit 147403a

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

src/runestone.jl

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,14 @@ function spaces_in_listlike(ctx::Context, node::Node)
340340
allow_trailing_semi = false
341341
allow_trailing_comma = multiline
342342
if kind(node) in KSet"call dotcall macrocall" ||
343-
(kind(node) === K"tuple" && ctx.lineage_kinds[end] === K"->" && ctx.next_sibling !== nothing)
343+
(
344+
kind(node) === K"tuple" && ctx.lineage_kinds[end] === K"->" && ctx.next_sibling !== nothing &&
345+
!(n_items == 1 && kind(kids[first_item_idx::Int]) === K"tuple")
346+
)
344347
# For calls and anonymous function argument tuples, trailing commas are never required
345348
# (and only allowed if multiline, same as regular function definitions).
349+
# Exception: `((a, b),) -> body` — when the single item is itself a tuple the outer
350+
# trailing comma must be kept, otherwise `((a, b))` is parsed as `(a, b)` (2 args).
346351
require_trailing_comma = false
347352
elseif n_items > 0 && kind(kids[last_item_idx::Int]) === K"generator"
348353
# https://github.com/fredrikekre/Runic.jl/issues/151
@@ -2514,8 +2519,17 @@ function indent_listlike(
25142519
end
25152520
any_kid_changed && push!(kids′, kid)
25162521
accept_node!(ctx, kid)
2517-
# Keep any remaining kids
2518-
@assert close_idx == lastindex(kids)
2522+
# Keep remaining kids. In JuliaSyntax v1, do-block calls are represented as K"call"
2523+
# nodes with a trailing K"do" child after the closing paren, so close_idx may not be the
2524+
# last index.
2525+
if close_idx < lastindex(kids)
2526+
@assert kind(node) in KSet"call dotcall" && kind(kids[end]) === K"do"
2527+
if any_kid_changed
2528+
for i in (close_idx + 1):lastindex(kids)
2529+
push!(kids′, kids[i])
2530+
end
2531+
end
2532+
end
25192533
# Reset stream
25202534
seek(ctx.fmt_io, pos)
25212535
# Make a new node and return

test/runtests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,10 @@ end
453453
@test format_string("(@a(b), d) -> c") == "(@a(b), d) -> c"
454454
@test format_string("x -> (x,)") == "x -> (x,)"
455455
@test format_string("(x,) -> (x,)") == "(x) -> (x,)"
456+
# Trailing comma is semantically significant when the single item is a tuple pattern:
457+
# `((a, b),) -> a` is a 1-arg destructuring lambda; removing the comma gives a 2-arg lambda.
458+
@test format_string("((a, b),) -> a") == "((a, b),) -> a"
459+
@test format_string("((a, b, c),) -> a") == "((a, b, c),) -> a"
456460
@test format_string("x -> (x)") == "x -> (x)"
457461
# keyword-style anonymous functions: same trailing comma rules as -> style
458462
@test format_string("function (a)\n a\nend") == "function (a)\n return a\nend"
@@ -771,6 +775,9 @@ end
771775
# do-end
772776
@test format_string("open() do\n$(sp)a\n$(sp)end") == "open() do\n a\nend"
773777
@test format_string("open() do io\n$(sp)a\n$(sp)end") == "open() do io\n a\nend"
778+
# do-end with multiline argument list
779+
@test format_string("f(\n$(sp)a,\n$(sp)b,\n) do x\n$(sp)y\n$(sp)end") ==
780+
"f(\n a,\n b,\n) do x\n y\nend"
774781
# module-end, baremodule-end
775782
for b in ("", "bare")
776783
# Just a module

0 commit comments

Comments
 (0)