Skip to content

Commit

Permalink
Spacing in curly, braces, bracescat.
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre committed Jun 19, 2024
1 parent a0c5648 commit 4a63e66
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
17 changes: 11 additions & 6 deletions src/runestone.jl
Original file line number Diff line number Diff line change
Expand Up @@ -293,16 +293,15 @@ end
# TODO: Why did this function become sooo complicated?
function spaces_in_listlike(ctx::Context, node::Node)
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"dotcall" && flags(node) == 0) ||
kind(node) === K"parameters"
(kind(node) === K"dotcall" && flags(node) == 0)
)
return nothing
end
if kind(node) === K"parameters"
# 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

@assert !is_leaf(node)
Expand All @@ -323,6 +322,10 @@ function spaces_in_listlike(ctx::Context, node::Node)
end
closing_leaf_idx = findnext(x -> kind(x) === K")", kids, opening_leaf_idx + 1)::Int
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
@assert kind(node) === K"parameters"
opening_leaf_idx = findfirst(x -> kind(x) === K";", kids)::Int
Expand All @@ -348,6 +351,8 @@ function spaces_in_listlike(ctx::Context, node::Node)
require_trailing_comma = false
if kind(node) === K"tuple" && n_items == 1
require_trailing_comma = true
elseif kind(node) === K"bracescat"
require_trailing_comma = false # Leads to parser error
elseif kind(node) === K"parameters"
# For parameters the trailing comma is configured from the parent
require_trailing_comma = has_tag(node, TAG_TRAILING_COMMA)
Expand Down Expand Up @@ -430,7 +435,7 @@ function spaces_in_listlike(ctx::Context, node::Node)
state = state_after_item(i)
end
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
if before_last_item || expect_trailing_comma
# Nice, just accept it.
Expand Down Expand Up @@ -479,7 +484,7 @@ function spaces_in_listlike(ctx::Context, node::Node)
accept_node!(ctx, kid′)
any_kid_changed && push!(kids′, kid′)
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 findnext(
!JuliaSyntax.is_whitespace, @view(kids[1:closing_leaf_idx - 1]), i + 1,
Expand Down
13 changes: 11 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,15 @@ end
@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)...)"
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

@testset "whitespace around ->" begin
Expand Down Expand Up @@ -535,8 +544,8 @@ end
@test format_string("a <= b >=\n$(sp)c") == "a <= b >=\n c"
# curly braces
@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)}") == "{a,\n b,\n}"
@test format_string("{a,\n$(sp)b\n$(sp)}") ==
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}"
end
end

0 comments on commit 4a63e66

Please sign in to comment.