Browse Source

Spacing in curly, braces, bracescat.

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

17
src/runestone.jl

@ -293,16 +293,15 @@ end
# TODO: Why did this function become sooo complicated? # TODO: Why did this function become sooo complicated?
function spaces_in_listlike(ctx::Context, node::Node) function spaces_in_listlike(ctx::Context, node::Node)
if !( if !(
kind(node) === K"tuple" || kind(node) in KSet"tuple parameters curly braces bracescat" ||
(kind(node) === K"call" && flags(node) == 0) || # Flag check rules out op-calls (kind(node) === K"call" && flags(node) == 0) || # Flag check rules out op-calls
(kind(node) === K"dotcall" && flags(node) == 0) || (kind(node) === K"dotcall" && flags(node) == 0)
kind(node) === K"parameters"
) )
return nothing return nothing
end end
if kind(node) === K"parameters" if kind(node) === K"parameters"
# TODO: Can probably show up elsewhere but... # TODO: Can probably show up elsewhere but...
@assert ctx.lineage_kinds[end] in KSet"tuple call dotcall" @assert ctx.lineage_kinds[end] in KSet"tuple call dotcall curly"
end end
@assert !is_leaf(node) @assert !is_leaf(node)
@ -323,6 +322,10 @@ function spaces_in_listlike(ctx::Context, node::Node)
end end
closing_leaf_idx = findnext(x -> kind(x) === K")", kids, opening_leaf_idx + 1)::Int closing_leaf_idx = findnext(x -> kind(x) === K")", kids, opening_leaf_idx + 1)::Int
closing_leaf_idx == opening_leaf_idx + 1 && return nothing # empty closing_leaf_idx == opening_leaf_idx + 1 && return nothing # empty
elseif kind(node) in KSet"curly braces bracescat"
opening_leaf_idx = findfirst(x -> kind(x) === K"{", kids)::Int
closing_leaf_idx = findnext(x -> kind(x) === K"}", kids, opening_leaf_idx + 1)::Int
closing_leaf_idx == opening_leaf_idx + 1 && return nothing # empty
else else
@assert kind(node) === K"parameters" @assert kind(node) === K"parameters"
opening_leaf_idx = findfirst(x -> kind(x) === K";", kids)::Int opening_leaf_idx = findfirst(x -> kind(x) === K";", kids)::Int
@ -348,6 +351,8 @@ function spaces_in_listlike(ctx::Context, node::Node)
require_trailing_comma = false require_trailing_comma = false
if kind(node) === K"tuple" && n_items == 1 if kind(node) === K"tuple" && n_items == 1
require_trailing_comma = true require_trailing_comma = true
elseif kind(node) === K"bracescat"
require_trailing_comma = false # Leads to parser error
elseif kind(node) === K"parameters" elseif kind(node) === K"parameters"
# For parameters the trailing comma is configured from the parent # For parameters the trailing comma is configured from the parent
require_trailing_comma = has_tag(node, TAG_TRAILING_COMMA) require_trailing_comma = has_tag(node, TAG_TRAILING_COMMA)
@ -430,7 +435,7 @@ function spaces_in_listlike(ctx::Context, node::Node)
state = state_after_item(i) state = state_after_item(i)
end end
elseif state === :expect_comma elseif state === :expect_comma
if kind(kid′) === K"," if kind(kid′) === K"," || (kind(kid′) === K";" && kind(node) === K"bracescat")
before_last_item = i < last_item_idx before_last_item = i < last_item_idx
if before_last_item || expect_trailing_comma if before_last_item || expect_trailing_comma
# Nice, just accept it. # Nice, just accept it.
@ -479,7 +484,7 @@ function spaces_in_listlike(ctx::Context, node::Node)
accept_node!(ctx, kid′) accept_node!(ctx, kid′)
any_kid_changed && push!(kids′, kid′) any_kid_changed && push!(kids′, kid′)
elseif kind(kid′) === K"parameters" elseif kind(kid′) === K"parameters"
@assert kind(node) in KSet"call dotcall" # TODO: Can this happen for named tuples? @assert kind(node) in KSet"call dotcall curly" # TODO: Can this happen for named tuples?
@assert i === last_item_idx @assert i === last_item_idx
@assert findnext( @assert findnext(
!JuliaSyntax.is_whitespace, @view(kids[1:closing_leaf_idx - 1]), i + 1, !JuliaSyntax.is_whitespace, @view(kids[1:closing_leaf_idx - 1]), i + 1,

13
test/runtests.jl

@ -253,6 +253,15 @@ end
@test format_string("f($(sp)a$(sp)...,$(sp))") == "f(a$(sp)...)" @test format_string("f($(sp)a$(sp)...,$(sp))") == "f(a$(sp)...)"
@test format_string("f($(sp)a$(sp)...;$(sp)b$(sp)...$(sp))") == "f(a$(sp)...; b$(sp)...)" @test format_string("f($(sp)a$(sp)...;$(sp)b$(sp)...$(sp))") == "f(a$(sp)...; b$(sp)...)"
end end
# Curly (not as extensive testing as tuple/call/dotcall above but the code path is the
# same)
for x in ("", "X"), sp in ("", " ", " "), a in ("A", "<:B", "C <: D"), b in ("E", "<:F", "G <: H")
tr = x == "" ? "" : ","
@test format_string("$(x){$(sp)$(a)$(sp),$(sp)$(b)$(sp)}") == "$(x){$(a), $(b)}"
@test format_string("$(x){$(sp)$(a)$(sp);$(sp)$(b)$(sp)}") == "$(x){$(a); $(b)}"
@test format_string("$(x){$(sp)$(a)$(sp);$(sp)$(b)$(sp)}") == "$(x){$(a); $(b)}"
@test format_string("$(x){\n$(sp)$(a)$(sp);$(sp)$(b)$(sp)\n}") == "$(x){\n$(a); $(b)$(tr)\n}"
end
end end
@testset "whitespace around ->" begin @testset "whitespace around ->" begin
@ -535,8 +544,8 @@ end
@test format_string("a <= b >=\n$(sp)c") == "a <= b >=\n c" @test format_string("a <= b >=\n$(sp)c") == "a <= b >=\n c"
# curly braces # curly braces
@test format_string("{a,\n$(sp)b}") == "{a,\n b}" @test format_string("{a,\n$(sp)b}") == "{a,\n b}"
@test format_string("{a,\n$(sp)b\n$(sp)}") == "{a,\n b\n}" @test format_string("{a,\n$(sp)b\n$(sp)}") ==
@test format_string("{a,\n$(sp)b,\n$(sp)}") == "{a,\n b,\n}" format_string("{a,\n$(sp)b,\n$(sp)}") == "{a,\n b,\n}"
@test format_string("{\n$(sp)a,\n$(sp)b,\n$(sp)}") == "{\n a,\n b,\n}" @test format_string("{\n$(sp)a,\n$(sp)b,\n$(sp)}") == "{\n a,\n b,\n}"
end end
end end

Loading…
Cancel
Save