From 67d9aa62328b1694f07f00974130cd1c41a93d57 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Fri, 18 Oct 2024 17:05:22 +0200 Subject: [PATCH] Fix indent of local/global variable lists, fixes #63 --- src/chisels.jl | 24 ++++++++++++++++++++++-- test/runtests.jl | 6 +++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/chisels.jl b/src/chisels.jl index ef8f759..0ca817c 100644 --- a/src/chisels.jl +++ b/src/chisels.jl @@ -505,8 +505,28 @@ 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)) + if !(kind(node) in KSet"global local" && !is_leaf(node)) + return false + end + kids = verified_kids(node) + # If it contains assignments it is not a list + if any(x -> is_assignment(x), kids) + return false + end + # If it contains K"," it is a list + if any(x -> kind(x) === K",", kids) + return true + end + # If we reach here we have a single item list (`local a`) or something like + # ``` + # global function f() + # # ... + # end + # ``` + # For now we only say it is a list if the item is in the subset below + idx = findfirst(x -> kind(x) in KSet"global local", kids)::Int + idx = findnext(x -> !JuliaSyntax.is_whitespace(x), kids, idx + 1)::Int + return kind(kids[idx]) in KSet"Identifier var" end function unwrap_to_call_or_tuple(x) diff --git a/test/runtests.jl b/test/runtests.jl index 219b137..85293e5 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -802,9 +802,9 @@ end @test format_string("$(verb) A:\n$(sp)a,\n$(sp)b") == "$(verb) A:\n a,\n b" end # 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" + for verb in ("export", "public", "global", "local"), b in ("b", "var\"b\"") + @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"