@@ -464,17 +464,59 @@ function get_type(sym, fn::Module)
464464 return found ? Core. Typeof (val) : Any, found
465465end
466466
467+ function get_type (T, found:: Bool , default_any:: Bool )
468+ return found ? T :
469+ default_any ? Any : throw (ArgumentError (" argument not found" ))
470+ end
471+
467472# Method completion on function call expression that look like :(max(1))
468473function complete_methods (ex_org:: Expr , context_module:: Module = Main)
469474 func, found = get_value (ex_org. args[1 ], context_module):: Tuple{Any,Bool}
470475 ! found && return Completion[]
471476
472- funargs = ex_org. args[2 : end ]
473- # handle broadcasting, but only handle number of arguments instead of
474- # argument types
477+ args_ex, kwargs_ex = complete_methods_args (ex_org. args[2 : end ], ex_org, context_module, true , true )
478+
479+ out = Completion[]
480+ complete_methods! (out, func, args_ex, kwargs_ex)
481+ return out
482+ end
483+
484+ function complete_any_methods (ex_org:: Expr , callee_module:: Module , context_module:: Module , moreargs:: Bool )
485+ out = Completion[]
486+ args_ex, kwargs_ex = try
487+ complete_methods_args (ex_org. args[2 : end ], ex_org, context_module, false , false )
488+ catch
489+ return out
490+ end
491+
492+ for name in names (callee_module; all= true )
493+ if isdefined (callee_module, name)
494+ func = getfield (callee_module, name)
495+ if isa (func, Base. Callable) && func != = Vararg
496+ complete_methods! (out, func, args_ex, kwargs_ex, moreargs)
497+ elseif callee_module === Main:: Module && isa (func, Module)
498+ callee_module2 = func
499+ for name in names (callee_module2)
500+ if isdefined (callee_module2, name)
501+ func = getfield (callee_module, name)
502+ if isa (func, Base. Callable) && func != = Vararg
503+ complete_methods! (out, func, args_ex, kwargs_ex, moreargs)
504+ end
505+ end
506+ end
507+ end
508+ end
509+ end
510+
511+ return out
512+ end
513+
514+ function complete_methods_args (funargs:: Vector{Any} , ex_org:: Expr , context_module:: Module , default_any:: Bool , allow_broadcasting:: Bool )
475515 args_ex = Any[]
476516 kwargs_ex = Pair{Symbol,Any}[]
477- if ex_org. head === :. && ex_org. args[2 ] isa Expr
517+ if allow_broadcasting && ex_org. head === :. && ex_org. args[2 ] isa Expr
518+ # handle broadcasting, but only handle number of arguments instead of
519+ # argument types
478520 for _ in (ex_org. args[2 ]:: Expr ). args
479521 push! (args_ex, Any)
480522 end
@@ -483,18 +525,20 @@ function complete_methods(ex_org::Expr, context_module::Module=Main)
483525 if isexpr (ex, :parameters )
484526 for x in ex. args
485527 n, v = isexpr (x, :kw ) ? (x. args... ,) : (x, x)
486- push! (kwargs_ex, n => first (get_type (v, context_module)))
528+ push! (kwargs_ex, n => get_type (get_type (v, context_module)... , default_any ))
487529 end
488530 elseif isexpr (ex, :kw )
489531 n, v = (ex. args... ,)
490- push! (kwargs_ex, n => first (get_type (v, context_module)))
532+ push! (kwargs_ex, n => get_type (get_type (v, context_module)... , default_any ))
491533 else
492- push! (args_ex, first (get_type (ex, context_module)))
534+ push! (args_ex, get_type (get_type (ex, context_module)... , default_any ))
493535 end
494536 end
495537 end
538+ return args_ex, kwargs_ex
539+ end
496540
497- out = Completion[]
541+ function complete_methods! (out :: Vector{Completion} , @nospecialize (func :: Base.Callable ), args_ex :: Vector{Any} , kwargs_ex :: Vector{Pair{Symbol,Any}} , moreargs :: Bool = true )
498542 ml = methods (func)
499543 # Input types and number of arguments
500544 if isempty (kwargs_ex)
@@ -511,6 +555,9 @@ function complete_methods(ex_org::Expr, context_module::Module=Main)
511555 ml = methods (kwfunc)
512556 func = kwfunc
513557 end
558+ if ! moreargs
559+ na = typemax (Int)
560+ end
514561
515562 for (method:: Method , orig_method) in zip (ml, orig_ml)
516563 ms = method. sig
@@ -520,7 +567,6 @@ function complete_methods(ex_org::Expr, context_module::Module=Main)
520567 push! (out, MethodCompletion (func, t_in, method, orig_method))
521568 end
522569 end
523- return out
524570end
525571
526572include (" latex_symbols.jl" )
@@ -638,6 +684,36 @@ function completions(string::String, pos::Int, context_module::Module=Main)
638684 partial = string[1 : pos]
639685 inc_tag = Base. incomplete_tag (Meta. parse (partial, raise= false , depwarn= false ))
640686
687+ # _(x, y)TAB lists methods you can call with these objects
688+ # _(x, y TAB lists methods that take these objects as the first two arguments
689+ # MyModule._(x, y)TAB restricts the search to names in MyModule
690+ rexm = match (r" (\w +\. |)\?\( (.*)$" , partial)
691+ if rexm != = nothing
692+ # Get the module scope
693+ if isempty (rexm. captures[1 ])
694+ callee_module = context_module
695+ else
696+ modname = Symbol (rexm. captures[1 ][1 : end - 1 ])
697+ if isdefined (context_module, modname)
698+ callee_module = getfield (context_module, modname)
699+ if ! isa (callee_module, Module)
700+ callee_module = context_module
701+ end
702+ else
703+ callee_module = context_module
704+ end
705+ end
706+ moreargs = ! endswith (rexm. captures[2 ], ' )' )
707+ callstr = " _(" * rexm. captures[2 ]
708+ if moreargs
709+ callstr *= ' )'
710+ end
711+ ex_org = Meta. parse (callstr, raise= false , depwarn= false )
712+ if isa (ex_org, Expr)
713+ return complete_any_methods (ex_org, callee_module:: Module , context_module, moreargs), (0 : length (rexm. captures[1 ])+ 1 ) .+ rexm. offset, false
714+ end
715+ end
716+
641717 # if completing a key in a Dict
642718 identifier, partial_key, loc = dict_identifier_key (partial, inc_tag, context_module)
643719 if identifier != = nothing
0 commit comments