Browse Source

Fix indent of multiline vcat with trailing whitespace

pull/30/head
Fredrik Ekre 1 year ago
parent
commit
ba6522aced
No known key found for this signature in database
GPG Key ID: DE82E6D5E364C0A2
  1. 20
      src/runestone.jl
  2. 5
      test/runtests.jl

20
src/runestone.jl

@ -2308,8 +2308,24 @@ function indent_listlike(
push!(kids′, kid) push!(kids′, kid)
accept_node!(ctx, kid) accept_node!(ctx, kid)
elseif kind(last_leaf(kid)) === K"Whitespace" elseif kind(last_leaf(kid)) === K"Whitespace"
# TODO: Testcase? Need to merge here. # TODO: I believe this is only reachable because whitespace isn't trimmed from
@assert false # 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 else
# Note that this is a trailing newline and should be put after this item # Note that this is a trailing newline and should be put after this item
if kids′ === kids if kids′ === kids

5
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)[\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)[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]" @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 end
# array comprehension # array comprehension
for t in ("", "T") for t in ("", "T")

Loading…
Cancel
Save