Skip to content

Commit 50c8956

Browse files
authored
[REPL] Fix keyword arguments completions with do block (#59123)
In `_complete_methods`, desugar the `:do` Expr into a call with a lambda in the first argument. Fixes #58833.
1 parent f2c2837 commit 50c8956

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

stdlib/REPL/src/REPLCompletions.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,15 @@ code_typed(CC.typeinf, (REPLInterpreter, CC.InferenceState))
692692
MAX_METHOD_COMPLETIONS::Int = 40
693693
function _complete_methods(ex_org::Expr, context_module::Module, shift::Bool)
694694
isempty(ex_org.args) && return 2, nothing, [], Set{Symbol}()
695+
# Desugar do block call into call with lambda
696+
if ex_org.head === :do && length(ex_org.args) >= 2
697+
ex_call = ex_org.args[1]
698+
ex_args = [x for x in ex_call.args if !(x isa Expr && x.head === :parameters)]
699+
ex_params = findfirst(x -> x isa Expr && x.head === :parameters, ex_call.args)
700+
new_args = [ex_args[1], ex_org.args[end], ex_args[2:end]...]
701+
ex_params !== nothing && push!(new_args, ex_call.args[ex_params])
702+
ex_org = Expr(:call, new_args...)
703+
end
695704
funct = repl_eval_ex(ex_org.args[1], context_module)
696705
funct === nothing && return 2, nothing, [], Set{Symbol}()
697706
funct = CC.widenconst(funct)

stdlib/REPL/test/replcompletions.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ let ex =
139139
kwtest4(a::SubString; x23, _something) = pass
140140
kwtest5(a::Int, b, x...; somekwarg, somekotherkwarg) = pass
141141
kwtest5(a::Char, b; xyz) = pass
142+
kwtest6(f::Function, arg1; somekwarg) = pass
142143

143144
const named = (; len2=3)
144145
const fmsoebelkv = (; len2=3)
@@ -198,6 +199,8 @@ test_scomplete(s) = map_completion_text(@inferred(shell_completions(s, lastinde
198199
test_complete_pos(s) = map_completion_text(@inferred(completions(replace(s, '|' => ""), findfirst('|', s)-1)))
199200
test_complete_context(s, m=@__MODULE__; shift::Bool=true) =
200201
map_completion_text(@inferred(completions(s,lastindex(s), m, shift)))
202+
test_complete_context_pos(s, m=@__MODULE__; shift::Bool=true) =
203+
map_completion_text(@inferred(completions(replace(s, '|' => ""), findfirst('|', s)-1, m, shift)))
201204
test_complete_foo(s; shift::Bool=true) = test_complete_context(s, Main.CompletionFoo; shift)
202205
test_complete_noshift(s) = map_completion_text(@inferred(completions(s, lastindex(s), Main, false)))
203206

@@ -2742,3 +2745,10 @@ let s = "for"
27422745
@test "foreach" in c
27432746
@test !("rand" in c)
27442747
end
2748+
2749+
# #58833 - Autocompletion of keyword arguments with do-blocks is broken
2750+
let s = "kwtest6(123; som|) do x; x + 3 end"
2751+
c, r = test_complete_context_pos(s, Main.CompletionFoo)
2752+
@test "somekwarg=" in c
2753+
@test r == 14:16
2754+
end

0 commit comments

Comments
 (0)