Browse Source

Fix function indent with unusual infix definitions

fe/perf
Fredrik Ekre 1 year ago
parent
commit
3ee9531e4d
No known key found for this signature in database
GPG Key ID: DE82E6D5E364C0A2
  1. 6
      src/chisels.jl
  2. 4
      src/runestone.jl
  3. 3
      test/runtests.jl

6
src/chisels.jl

@ -316,7 +316,7 @@ end
function unwrap_to_call_or_tuple(x) function unwrap_to_call_or_tuple(x)
is_leaf(x) && return nothing is_leaf(x) && return nothing
@assert !is_leaf(x) @assert !is_leaf(x)
if kind(x) in KSet"call tuple" if kind(x) in KSet"call tuple parens"
return x return x
end end
xkids = verified_kids(x) xkids = verified_kids(x)
@ -324,6 +324,8 @@ function unwrap_to_call_or_tuple(x)
return unwrap_to_call_or_tuple(xkids[xi]) return unwrap_to_call_or_tuple(xkids[xi])
end 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) function is_longform_anon_function(node::Node)
is_leaf(node) && return false is_leaf(node) && return false
kind(node) === K"function" || return false kind(node) === K"function" || return false
@ -336,7 +338,7 @@ function is_longform_anon_function(node::Node)
if maybe_tuple === nothing if maybe_tuple === nothing
return false return false
else else
return kind(maybe_tuple) === K"tuple" return kind(maybe_tuple) in KSet"tuple parens"
end end
end end

4
src/runestone.jl

@ -1760,7 +1760,7 @@ function indent_function_or_macro(ctx::Context, node::Node)
sig_node = kids[sig_idx] sig_node = kids[sig_idx]
# Identifier for regular names but "not function call" for empty functions with Unicode # Identifier for regular names but "not function call" for empty functions with Unicode
# symbols?? # 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`. # Empty function definition like `function f end`.
# TODO: Make sure the spaces around are correct # TODO: Make sure the spaces around are correct
end_idx = findnext(x -> kind(x) === K"end", kids, sig_idx + 1)::Int end_idx = findnext(x -> kind(x) === K"end", kids, sig_idx + 1)::Int
@ -1773,7 +1773,7 @@ function indent_function_or_macro(ctx::Context, node::Node)
return any_kid_changed ? node : nothing return any_kid_changed ? node : nothing
end end
# K"tuple" when this is an anonymous function # 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. # Fourth node is the function/macro body block.
block_idx = sig_idx + 1 block_idx = sig_idx + 1
block_node′ = indent_block(ctx, kids[block_idx]) block_node′ = indent_block(ctx, kids[block_idx])

3
test/runtests.jl

@ -717,6 +717,9 @@ end
# Functors # Functors
@test format_string("function$(sp)(a::A)(b)\nx\nend") == @test format_string("function$(sp)(a::A)(b)\nx\nend") ==
"function (a::A)(b)\n x\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 # Multiline strings inside lists
for trip in ("\"\"\"", "```") for trip in ("\"\"\"", "```")
@test format_string("println(io, $(trip)\n$(sp)a\n$(sp)\n$(sp)b\n$(sp)$(trip))") == @test format_string("println(io, $(trip)\n$(sp)a\n$(sp)\n$(sp)b\n$(sp)$(trip))") ==

Loading…
Cancel
Save