@@ -505,6 +505,49 @@ isquotedmacrocall(@nospecialize x) =
505
505
isbasicdoc (@nospecialize x) = isexpr (x, :.) || isa (x, Union{QuoteNode, Symbol})
506
506
is_signature (@nospecialize x) = isexpr (x, :call ) || (isexpr (x, :(:: ), 2 ) && isexpr (x. args[1 ], :call )) || isexpr (x, :where )
507
507
508
+ function _doc (binding:: Binding , sig:: Type = Union{})
509
+ if defined (binding)
510
+ result = getdoc (resolve (binding), sig)
511
+ result === nothing || return result
512
+ end
513
+ # Lookup first match for `binding` and `sig` in all modules of the docsystem.
514
+ for mod in modules
515
+ dict = meta (mod; autoinit= false )
516
+ isnothing (dict) && continue
517
+ if haskey (dict, binding)
518
+ multidoc = dict[binding]
519
+ for msig in multidoc. order
520
+ sig <: msig && return multidoc. docs[msig]
521
+ end
522
+ end
523
+ end
524
+ return nothing
525
+ end
526
+
527
+ # Some additional convenience `doc` methods that take objects rather than `Binding`s.
528
+ _doc (obj:: UnionAll ) = _doc (Base. unwrap_unionall (obj))
529
+ _doc (object, sig:: Type = Union{}) = _doc (aliasof (object, typeof (object)), sig)
530
+ _doc (object, sig... ) = _doc (object, Tuple{sig... })
531
+
532
+ function simple_lookup_doc (ex)
533
+ if isa (ex, Expr) && ex. head != = :(.) && Base. isoperator (ex. head)
534
+ # handle syntactic operators, e.g. +=, ::, .=
535
+ ex = ex. head
536
+ end
537
+ if haskey (keywords, ex)
538
+ return keywords[ex]
539
+ elseif ! isa (ex, Expr) && ! isa (ex, Symbol)
540
+ return :($ (_doc)($ (typeof)($ (esc (ex)))))
541
+ end
542
+ binding = esc (bindingexpr (namify (ex)))
543
+ if isexpr (ex, :call ) || isexpr (ex, :macrocall ) || isexpr (ex, :where )
544
+ sig = esc (signature (ex))
545
+ :($ (_doc)($ binding, $ sig))
546
+ else
547
+ :($ (_doc)($ binding))
548
+ end
549
+ end
550
+
508
551
function docm (source:: LineNumberNode , mod:: Module , ex)
509
552
@nospecialize ex
510
553
if isexpr (ex, :-> ) && length (ex. args) > 1
@@ -513,6 +556,8 @@ function docm(source::LineNumberNode, mod::Module, ex)
513
556
# TODO : this is a shim to continue to allow `@doc` for looking up docstrings
514
557
REPL = Base. REPL_MODULE_REF[]
515
558
return invokelatest (REPL. lookup_doc, ex)
559
+ else
560
+ return simple_lookup_doc (ex)
516
561
end
517
562
return nothing
518
563
end
0 commit comments