Skip to content

Commit

Permalink
print kwargs in method show
Browse files Browse the repository at this point in the history
fix #4469
fix #2758
reopen #5230
  • Loading branch information
vtjnash committed Mar 21, 2016
1 parent 05bdbb0 commit b892bec
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
44 changes: 40 additions & 4 deletions base/methodshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,25 @@ function arg_decl_parts(m::Method)
return tv, decls, li.file, li.line
end

function show(io::IO, m::Method)
function kwarg_decl(m::Method, kwtype::DataType)
sig = Tuple{kwtype, Array, m.sig.parameters...}
mt = kwtype.name.mt
d = mt.defs
while d !== nothing
if typeseq(d.sig, sig)
li = d.func
e = uncompressed_ast(li)
argnames = Any[(isa(n,Expr) ? n.args[1] : n) for n in e.args[1]]
kwargs = filter!(x->!(x in argnames || '#' in string(x)),
Any[x[1] for x in e.args[2][1]])
return kwargs
end
d = d.next
end
return ()
end

function show(io::IO, m::Method; kwtype::Nullable{DataType}=Nullable{DataType}())
tv, decls, file, line = arg_decl_parts(m)
ft = m.sig.parameters[1]
d1 = decls[1]
Expand All @@ -66,6 +84,13 @@ function show(io::IO, m::Method)
print(io, "(")
print_joined(io, [isempty(d[2]) ? d[1] : d[1]*"::"*d[2] for d in decls[2:end]],
", ", ", ")
if !isnull(kwtype)
kwargs = kwarg_decl(m, get(kwtype))
if !isempty(kwargs)
print(io, "; ")
print_joined(io, kwargs, ", ", ", ")
end
end
print(io, ")")
if line > 0
print(io, " at ", file, ":", line)
Expand All @@ -85,10 +110,11 @@ function show_method_table(io::IO, mt::MethodTable, max::Int=-1, header::Bool=tr
end
d = mt.defs
n = rest = 0
kwtype = isdefined(mt, :kwsorter) ? Nullable{DataType}(typeof(mt.kwsorter)) : Nullable{DataType}()
while d !== nothing
if max==-1 || n<max || (rest==0 && n==max && d.next === nothing)
println(io)
show(io, d)
show(io, d; kwtype=kwtype)
n += 1
else
rest += 1
Expand Down Expand Up @@ -148,7 +174,7 @@ function url(m::Method)
end
end

function writemime(io::IO, ::MIME"text/html", m::Method)
function writemime(io::IO, ::MIME"text/html", m::Method; kwtype::Nullable{DataType}=Nullable{DataType}())
tv, decls, file, line = arg_decl_parts(m)
ft = m.sig.parameters[1]
d1 = decls[1]
Expand All @@ -174,6 +200,14 @@ function writemime(io::IO, ::MIME"text/html", m::Method)
print(io, "(")
print_joined(io, [isempty(d[2]) ? d[1] : d[1]*"::<b>"*d[2]*"</b>"
for d in decls[2:end]], ", ", ", ")
if !isnull(kwtype)
kwargs = kwarg_decl(m, get(kwtype))
if !isempty(kwargs)
print(io, "; <i>")
print_joined(io, kwargs, ", ", ", ")
print(io, "</i>")
end
end
print(io, ")")
if line > 0
u = url(m)
Expand All @@ -194,9 +228,11 @@ function writemime(io::IO, mime::MIME"text/html", mt::MethodTable)
what = startswith(ns, '@') ? "macro" : "generic function"
print(io, "$n $meths for ", what, " <b>$ns</b>:<ul>")
d = mt.defs
kwtype = isdefined(mt, :kwsorter) ? Nullable{DataType}(typeof(mt.kwsorter)) : Nullable{DataType}()
while d !== nothing
print(io, "<li> ")
writemime(io, mime, d)
writemime(io, mime, d; kwtype=kwtype)
print(io, "</li> ")
d = d.next
end
print(io, "</ul>")
Expand Down
3 changes: 1 addition & 2 deletions test/replutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Base.show_method_candidates(buf, Base.MethodError(method_c1,(1, 1, 1)))
test_have_color(buf, "", "")

method_c2(x::Int32, args...) = true
method_c2(x::Int32, y::Float64 ,args...) = true
method_c2(x::Int32, y::Float64, args...) = true
method_c2(x::Int32, y::Float64) = true
method_c2{T<:Real}(x::T, y::T, z::T) = true

Expand Down Expand Up @@ -297,7 +297,6 @@ let err_str,
@test err_str == " in (::FunctionLike)() at $sn:$(method_defs_lineno + 7)"
end


# Issue #13032
withenv("JULIA_EDITOR" => nothing, "VISUAL" => nothing, "EDITOR" => nothing) do

Expand Down
21 changes: 19 additions & 2 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,26 @@ end
#test methodshow.jl functions
@test Base.inbase(Base)
@test Base.inbase(LinAlg)
@test !Base.inbase(Core)

let repr = sprint(io -> writemime(io,"text/plain", methods(Base.inbase)))
@test contains(repr, "inbase(m::Module)")
end
let repr = sprint(io -> writemime(io,"text/html", methods(Base.inbase)))
@test contains(repr, "inbase(m::<b>Module</b>)")
end

f5971(x, y...; z=1, w...) = nothing
let repr = sprint(io -> writemime(io,"text/plain", methods(f5971)))
@test contains(repr, "f5971(x, y...; z)")
end
let repr = sprint(io -> writemime(io,"text/html", methods(f5971)))
@test contains(repr, "f5971(x, y...; <i>z</i>)")
end


@test sprint(show, which(f5971, Tuple{})) == "Symbol() at $sp:$(method_defs_lineno + 0)"

@test contains(sprint(io -> writemime(io,"text/plain",methods(Base.inbase))),"inbase(m::Module)")
@test contains(sprint(io -> writemime(io,"text/html",methods(Base.inbase))),"inbase(m::<b>Module</b>)")

if isempty(Base.GIT_VERSION_INFO.commit)
@test contains(Base.url(methods(eigs).defs),"https://github.com/JuliaLang/julia/tree/v$VERSION/base/linalg/arnoldi.jl#L")
Expand Down

0 comments on commit b892bec

Please sign in to comment.