From 68675cac4847b577ee5f98713edd96140260d1fd Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Thu, 13 Jun 2024 12:18:39 +0200 Subject: [PATCH] Indent typed-arrays and getindex nodes, fixes #2. --- src/runestone.jl | 13 +++++++------ test/runtests.jl | 9 ++++++--- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/runestone.jl b/src/runestone.jl index 4acb13d..61bb33d 100644 --- a/src/runestone.jl +++ b/src/runestone.jl @@ -852,9 +852,10 @@ function indent_newlines_between_indices( this_kid_changed = true end # NewlineWs nodes can also hide as the first or last leaf of a node, tag'em. - # TODO: The trailing argument should maybe depend on `indent_closing_token` or - # possibly set to `false` if `i == close_idx - 1`? - kid′ = continue_newlines(kid; leading = true, trailing = true) + # Skip trailing newline of this kid if the next token is the closing one and the + # closing token should not be indented. + trailing = !(i == close_idx - 1 && !indent_closing_token) + kid′ = continue_newlines(kid; leading = true, trailing = trailing) if kid′ !== nothing kid = kid′ this_kid_changed = true @@ -1091,10 +1092,10 @@ function indent_quote(ctx::Context, node::Node) end end +# Literal array nodes and also ref-nodes (which can be either a typed-array or a getindex) function indent_array(ctx::Context, node::Node) - @assert kind(node) in KSet"vect vcat ncat" + @assert kind(node) in KSet"vect vcat typed_vcat ncat ref" kids = verified_kids(node) - any_kid_changed = false opening_bracket_idx = findfirst(x -> kind(x) === K"[", kids)::Int closing_bracket_idx = findnext(x -> kind(x) === K"]", kids, opening_bracket_idx + 1)::Int return indent_newlines_between_indices( @@ -1153,7 +1154,7 @@ function insert_delete_mark_newlines(ctx::Context, node::Node) return indent_do(ctx, node) elseif is_paren_block(node) return indent_paren_block(ctx, node) - elseif kind(node) in KSet"vect vcat ncat" + elseif kind(node) in KSet"vect vcat typed_vcat ncat ref" return indent_array(ctx, node) elseif kind(node) in KSet"row" return indent_array_row(ctx, node) diff --git a/test/runtests.jl b/test/runtests.jl index 44b4983..321a628 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -430,9 +430,12 @@ end # paren-block @test format_string("(a;\n$(sp)b)") == "(a;\n b)" # array literals - @test format_string("[a,\n$(sp)b]") == "[a,\n b]" - @test format_string("[\n$(sp)a,\n$(sp)b\n$(sp)]") == "[\n a,\n b\n]" - @test format_string("[a b\n$(sp)c d]") == "[a b\n c d]" + for t in ("", "T") + @test format_string("$(t)[a,\n$(sp)b]") == "$(t)[a,\n b]" + @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)[a b\n c d]" + @test format_string("$(t)[\n$(sp)a b\n$(sp)c d\n$(sp)]") == "$(t)[\n a b\n c d\n]" + end # comparison @test format_string("a == b ==\n$(sp)c") == "a == b ==\n c" @test format_string("a <= b >=\n$(sp)c") == "a <= b >=\n c"