Skip to content

Commit 7232ce1

Browse files
xal-0KristofferC
authored andcommitted
[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. (cherry picked from commit 50c8956)
1 parent 00fc6bb commit 7232ce1

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
@@ -698,6 +698,15 @@ code_typed(CC.typeinf, (REPLInterpreter, CC.InferenceState))
698698
MAX_METHOD_COMPLETIONS::Int = 40
699699
function _complete_methods(ex_org::Expr, context_module::Module, shift::Bool)
700700
isempty(ex_org.args) && return 2, nothing, [], Set{Symbol}()
701+
# Desugar do block call into call with lambda
702+
if ex_org.head === :do && length(ex_org.args) >= 2
703+
ex_call = ex_org.args[1]
704+
ex_args = [x for x in ex_call.args if !(x isa Expr && x.head === :parameters)]
705+
ex_params = findfirst(x -> x isa Expr && x.head === :parameters, ex_call.args)
706+
new_args = [ex_args[1], ex_org.args[end], ex_args[2:end]...]
707+
ex_params !== nothing && push!(new_args, ex_call.args[ex_params])
708+
ex_org = Expr(:call, new_args...)
709+
end
701710
funct = repl_eval_ex(ex_org.args[1], context_module)
702711
funct === nothing && return 2, nothing, [], Set{Symbol}()
703712
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

@@ -2720,3 +2723,10 @@ let s = "foo58296(findfi"
27202723
@test "findfirst" in c
27212724
@test r == 10:15
27222725
end
2726+
2727+
# #58833 - Autocompletion of keyword arguments with do-blocks is broken
2728+
let s = "kwtest6(123; som|) do x; x + 3 end"
2729+
c, r = test_complete_context_pos(s, Main.CompletionFoo)
2730+
@test "somekwarg=" in c
2731+
@test r == 14:16
2732+
end

0 commit comments

Comments
 (0)