Skip to content

Commit

Permalink
Fix indent of local/global variable lists, fixes #63
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre committed Oct 18, 2024
1 parent 8baaf83 commit 67d9aa6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
24 changes: 22 additions & 2 deletions src/chisels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 67d9aa6

Please sign in to comment.