From a0a5dff33f712f1572408aa12eb7e8e3bc5e76ac Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Thu, 29 Aug 2024 01:31:01 +0200 Subject: [PATCH] Format spacing and indent in global and local lists (#54) Closes #49. --- src/chisels.jl | 7 +++++++ src/runestone.jl | 9 +++++---- test/runtests.jl | 19 +++++++++++++------ 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/chisels.jl b/src/chisels.jl index 6dad995..5798d7f 100644 --- a/src/chisels.jl +++ b/src/chisels.jl @@ -476,6 +476,13 @@ function is_variable_assignment(ctx, node::Node) !(ctx.lineage_kinds[end] in KSet"for generator cartesian_iterator filter") end +# K"global" and K"local" nodes can be either `global a, b, c` or `global a = 1`. This method +# checks whether the node is of the former kind. +function is_global_local_list(node) + return kind(node) in KSet"global local" && !is_leaf(node) && + all(x -> kind(x) in KSet"global local Identifier , Whitespace NewlineWs Comment", verified_kids(node)) +end + function unwrap_to_call_or_tuple(x) is_leaf(x) && return nothing @assert !is_leaf(x) diff --git a/src/runestone.jl b/src/runestone.jl index c2e751a..be89d25 100644 --- a/src/runestone.jl +++ b/src/runestone.jl @@ -913,7 +913,8 @@ function no_spaces_around_x(ctx::Context, node::Node, is_x::F) where {F} end function spaces_in_export_public(ctx::Context, node::Node) - if !(kind(node) in KSet"export public" && !is_leaf(node)) + is_leaf(node) && return nothing + if !(kind(node) in KSet"export public" || is_global_local_list(node)) return nothing end kids = verified_kids(node) @@ -923,7 +924,7 @@ function spaces_in_export_public(ctx::Context, node::Node) spacenode = Node(JuliaSyntax.SyntaxHead(K"Whitespace", JuliaSyntax.TRIVIA_FLAG), 1) - @assert is_leaf(kids[1]) && kind(kids[1]) in KSet"export public" + @assert is_leaf(kids[1]) && kind(kids[1]) in KSet"export public global local" accept_node!(ctx, kids[1]) # space -> identifier -> comma @@ -2753,7 +2754,7 @@ function continue_all_newlines( end function indent_using_import_export_public(ctx::Context, node::Node) - @assert kind(node) in KSet"using import export public" + @assert kind(node) in KSet"using import export public global local" return continue_all_newlines(ctx, node) end @@ -3044,7 +3045,7 @@ function insert_delete_mark_newlines(ctx::Context, node::Node) return indent_braces(ctx, node) elseif kind(node) in KSet"|| &&" return indent_short_circuit(ctx, node) - elseif kind(node) in KSet"using import export public" + elseif kind(node) in KSet"using import export public" || is_global_local_list(node) return indent_using_import_export_public(ctx, node) elseif is_variable_assignment(ctx, node) return indent_assignment(ctx, node) diff --git a/test/runtests.jl b/test/runtests.jl index a1a91df..fa4539d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -784,9 +784,11 @@ end @test format_string("$(verb) A: a,\n$(sp)b") == "$(verb) A: a,\n b" @test format_string("$(verb) A:\n$(sp)a,\n$(sp)b") == "$(verb) A:\n a,\n b" end - # export - @test format_string("export a,\n$(sp)b") == "export a,\n b" - @test format_string("export\n$(sp)a,\n$(sp)b") == "export\n a,\n b" + # export/public/global/local + for verb in ("export", "public", "global", "local") + @test format_string("$(verb) a,\n$(sp)b") == "$(verb) a,\n b" + @test format_string("$(verb)\n$(sp)a,\n$(sp)b") == "$(verb)\n a,\n b" + end # ternary @test format_string("a ?\n$(sp)b : c") == "a ?\n b : c" @test format_string("a ? b :\n$(sp)c") == "a ? b :\n c" @@ -917,8 +919,13 @@ end @test format_string("import A.@a as @b") == "import A.@a as @b" end -@testset "spaces in export/public" begin - for sp in ("", " ", " ", "\t"), verb in ("export", "public"), (a, b) in (("a", "@b"), ("@a", "b")) +@testset "spaces in export/public/global/local" begin + for sp in ("", " ", " ", "\t"), verb in ("export", "public", "global", "local"), + (a, b) in (("a", "b"), ("a", "@b"), ("@a", "b")) + if verb in ("global", "local") && (a, b) != ("a", "b") + # global and local only support K"Identifier"s right now + continue + end @test format_string("$(verb) $(sp)$(a)") == "$(verb) $(a)" @test format_string("$(verb)\n$(a)") == "$(verb)\n $(a)" @test format_string("$(verb) $(sp)$(a)$(sp),$(sp)$(b)") == "$(verb) $(a), $(b)" @@ -926,7 +933,7 @@ end @test format_string("$(verb) \n$(a)$(sp),\n$(b)") == "$(verb)\n $(a),\n $(b)" @test format_string("$(verb) $(a)$(sp),\n# b\n$(b)") == "$(verb) $(a),\n # b\n $(b)" # Inline comments - @test format_string("export a$(sp),$(sp)#= b, =#$(sp)c") == "export a, #= b, =# c" + @test format_string("$(verb) a$(sp),$(sp)#= b, =#$(sp)c") == "$(verb) a, #= b, =# c" end # Interpolated identifiers (currently only expected in K"quote" and K"macrocall") @test format_string(":(export \$a)") == ":(export \$a)"