Browse Source

Fix indent of long form functor definitions

pull/26/head
Fredrik Ekre 1 year ago
parent
commit
88e6560700
No known key found for this signature in database
GPG Key ID: DE82E6D5E364C0A2
  1. 15
      src/chisels.jl
  2. 6
      src/runestone.jl
  3. 3
      test/runtests.jl

15
src/chisels.jl

@ -343,6 +343,21 @@ function is_longform_anon_function(node::Node) @@ -343,6 +343,21 @@ function is_longform_anon_function(node::Node)
end
end
function is_longform_functor(node::Node)
is_leaf(node) && return false
kind(node) === K"function" || return false
kids = verified_kids(node)
kw = findfirst(x -> kind(x) === K"function", kids)
@assert kw !== nothing
calli = findnext(x -> !JuliaSyntax.is_whitespace(x), kids, kw + 1)::Int
call = kids[calli]
if !is_leaf(call) && kind(call) == K"call" &&
kind(first(verified_kids(call))) === K"parens"
return true
end
return false
end
# Just like `JuliaSyntax.is_infix_op_call`, but also check that the node is K"call" or
# K"dotcall"
function is_infix_op_call(node::Node)

6
src/runestone.jl

@ -1324,7 +1324,7 @@ function spaces_around_keywords(ctx::Context, node::Node) @@ -1324,7 +1324,7 @@ function spaces_around_keywords(ctx::Context, node::Node)
@assert false # Unreachable?
else
# Reachable in e.g. `T where{T}`, `if(`, ... insert space
@assert kind(node) in KSet"where if elseif while do"
@assert kind(node) in KSet"where if elseif while do function"
any_changes = true
if kids′ === kids
kids′ = kids[1:(i - 1)]
@ -1735,7 +1735,7 @@ function indent_function_or_macro(ctx::Context, node::Node) @@ -1735,7 +1735,7 @@ function indent_function_or_macro(ctx::Context, node::Node)
any_kid_changed = true
end
# Second node is the space between keyword and name
if !is_longform_anon_function(node)
if !(is_longform_anon_function(node) || is_longform_functor(node))
space_idx = 2
space_node = kids[space_idx]
@assert is_leaf(space_node) && kind(space_node) === K"Whitespace"
@ -1745,7 +1745,7 @@ function indent_function_or_macro(ctx::Context, node::Node) @@ -1745,7 +1745,7 @@ function indent_function_or_macro(ctx::Context, node::Node)
sig_idx = findnext(x -> !JuliaSyntax.is_whitespace(x), kids, func_idx + 1)::Int
if sig_idx == 2
# Only case where no space is needed after the keyword
@assert is_longform_anon_function(node)
@assert is_longform_anon_function(node) || is_longform_functor(node)
end
sig_node = kids[sig_idx]
# Identifier for regular names but "not function call" for empty functions with Unicode

3
test/runtests.jl

@ -690,6 +690,9 @@ end @@ -690,6 +690,9 @@ end
@test format_string("begin x\n$(sp)end") == "begin x\nend"
@test format_string("begin x end") == "begin x end"
@test format_string("begin\n x end") == "begin\n x end"
# Functors
@test format_string("function$(sp)(a::A)(b)\nx\nend") ==
"function (a::A)(b)\n x\nend"
end
end

Loading…
Cancel
Save