Skip to content

Commit f9acba7

Browse files
LiozouLilithHafner
authored andcommitted
Make TAB-completion behave as SHIFT-TAB in REPL except for ?(x,y) syntax (JuliaLang#43577)
1 parent 4bb3033 commit f9acba7

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

stdlib/REPL/src/REPL.jl

+1-9
Original file line numberDiff line numberDiff line change
@@ -494,15 +494,7 @@ beforecursor(buf::IOBuffer) = String(buf.data[1:buf.ptr-1])
494494
function complete_line(c::REPLCompletionProvider, s::PromptState)
495495
partial = beforecursor(s.input_buffer)
496496
full = LineEdit.input_string(s)
497-
ret, range, should_complete = completions(full, lastindex(partial))
498-
if !c.modifiers.shift
499-
# Filter out methods where all arguments are `Any`
500-
filter!(ret) do c
501-
isa(c, REPLCompletions.MethodCompletion) || return true
502-
sig = Base.unwrap_unionall(c.method.sig)::DataType
503-
return !all(T -> T === Any || T === Vararg{Any}, sig.parameters[2:end])
504-
end
505-
end
497+
ret, range, should_complete = completions(full, lastindex(partial), Main, c.modifiers.shift)
506498
c.modifiers = LineEdit.Modifiers()
507499
return unique!(map(completion_text, ret)), partial[range], should_complete
508500
end

stdlib/REPL/src/REPLCompletions.jl

+12-3
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ function complete_methods(ex_org::Expr, context_module::Module=Main)
506506
return out
507507
end
508508

509-
function complete_any_methods(ex_org::Expr, callee_module::Module, context_module::Module, moreargs::Bool)
509+
function complete_any_methods(ex_org::Expr, callee_module::Module, context_module::Module, moreargs::Bool, shift::Bool)
510510
out = Completion[]
511511
args_ex, kwargs_ex = try
512512
complete_methods_args(ex_org.args[2:end], ex_org, context_module, false, false)
@@ -533,6 +533,15 @@ function complete_any_methods(ex_org::Expr, callee_module::Module, context_modul
533533
end
534534
end
535535

536+
if !shift
537+
# Filter out methods where all arguments are `Any`
538+
filter!(out) do c
539+
isa(c, REPLCompletions.MethodCompletion) || return true
540+
sig = Base.unwrap_unionall(c.method.sig)::DataType
541+
return !all(T -> T === Any || T === Vararg{Any}, sig.parameters[2:end])
542+
end
543+
end
544+
536545
return out
537546
end
538547

@@ -719,7 +728,7 @@ function project_deps_get_completion_candidates(pkgstarts::String, project_file:
719728
return Completion[PackageCompletion(name) for name in loading_candidates]
720729
end
721730

722-
function completions(string::String, pos::Int, context_module::Module=Main)
731+
function completions(string::String, pos::Int, context_module::Module=Main, shift::Bool=true)
723732
# First parse everything up to the current position
724733
partial = string[1:pos]
725734
inc_tag = Base.incomplete_tag(Meta.parse(partial, raise=false, depwarn=false))
@@ -750,7 +759,7 @@ function completions(string::String, pos::Int, context_module::Module=Main)
750759
end
751760
ex_org = Meta.parse(callstr, raise=false, depwarn=false)
752761
if isa(ex_org, Expr)
753-
return complete_any_methods(ex_org, callee_module::Module, context_module, moreargs), (0:length(rexm.captures[1])+1) .+ rexm.offset, false
762+
return complete_any_methods(ex_org, callee_module::Module, context_module, moreargs, shift), (0:length(rexm.captures[1])+1) .+ rexm.offset, false
754763
end
755764
end
756765

stdlib/REPL/test/replcompletions.jl

+28
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ test_scomplete(s) = map_completion_text(@inferred(shell_completions(s, lastinde
127127
test_bslashcomplete(s) = map_completion_text(@inferred(bslash_completions(s, lastindex(s)))[2])
128128
test_complete_context(s, m) = map_completion_text(@inferred(completions(s,lastindex(s), m)))
129129
test_complete_foo(s) = test_complete_context(s, Main.CompletionFoo)
130+
test_complete_noshift(s) = map_completion_text(@inferred(completions(s, lastindex(s), Main, false)))
130131

131132
module M32377 end
132133
test_complete_32377(s) = map_completion_text(completions(s,lastindex(s), M32377))
@@ -567,6 +568,33 @@ let s = "CompletionFoo.?('c'"
567568
@test any(str->occursin("test9(x::Char, i::Int", str), c)
568569
end
569570

571+
let s = "CompletionFoo.?(false, \"a\", 3, "
572+
c, r, res = test_complete(s)
573+
@test !res
574+
@test length(c) == 1
575+
@test occursin("test(args...)", c[1])
576+
end
577+
578+
let s = "CompletionFoo.?(false, \"a\", 3, "
579+
c, r, res = test_complete_noshift(s)
580+
@test !res
581+
@test isempty(c)
582+
end
583+
584+
let s = "CompletionFoo.?()"
585+
c, r, res = test_complete(s)
586+
@test !res
587+
@test any(str->occursin("foo()", str), c)
588+
@test any(str->occursin("kwtest(;", str), c)
589+
@test any(str->occursin("test(args...)", str), c)
590+
end
591+
592+
let s = "CompletionFoo.?()"
593+
c, r, res = test_complete_noshift(s)
594+
@test !res
595+
@test isempty(c)
596+
end
597+
570598
#################################################################
571599

572600
# Test of inference based getfield completion

0 commit comments

Comments
 (0)