From 594c9f8dc0f189f903c88e3afd0d5b301ddb4372 Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Sun, 25 Jul 2021 13:37:01 -0500 Subject: [PATCH] Support future generalization with Modifiers type --- stdlib/REPL/src/LineEdit.jl | 13 +++++++++---- stdlib/REPL/src/REPL.jl | 12 ++++++------ stdlib/REPL/src/REPLCompletions.jl | 10 +++++----- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/stdlib/REPL/src/LineEdit.jl b/stdlib/REPL/src/LineEdit.jl index 531fbd4a8566c..89f9a4cb99208 100644 --- a/stdlib/REPL/src/LineEdit.jl +++ b/stdlib/REPL/src/LineEdit.jl @@ -106,6 +106,11 @@ mutable struct PromptState <: ModeState refresh_wait::Union{Timer,Nothing} end +struct Modifiers + shift::Bool +end +Modifiers() = Modifiers(false) + options(s::PromptState) = if isdefined(s.p, :repl) && isdefined(s.p.repl, :options) # we can't test isa(s.p.repl, LineEditREPL) as LineEditREPL is defined @@ -1907,9 +1912,9 @@ mode(s::PromptState) = s.p # ::Prompt mode(s::SearchState) = @assert false mode(s::PrefixSearchState) = s.histprompt.parent_prompt # ::Prompt -setmodifier!(s::MIState, val::Symbol) = setmodifier!(mode(s), val) -setmodifier!(p::Prompt, val::Symbol) = setmodifier!(p.complete, val) -setmodifier!(c, val::Symbol) = nothing +setmodifiers!(s::MIState, m::Modifiers) = setmodifiers!(mode(s), m) +setmodifiers!(p::Prompt, m::Modifiers) = setmodifiers!(p.complete, m) +setmodifiers!(c) = nothing # Search Mode completions function complete_line(s::SearchState, repeats) @@ -2179,7 +2184,7 @@ function edit_tab(s::MIState, jump_spaces::Bool=false, delete_trailing::Bool=jum end function shift_tab_completion(s::MIState) - setmodifier!(s, :shift) + setmodifiers!(s, Modifiers(true)) return complete_line(s) end diff --git a/stdlib/REPL/src/REPL.jl b/stdlib/REPL/src/REPL.jl index 8f67384d698d1..a815678b7ba52 100644 --- a/stdlib/REPL/src/REPL.jl +++ b/stdlib/REPL/src/REPL.jl @@ -55,7 +55,7 @@ import ..LineEdit: history_last, history_search, accept_result, - setmodifier!, + setmodifiers!, terminal, MIState, PromptState, @@ -472,13 +472,13 @@ LineEditREPL(t::TextTerminal, hascolor::Bool, envcolors::Bool=false) = ) mutable struct REPLCompletionProvider <: CompletionProvider - shift::Bool + modifiers::LineEdit.Modifiers end -REPLCompletionProvider() = REPLCompletionProvider(:none) +REPLCompletionProvider() = REPLCompletionProvider(LineEdit.Modifiers()) mutable struct ShellCompletionProvider <: CompletionProvider end struct LatexCompletions <: CompletionProvider end -setmodifier!(c::REPLCompletionProvider, val::Symbol) = c.modifier = val +setmodifiers!(c::REPLCompletionProvider, m::LineEdit.Modifiers) = c.modifiers = m beforecursor(buf::IOBuffer) = String(buf.data[1:buf.ptr-1]) @@ -486,8 +486,7 @@ function complete_line(c::REPLCompletionProvider, s::PromptState) partial = beforecursor(s.input_buffer) full = LineEdit.input_string(s) ret, range, should_complete = completions(full, lastindex(partial)) - if c.modifier === :shift - c.modifier = :none + if !c.modifiers.shift # Filter out methods where all arguments are `Any` filter!(ret) do c isa(c, REPLCompletions.MethodCompletion) || return true @@ -495,6 +494,7 @@ function complete_line(c::REPLCompletionProvider, s::PromptState) return !all(T -> T === Any || T === Vararg{Any}, sig.parameters[2:end]) end end + c.modifiers = LineEdit.Modifiers() return unique!(map(completion_text, ret)), partial[range], should_complete end diff --git a/stdlib/REPL/src/REPLCompletions.jl b/stdlib/REPL/src/REPLCompletions.jl index dd765f0a2da72..d1d4daa413111 100644 --- a/stdlib/REPL/src/REPLCompletions.jl +++ b/stdlib/REPL/src/REPLCompletions.jl @@ -504,7 +504,7 @@ function complete_any_methods(ex_org::Expr, callee_module::Module, context_modul end for name in names(callee_module; all=true) - if !isdeprecated(callee_module, name) && isdefined(callee_module, name) + if !Base.isdeprecated(callee_module, name) && isdefined(callee_module, name) func = getfield(callee_module, name) if !isa(func, Module) complete_methods!(out, func, args_ex, kwargs_ex, moreargs) @@ -513,7 +513,7 @@ function complete_any_methods(ex_org::Expr, callee_module::Module, context_modul for name in names(callee_module2) if isdefined(callee_module2, name) func = getfield(callee_module, name) - if isa(func, Base.Callable) + if !isa(func, Module) complete_methods!(out, func, args_ex, kwargs_ex, moreargs) end end @@ -552,7 +552,7 @@ function complete_methods_args(funargs::Vector{Any}, ex_org::Expr, context_modul return args_ex, kwargs_ex end -function complete_methods!(out::Vector{Completion}, @nospecialize(func::Base.Callable), args_ex::Vector{Any}, kwargs_ex::Vector{Pair{Symbol,Any}}, moreargs::Bool=true) +function complete_methods!(out::Vector{Completion}, @nospecialize(func), args_ex::Vector{Any}, kwargs_ex::Vector{Pair{Symbol,Any}}, moreargs::Bool=true) ml = methods(func) # Input types and number of arguments if isempty(kwargs_ex) @@ -698,8 +698,8 @@ function completions(string::String, pos::Int, context_module::Module=Main) partial = string[1:pos] inc_tag = Base.incomplete_tag(Meta.parse(partial, raise=false, depwarn=false)) - # _(x, y)TAB lists methods you can call with these objects - # _(x, y TAB lists methods that take these objects as the first two arguments + # ?(x, y)TAB lists methods you can call with these objects + # ?(x, y TAB lists methods that take these objects as the first two arguments # MyModule._(x, y)TAB restricts the search to names in MyModule rexm = match(r"(\w+\.|)\?\((.*)$", partial) if rexm !== nothing