From ba6522aced10635b5838185d281c93afbabdcff6 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Sun, 28 Jul 2024 13:57:05 +0200 Subject: [PATCH] Fix indent of multiline vcat with trailing whitespace --- src/runestone.jl | 20 ++++++++++++++++++-- test/runtests.jl | 5 +++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/runestone.jl b/src/runestone.jl index fa13add..4ccbd8d 100644 --- a/src/runestone.jl +++ b/src/runestone.jl @@ -2308,8 +2308,24 @@ function indent_listlike( push!(kids′, kid) accept_node!(ctx, kid) elseif kind(last_leaf(kid)) === K"Whitespace" - # TODO: Testcase? Need to merge here. - @assert false + # TODO: I believe this is only reachable because whitespace isn't trimmed from + # vcat nodes like function calls and tuples etc. + @assert kind(node) in KSet"vcat typed_vcat" + # Since the whitespace is inside the kid node we assume that a newlinews would + # have ended up inside too. We also remove all existing whitespace since it + # would otherwise have to be cleaned up by the trailing whitespace pass. + accept_node!(ctx, kid) + seek(ctx.fmt_io, position(ctx.fmt_io) - span(last_leaf(kid))) + replace_bytes!(ctx, "\n", span(last_leaf(kid))) + nlws = Node(JuliaSyntax.SyntaxHead(K"NewlineWs", JuliaSyntax.TRIVIA_FLAG), 1) + nlws = add_tag(nlws, TAG_PRE_DEDENT) + accept_node!(ctx, nlws) + kid′ = replace_last_leaf(kid, nlws) + if kids′ === kids + kids′ = kids[1:(open_idx - 1)] + end + any_kid_changed = true + push!(kids′, kid′) else # Note that this is a trailing newline and should be put after this item if kids′ === kids diff --git a/test/runtests.jl b/test/runtests.jl index ef06514..2676908 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -687,6 +687,11 @@ end @test format_string("$(t)[\n$(sp)a,\n$(sp)b\n$(sp)]") == "$(t)[\n a,\n b,\n]" @test format_string("$(t)[a b\n$(sp)c d]") == "$(t)[\n a b\n c d\n]" @test format_string("$(t)[\n$(sp)a b\n$(sp)c d\n$(sp)]") == "$(t)[\n a b\n c d\n]" + # vcat + @test format_string("$(t)[$(sp)a b;\nc d;$(sp)]") == + format_string("$(t)[\na b;\nc d;$(sp)]") == + format_string("$(t)[$(sp)a b;\nc d;\n]") == + format_string("$(t)[\na b;\nc d;\n]") == "$(t)[\n a b;\n c d;\n]" end # array comprehension for t in ("", "T")