From 4a63e66a124fa3d13520e40e8730bc81c61984b4 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Thu, 20 Jun 2024 01:32:09 +0200 Subject: [PATCH] Spacing in curly, braces, bracescat. --- src/runestone.jl | 17 +++++++++++------ test/runtests.jl | 13 +++++++++++-- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/runestone.jl b/src/runestone.jl index e1a95a6..671b3a6 100644 --- a/src/runestone.jl +++ b/src/runestone.jl @@ -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) @@ -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 @@ -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) @@ -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. @@ -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, diff --git a/test/runtests.jl b/test/runtests.jl index e3cb112..e9c567c 100644 --- a/test/runtests.jl +++ b/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)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 @@ -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