From 88e656070008befe6d8f0a02fe8be44e0f2dc5eb Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Thu, 25 Jul 2024 02:28:16 +0200 Subject: [PATCH] Fix indent of long form functor definitions --- src/chisels.jl | 15 +++++++++++++++ src/runestone.jl | 6 +++--- test/runtests.jl | 3 +++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/chisels.jl b/src/chisels.jl index d264e1c..5cb2531 100644 --- a/src/chisels.jl +++ b/src/chisels.jl @@ -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) diff --git a/src/runestone.jl b/src/runestone.jl index b4702ec..4221897 100644 --- a/src/runestone.jl +++ b/src/runestone.jl @@ -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) 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) 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 diff --git a/test/runtests.jl b/test/runtests.jl index 0ac6e29..e40bf02 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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