@@ -169,10 +169,10 @@ _docstr(doc::DocStr, data) = (doc.data = merge(data, doc.data); doc)
169
169
macro ref (x)
170
170
binding = bindingexpr (namify (x))
171
171
typesig = signature (x)
172
- esc (docexpr (binding, typesig))
172
+ esc (docexpr (__source__, binding, typesig))
173
173
end
174
174
175
- docexpr (args... ) = Expr (:call , docstr, args... )
175
+ docexpr (__source__, args... ) = Expr (:call , docstr, args... )
176
176
177
177
function formatdoc (d:: DocStr )
178
178
buffer = IOBuffer ()
@@ -482,26 +482,27 @@ isfield(x) = isexpr(x, :.) &&
482
482
# =========================
483
483
484
484
"""
485
- Docs.metadata(expr)
485
+ Docs.metadata(source, expr)
486
486
487
487
Build a `Dict` expression containing metadata captured from the expression `expr`.
488
488
489
489
Fields that may be included in the returned `Dict`:
490
490
491
- - `:path`: String representing the file where `expr` is defined.
491
+ - `:path`: Symbol representing the file where `expr` is defined.
492
492
- `:linenumber`: Linenumber where `expr` is defined.
493
493
- `:module`: Module where the docstring is defined.
494
494
- `:fields`: `Dict` of all field docs found in `expr`. Only for concrete types.
495
495
"""
496
- function metadata (expr)
496
+ function metadata (__source__, expr)
497
497
args = []
498
498
# Filename and linenumber of the docstring.
499
- push! (args, :($ (Pair)(:path , $ (Base). @__FILE__ )))
500
- push! (args, :($ (Pair)(:linenumber , $ (unsafe_load (cglobal (:jl_lineno , Cint))))))
499
+ __file__ = isa (__source__. file, Symbol) ? String (__source__. file) : " "
500
+ push! (args, Pair (:path , __file__))
501
+ push! (args, Pair (:linenumber , __source__. line))
501
502
# Module in which the docstring is defined.
502
503
push! (args, :($ (Pair)(:module , $ (current_module)())))
503
- # Field docs for concrete types.
504
504
if isexpr (expr, :type )
505
+ # Field docs for concrete types.
505
506
fields = []
506
507
tmp = nothing
507
508
for each in expr. args[3 ]. args
@@ -518,37 +519,36 @@ function metadata(expr)
518
519
:($ (Dict)($ (args... )))
519
520
end
520
521
521
- function keyworddoc (str, def)
522
- docstr = esc (docexpr (lazy_iterpolate (str), metadata (def)))
523
- :($ (keywords)[$ (esc (quot (def. name)))] = $ docstr)
522
+ function keyworddoc (__source__, str, def)
523
+ docstr = esc (docexpr (__source__, lazy_iterpolate (str), metadata (__source__, def)))
524
+ return :($ (keywords)[$ (esc (quot (def. name)))] = $ docstr)
524
525
end
525
526
526
- function objectdoc (str, def, expr, sig = :(Union{}))
527
+ function objectdoc (__source__, str, def, expr, sig = :(Union{}))
527
528
binding = esc (bindingexpr (namify (expr)))
528
- docstr = esc (docexpr (lazy_iterpolate (str), metadata (expr)))
529
+ docstr = esc (docexpr (__source__, lazy_iterpolate (str), metadata (__source__, expr)))
529
530
quote
530
531
$ (esc (def))
531
532
$ (doc!)($ binding, $ docstr, $ (esc (sig)))
532
533
end
533
534
end
534
535
535
- function calldoc (str, def)
536
+ function calldoc (__source__, str, def)
536
537
args = def. args[2 : end ]
537
538
if isempty (args) || all (validcall, args)
538
- objectdoc (str, nothing , def, signature (def))
539
+ objectdoc (__source__, str, nothing , def, signature (def))
539
540
else
540
541
docerror (def)
541
542
end
542
543
end
543
544
validcall (x) = isa (x, Symbol) || isexpr (x, (:(:: ), :... , :kw , :parameters ))
544
545
545
- function moduledoc (meta, def, def′)
546
+ function moduledoc (__source__, meta, def, def′)
546
547
name = namify (def′)
547
548
docex = Expr (:call , doc!, bindingexpr (name),
548
- docexpr (lazy_iterpolate (meta), metadata (name))
549
- )
549
+ docexpr (__source__, lazy_iterpolate (meta), metadata (__source__, name)))
550
550
if def === nothing
551
- esc (:(eval ($ name, $ (quot (docex)))))
551
+ esc (:($ eval ($ name, $ (quot (docex)))))
552
552
else
553
553
def = unblock (def)
554
554
block = def. args[3 ]. args
@@ -562,18 +562,18 @@ function moduledoc(meta, def, def′)
562
562
end
563
563
564
564
# Shares a single doc, `meta`, between several expressions from the tuple expression `ex`.
565
- function multidoc (meta, ex, define)
565
+ function multidoc (__source__, meta, ex, define)
566
566
out = Expr (:toplevel )
567
- str = docexpr (lazy_iterpolate (meta), metadata (ex))
567
+ str = docexpr (__source__, lazy_iterpolate (meta), metadata (__source__, ex))
568
568
ref = Ref {DocStr} ()
569
569
for (n, arg) in enumerate (ex. args)
570
570
# The first `arg` to be documented needs to also create the docstring for the group.
571
571
# Subsequent `arg`s just need `ref` to be able to find the docstring without having
572
572
# to create an entirely new one each.
573
573
docstr = n === 1 ? :($ (ref)[] = $ str) : :($ (ref)[])
574
- push! (out. args, :( @doc ( $ docstr, $ arg, $ define) ))
574
+ push! (out. args, docm (__source__, docstr, arg, define))
575
575
end
576
- esc ( out)
576
+ return out
577
577
end
578
578
579
579
"""
@@ -649,7 +649,7 @@ isquotedmacrocall(x) =
649
649
isbasicdoc (x) = isexpr (x, :.) || isa (x, Union{QuoteNode, Symbol})
650
650
is_signature (x) = isexpr (x, :call ) || (isexpr (x, :(:: ), 2 ) && isexpr (x. args[1 ], :call )) || isexpr (x, :where )
651
651
652
- function docm (meta, ex, define = true )
652
+ function docm (source :: LineNumberNode , meta, ex, define = true )
653
653
# Some documented expressions may be decorated with macro calls which obscure the actual
654
654
# expression. Expand the macro calls and remove extra blocks.
655
655
x = unblock (macroexpand (ex))
@@ -665,7 +665,7 @@ function docm(meta, ex, define = true)
665
665
# "..."
666
666
# kw"if", kw"else"
667
667
#
668
- isa (x, Base. BaseDocs. Keyword) ? keyworddoc (meta, x) :
668
+ isa (x, Base. BaseDocs. Keyword) ? keyworddoc (source, meta, x) :
669
669
670
670
# Method / macro definitions and "call" syntax.
671
671
#
@@ -675,38 +675,38 @@ function docm(meta, ex, define = true)
675
675
# function f end
676
676
# f(...)
677
677
#
678
- isexpr (x, FUNC_HEADS) && is_signature (x. args[1 ]) ? objectdoc (meta, def, x, signature (x)) :
679
- isexpr (x, :function ) && ! isexpr (x. args[1 ], :call ) ? objectdoc (meta, def, x) :
680
- isexpr (x, :call ) ? calldoc (meta, x) :
678
+ isexpr (x, FUNC_HEADS) && is_signature (x. args[1 ]) ? objectdoc (source, meta, def, x, signature (x)) :
679
+ isexpr (x, :function ) && ! isexpr (x. args[1 ], :call ) ? objectdoc (source, meta, def, x) :
680
+ isexpr (x, :call ) ? calldoc (source, meta, x) :
681
681
682
682
# Type definitions.
683
683
#
684
684
# type T ... end
685
685
# abstract T
686
686
# bitstype N T
687
687
#
688
- isexpr (x, [:type , :abstract , :bitstype ]) ? objectdoc (meta, def, x) :
688
+ isexpr (x, [:type , :abstract , :bitstype ]) ? objectdoc (source, meta, def, x) :
689
689
690
690
# "Bindings". Names that resolve to objects with different names, ie.
691
691
#
692
692
# const T = S
693
693
# T = S
694
694
# global T = S
695
695
#
696
- isexpr (x, BINDING_HEADS) && ! isexpr (x. args[1 ], :call ) ? objectdoc (meta, def, x) :
696
+ isexpr (x, BINDING_HEADS) && ! isexpr (x. args[1 ], :call ) ? objectdoc (source, meta, def, x) :
697
697
698
698
# Quoted macrocall syntax. `:@time` / `:(Base.@time)`.
699
- isquotedmacrocall (x) ? objectdoc (meta, def, x) :
699
+ isquotedmacrocall (x) ? objectdoc (source, meta, def, x) :
700
700
# Modules and baremodules.
701
- isexpr (x, :module ) ? moduledoc (meta, def, x) :
701
+ isexpr (x, :module ) ? moduledoc (source, meta, def, x) :
702
702
# Document several expressions with the same docstring. `a, b, c`.
703
- isexpr (x, :tuple ) ? multidoc (meta, x, define) :
703
+ isexpr (x, :tuple ) ? multidoc (source, meta, x, define) :
704
704
# Errors generated by calling `macroexpand` are passed back to the call site.
705
705
isexpr (x, :error ) ? esc (x) :
706
706
# When documenting macro-generated code we look for embedded `@__doc__` calls.
707
707
__doc__! (meta, x, define) ? esc (x) :
708
708
# Any "basic" expression such as a bare function or module name or numeric literal.
709
- isbasicdoc (x) ? objectdoc (meta, nothing , x) :
709
+ isbasicdoc (x) ? objectdoc (source, meta, nothing , x) :
710
710
711
711
# All other expressions are undocumentable and should be handled on a case-by-case basis
712
712
# with `@__doc__`. Unbound string literals are also undocumentable since they cannot be
@@ -725,9 +725,9 @@ function docerror(ex)
725
725
:($ (error)($ txt, " \n " ))
726
726
end
727
727
728
- function docm (ex)
728
+ function docm (source :: LineNumberNode , ex)
729
729
if isexpr (ex, :-> )
730
- docm (ex. args... )
730
+ docm (source, ex. args... )
731
731
elseif haskey (keywords, ex)
732
732
parsedoc (keywords[ex])
733
733
elseif isa (ex, Union{Expr, Symbol})
@@ -764,7 +764,7 @@ function loaddocs(docs)
764
764
for (mod, ex, str, file, line) in docs
765
765
data = Dict (:path => string (file), :linenumber => line)
766
766
doc = docstr (str, data)
767
- docstring = eval (mod, Expr (:body , Expr (:return , Expr (:call , QuoteNode (docm), QuoteNode (doc), QuoteNode (ex), false )))) # expand the real @doc macro now (using a hack because macroexpand takes current-module as an implicit argument)
767
+ docstring = eval (mod, Expr (:body , Expr (:return , Expr (:call , QuoteNode (docm), QuoteNode (LineNumberNode (line, file)), QuoteNode ( doc), QuoteNode (ex), false )))) # expand the real @doc macro now (using a hack because macroexpand takes current-module as an implicit argument)
768
768
eval (mod, Expr (:macrocall , unescape, nothing , docstring))
769
769
end
770
770
empty! (docs)
0 commit comments