Skip to content

Commit

Permalink
Support future generalization with Modifiers type
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Jul 25, 2021
1 parent 0aab0dc commit 594c9f8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
13 changes: 9 additions & 4 deletions stdlib/REPL/src/LineEdit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
12 changes: 6 additions & 6 deletions stdlib/REPL/src/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import ..LineEdit:
history_last,
history_search,
accept_result,
setmodifier!,
setmodifiers!,
terminal,
MIState,
PromptState,
Expand Down Expand Up @@ -472,29 +472,29 @@ 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])

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
sig = Base.unwrap_unionall(c.method.sig)::DataType
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

Expand Down
10 changes: 5 additions & 5 deletions stdlib/REPL/src/REPLCompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 594c9f8

Please sign in to comment.