Skip to content

Commit

Permalink
special case help output for functions with no docstrings
Browse files Browse the repository at this point in the history
add some tests
  • Loading branch information
jakebolewski committed Sep 10, 2015
1 parent b6cc2ed commit ae7ba6a
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 28 deletions.
84 changes: 56 additions & 28 deletions base/docs/Docs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,43 +95,71 @@ function doc(obj)
end
end

function write_lambda_signature(io::IO, lam::LambdaStaticData)
ex = Base.uncompressed_ast(lam)
write(io, '(')
nargs = length(ex.args[1])
for (i,arg) in enumerate(ex.args[1])
argname, argtype = arg.args
if argtype === :Any || argtype === :ANY
write(io, argname)
elseif isa(argtype,Expr) && argtype.head === :... &&
(argtype.args[end] === :Any || argtype.args[end] === :ANY)
write(io, argname, "...")
else
write(io, argname, "::", argtype)
end
i < nargs && write(io, ',')
end
write(io, ')')
return io
end

function macrosummary(name::Symbol, func::Function)
parts = ["""
No documentation found.
"""]
if isdefined(func,:code) && func.code != nothing
lam = Base.uncompressed_ast(func.code)
io = IOBuffer()
write(io, name, '(')
nargs = length(lam.args[1])
for (i,arg) in enumerate(lam.args[1])
argname, argtype = arg.args
if argtype === :Any || argtype === :ANY
write(io, argname)
elseif isa(argtype,Expr) && argtype.head === :... &&
(argtype.args[end] === :Any || argtype.args[end] === :ANY)
write(io, argname, "...")
else
write(io, argname, "::", argtype)
end
i < nargs && write(io, ',')
if !isdefined(func,:code) || func.code == nothing
return Markdown.parse("\n")
end
io = IOBuffer()
write(io, "```julia\n")
write(io, name)
write_lambda_signature(io, func.code)
write(io, "\n```")
return Markdown.parse(takebuf_string(io))
end

function functionsummary(func::Function)
io = IOBuffer()
write(io, "```julia\n")
if isgeneric(func)
print(io, methods(func))
else
if isdefined(func,:code) && func.code !== nothing
write_lambda_signature(io, func.code)
write(io, " -> ...")
end
write(io, ')')
push!(parts, string("```julia\n", takebuf_string(io), "\n```"))
end
Markdown.parse(join(parts,'\n'))
write(io, "\n```")
return Markdown.parse(takebuf_string(io))
end

function doc(b::Binding)
d = invoke(doc, Tuple{Any}, b)
if d == nothing
if d === nothing
v = getfield(b.mod,b.var)
d = doc(v)
if d == nothing
# check to see if the binding var is a macro
if startswith(string(b.var),'@')
d = macrosummary(b.var, v)
if d === nothing
if isa(v,Function)
d = catdoc(Markdown.parse("""
No documentation found.
`$(b.mod === Main ? b.var : join((b.mod, b.var),'.'))` is $(isgeneric(v) ? "a generic" : "an anonymous") `Function`.
"""), functionsummary(v))
elseif startswith(string(b.var),'@')
# check to see if the binding var is a macro
d = catdoc(Markdown.parse("""
No documentation found.
"""), macrosummary(b.var, v))
else
T = typeof(v)
d = catdoc(Markdown.parse("""
Expand Down
27 changes: 27 additions & 0 deletions test/docs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,12 @@ immutable D <: B
three::Float64
end

f = () -> nothing

undocumented() = 1
undocumented(x) = 2
undocumented(x,y) = 3

end

@test docstrings_equal(@doc(Undocumented.A), doc"""
Expand Down Expand Up @@ -388,6 +394,27 @@ three :: Float64
```
""")

let d = @doc Undocumented.f
io = IOBuffer()
writemime(io, MIME"text/markdown"(), d)
@test startswith(takebuf_string(io),"""
No documentation found.
`Undocumented.f` is an anonymous `Function`.
""")
end

let d = @doc Undocumented.undocumented
io = IOBuffer()
writemime(io, MIME"text/markdown"(), d)
@test startswith(takebuf_string(io), """
No documentation found.
`Undocumented.undocumented` is a generic `Function`.
""")
end


# Bindings.

import Base.Docs: @var, Binding
Expand Down

0 comments on commit ae7ba6a

Please sign in to comment.