Skip to content

Commit

Permalink
Format spacing and indent in global and local lists (#54)
Browse files Browse the repository at this point in the history
Closes #49.
  • Loading branch information
fredrikekre authored Aug 28, 2024
1 parent 7bceb1c commit a0a5dff
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
7 changes: 7 additions & 0 deletions src/chisels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions src/runestone.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
19 changes: 13 additions & 6 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -917,16 +919,21 @@ 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)"
@test format_string("$(verb) $(a)$(sp),\n$(b)") == "$(verb) $(a),\n $(b)"
@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)"
Expand Down

0 comments on commit a0a5dff

Please sign in to comment.