Browse Source

Indent typed-arrays and getindex nodes, fixes #2.

pull/19/head
Fredrik Ekre 2 years ago
parent
commit
68675cac48
No known key found for this signature in database
GPG Key ID: DE82E6D5E364C0A2
  1. 13
      src/runestone.jl
  2. 9
      test/runtests.jl

13
src/runestone.jl

@ -852,9 +852,10 @@ function indent_newlines_between_indices( @@ -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) @@ -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) @@ -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)

9
test/runtests.jl

@ -430,9 +430,12 @@ end @@ -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"

Loading…
Cancel
Save