Skip to content

Commit

Permalink
Fix function indent with unusual infix definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre committed Aug 3, 2024
1 parent f0a6d32 commit 3ee9531
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/chisels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -316,14 +316,16 @@ end
function unwrap_to_call_or_tuple(x)
is_leaf(x) && return nothing
@assert !is_leaf(x)
if kind(x) in KSet"call tuple"
if kind(x) in KSet"call tuple parens"
return x
end
xkids = verified_kids(x)
xi = findfirst(x -> !JuliaSyntax.is_whitespace(x), xkids)::Int
return unwrap_to_call_or_tuple(xkids[xi])
end

# TODO: This should be reworked to be more specific, in particular K"parens" is maybe not
# correct (found in e.g. `function(x * b)\n\nend`).
function is_longform_anon_function(node::Node)
is_leaf(node) && return false
kind(node) === K"function" || return false
Expand All @@ -336,7 +338,7 @@ function is_longform_anon_function(node::Node)
if maybe_tuple === nothing
return false
else
return kind(maybe_tuple) === K"tuple"
return kind(maybe_tuple) in KSet"tuple parens"
end
end

Expand Down
4 changes: 2 additions & 2 deletions src/runestone.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1760,7 +1760,7 @@ function indent_function_or_macro(ctx::Context, node::Node)
sig_node = kids[sig_idx]
# Identifier for regular names but "not function call" for empty functions with Unicode
# symbols??
if kind(sig_node) === K"Identifier" || !(kind(sig_node) in KSet"call where :: tuple")
if kind(sig_node) === K"Identifier" || !(kind(sig_node) in KSet"call where :: tuple parens")
# Empty function definition like `function f end`.
# TODO: Make sure the spaces around are correct
end_idx = findnext(x -> kind(x) === K"end", kids, sig_idx + 1)::Int
Expand All @@ -1773,7 +1773,7 @@ function indent_function_or_macro(ctx::Context, node::Node)
return any_kid_changed ? node : nothing
end
# K"tuple" when this is an anonymous function
@assert !is_leaf(sig_node) && kind(sig_node) in KSet"call where :: tuple"
@assert !is_leaf(sig_node) && kind(sig_node) in KSet"call where :: tuple parens"
# Fourth node is the function/macro body block.
block_idx = sig_idx + 1
block_node′ = indent_block(ctx, kids[block_idx])
Expand Down
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,9 @@ end
# Functors
@test format_string("function$(sp)(a::A)(b)\nx\nend") ==
"function (a::A)(b)\n x\nend"
# TODO: Spaces after function keyword isn't removed.
@test format_string("function$(sp)(a * b)\nreturn\nend") ==
"function$(sp)(a * b)\n return\nend"
# Multiline strings inside lists
for trip in ("\"\"\"", "```")
@test format_string("println(io, $(trip)\n$(sp)a\n$(sp)\n$(sp)b\n$(sp)$(trip))") ==
Expand Down

0 comments on commit 3ee9531

Please sign in to comment.