Skip to content

Commit 13b75de

Browse files
MichaelHatherlytkelman
authored andcommitted
Minor docsystem refactoring
Combines the `doc(::Function)` and `doc(::DataType)` methods since they were nearly identical. Enables searching for docstrings attached to specific constructors in the same manner as is currently allowed for normal methods. Prior to this it resulted in a no method error. Fixes #12701 where splatted method docs were shadowing others. Although not the original cause of the issue, with the change to storing signatures as `Tuple{...}` (in #12835) this meant that `f(x...)` was stored as `Tuple{Vararg{Any}}` which is equal to `Tuple`. Since the default value of `sig` was `Tuple` not all matching docs were returned in the example provided in #12701. (cherry picked from commit 1b1e6c6) ref #13330
1 parent 90732c4 commit 13b75de

File tree

1 file changed

+31
-50
lines changed

1 file changed

+31
-50
lines changed

base/docs/Docs.jl

+31-50
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ function doc(b::Binding)
160160
161161
`$(b.mod === Main ? b.var : join((b.mod, b.var),'.'))` is $(isgeneric(v) ? "a generic" : "an anonymous") `Function`.
162162
"""), functionsummary(v))
163+
elseif isa(v,DataType)
164+
d = catdoc(Markdown.parse("""
165+
No documentation found.
166+
167+
"""), typesummary(v))
163168
else
164169
T = typeof(v)
165170
d = catdoc(Markdown.parse("""
@@ -234,37 +239,6 @@ function doc!(f::Function, sig::ANY, data, source)
234239
fd.source[sig] = source
235240
end
236241

237-
doc(f::Function) = doc(f, Tuple)
238-
239-
function doc(f::Function, sig::Type)
240-
isgeneric(f) && isempty(methods(f,sig)) && return nothing
241-
results, funcdocs = [], []
242-
for mod in modules
243-
if (haskey(meta(mod), f) && isa(meta(mod)[f], FuncDoc))
244-
fd = meta(mod)[f]
245-
push!(funcdocs, fd)
246-
for msig in fd.order
247-
# try to find specific matching method signatures
248-
if sig <: msig
249-
push!(results, (msig, fd.meta[msig]))
250-
end
251-
end
252-
end
253-
end
254-
# if all method signatures are Union{} ( ⊥ ), concat all docstrings
255-
if isempty(results)
256-
for fd in funcdocs
257-
append!(results, [fd.meta[msig] for msig in reverse(fd.order)])
258-
end
259-
else
260-
sort!(results, lt = (a, b) -> type_morespecific(first(a), first(b)))
261-
results = [last(r) for r in results]
262-
end
263-
catdoc(results...)
264-
end
265-
doc(f::Function,args::Any...) = doc(f, Tuple{args...})
266-
267-
268242
"""
269243
`catdoc(xs...)`: Combine the documentation metadata `xs` into a single meta object.
270244
"""
@@ -317,32 +291,39 @@ function doc!(T::DataType, sig::ANY, data, source)
317291
td.meta[sig] = data
318292
end
319293

320-
function doc(T::DataType)
321-
docs = []
322-
for mod in modules
323-
if haskey(meta(mod), T)
324-
Td = meta(mod)[T]
325-
if isa(Td, TypeDoc)
326-
if length(docs) == 0 && Td.main !== nothing
327-
push!(docs, Td.main)
294+
function doc(obj::Base.Callable, sig::Type = Union)
295+
isgeneric(obj) && sig !== Union && isempty(methods(obj, sig)) && return nothing
296+
results, groups = [], []
297+
for m in modules
298+
if haskey(meta(m), obj)
299+
docs = meta(m)[obj]
300+
if isa(docs, FuncDoc) || isa(docs, TypeDoc)
301+
push!(groups, docs)
302+
for msig in docs.order
303+
if sig <: msig
304+
push!(results, (msig, docs.meta[msig]))
305+
end
328306
end
329-
for m in Td.order
330-
push!(docs, Td.meta[m])
307+
if isempty(results) && docs.main !== nothing
308+
push!(results, (Union{}, docs.main))
331309
end
332-
elseif length(docs) == 0
333-
return Td
310+
else
311+
push!(results, (Union{}, docs))
334312
end
335313
end
336314
end
337-
if isempty(docs)
338-
catdoc(Markdown.parse("""
339-
No documentation found.
340-
341-
"""), typesummary(T))
342-
else
343-
catdoc(docs...)
315+
# If all method signatures are Union{} ( ⊥ ), concat all docstrings.
316+
if isempty(results)
317+
for group in groups
318+
append!(results, [group.meta[s] for s in reverse(group.order)])
319+
end
320+
else
321+
sort!(results, lt = (a, b) -> type_morespecific(first(a), first(b)))
322+
results = map(last, results)
344323
end
324+
catdoc(results...)
345325
end
326+
doc(f::Base.Callable, args::Any...) = doc(f, Tuple{args...})
346327

347328
function typesummary(T::DataType)
348329
parts = [

0 commit comments

Comments
 (0)