Skip to content

Commit

Permalink
Merge pull request #5464 from stevengj/prettymethods
Browse files Browse the repository at this point in the history
pretty-pretting of which and methodswith output
  • Loading branch information
JeffBezanson committed Jan 21, 2014
2 parents d5e570e + d2f0a07 commit 1242782
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
23 changes: 21 additions & 2 deletions base/methodshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function arg_decl_parts(m::Method)
end

function show(io::IO, m::Method)
print(io, m.func.code.name)
tv, decls, file, line = arg_decl_parts(m)
if !isempty(tv)
show_delim_array(io, tv, '{', ',', '}', false)
Expand All @@ -57,7 +58,6 @@ function show_method_table(io::IO, mt::MethodTable, max::Int=-1, header::Bool=tr
while !is(d,())
if max==-1 || n<max || (rest==0 && n==max && d.next === ())
println(io)
print(io, name)
show(io, d)
n += 1
else
Expand Down Expand Up @@ -102,6 +102,7 @@ function url(m::Method)
end

function writemime(io::IO, ::MIME"text/html", m::Method)
print(io, m.func.code.name)
tv, decls, file, line = arg_decl_parts(m)
if !isempty(tv)
print(io,"<i>")
Expand Down Expand Up @@ -130,9 +131,27 @@ function writemime(io::IO, mime::MIME"text/html", mt::MethodTable)
print(io, "$n $meths for generic function <b>$name</b>:<ul>")
d = mt.defs
while !is(d,())
print(io, "<li> ", name)
print(io, "<li> ")
writemime(io, mime, d)
d = d.next
end
print(io, "</ul>")
end

# pretty-printing of Vector{Method} for output of methodswith:

function writemime(io::IO, mime::MIME"text/html", mt::AbstractVector{Method})
print(io, summary(mt))
if !isempty(mt)
print(io, ":<ul>")
for d in mt
print(io, "<li> ")
writemime(io, mime, d)
end
print(io, "</ul>")
end
end

# override usual show method for Vector{Method}: don't abbreviate long lists
writemime(io::IO, mime::MIME"text/plain", mt::AbstractVector{Method}) =
showarray(io, mt, limit=false)
21 changes: 9 additions & 12 deletions base/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ function whicht(f, types)
d = f.env.defs
while !is(d,())
if is(d.func.code, lsd)
print(STDOUT, f.env.name)
show(STDOUT, d); println(STDOUT)
display(d)
return
end
d = d.next
Expand Down Expand Up @@ -412,38 +411,36 @@ versioninfo(verbose::Bool) = versioninfo(STDOUT,verbose)

# `methodswith` -- shows a list of methods using the type given

function methodswith(io::IO, t::Type, m::Module, showparents::Bool)
function methodswith(t::Type, m::Module, showparents::Bool=false)
meths = Method[]
for nm in names(m)
try
mt = eval(m, nm)
d = mt.env.defs
while !is(d,())
if any(map(x -> x == t || (showparents && t <: x && x != Any && x != ANY && !isa(x, TypeVar)), d.sig))
print(io, nm)
show(io, d)
println(io)
push!(meths, d)
end
d = d.next
end
end
end
return meths
end

methodswith(t::Type, m::Module, showparents::Bool) = methodswith(STDOUT, t, m, showparents)
methodswith(t::Type, showparents::Bool) = methodswith(STDOUT, t, showparents)
methodswith(t::Type, m::Module) = methodswith(STDOUT, t, m, false)
methodswith(t::Type) = methodswith(STDOUT, t, false)
function methodswith(io::IO, t::Type, showparents::Bool)
function methodswith(t::Type, showparents::Bool=false)
meths = Method[]
mainmod = current_module()
# find modules in Main
for nm in names(mainmod)
if isdefined(mainmod,nm)
mod = eval(mainmod, nm)
if isa(mod, Module)
methodswith(io, t, mod, showparents)
meths = [meths, methodswith(t, mod, showparents)]
end
end
end
return meths
end

## printing with color ##
Expand Down
4 changes: 2 additions & 2 deletions doc/stdlib/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ Getting Around

.. function:: methodswith(typ[, showparents])

Show all methods with an argument of type ``typ``. If optional
``showparents`` is ``true``, also show arguments with a parent type
Return an array of methods with an argument of type ``typ``. If optional
``showparents`` is ``true``, also return arguments with a parent type
of ``typ``, excluding type ``Any``.

.. function:: @show
Expand Down

0 comments on commit 1242782

Please sign in to comment.