Skip to content

Instantly share code, notes, and snippets.

@antoine-levitt
Created November 18, 2018 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antoine-levitt/4526575d11d7f5eb2e6a767e926f1606 to your computer and use it in GitHub Desktop.
Save antoine-levitt/4526575d11d7f5eb2e6a767e926f1606 to your computer and use it in GitHub Desktop.
diff --git a/share/julia/stdlib/v1.0/REPL/src/REPLCompletions.jl b/share/julia/stdlib/v1.0/REPL/src/REPLCompletions.jl
index 3a3e507..5b1db55 100644
--- a/share/julia/stdlib/v1.0/REPL/src/REPLCompletions.jl
+++ b/share/julia/stdlib/v1.0/REPL/src/REPLCompletions.jl
@@ -696,7 +696,47 @@ function completions(string, pos, context_module=Main)::Completions
s = string[startpos:pos]
end
end
+
+ # Completion of strings like si(1.0) with pos after si should only
+ # complete those functions that are compatible with the arguments.
+ # Return false if not applicable, or a filter function if it is
+ function should_methodname_complete()
+ if (length(string) == pos) || (string[pos+1] != '(') || (!endswith(string,')'))
+ return false
+ else
+ # get the argument list and its type list
+ ex = Meta.parse("DUMMY"*string[pos+1:end], raise=false, depwarn=false)
+ (isa(ex, Expr) && ex.head==:call) || return false
+ types = Any[]
+ for arg in ex.args[2:end]
+ val, found = get_type(arg, context_module)
+ push!(types, val)
+ end
+
+ # return a filter function
+ ret = x -> begin
+ isdefined(context_module, x) || return false
+ fun = getfield(context_module, x)
+ isa(fun, Function) || return false
+ does_match = any(methods(fun)) do method
+ ms = method.sig
+ match = typeintersect(Base.rewrap_unionall(Tuple{Base.unwrap_unionall(ms).parameters...}, ms), Tuple{Core.Typeof(fun), types...}) != Union{}
+ match
+ end
+ return does_match
+ end
+ return ret
+ end
+ end
+
+
append!(suggestions, complete_symbol(s, ffunc, context_module))
+ filter_func = should_methodname_complete()
+ if filter_func != false
+ filter!(suggestions) do c
+ isa(c, ModuleCompletion) && filter_func(Symbol(completion_text(c)))
+ end
+ end
return sort!(unique(suggestions), by=completion_text), (dotpos+1):pos, true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment