Skip to content

Commit

Permalink
fix REPL summary for types (#39358)
Browse files Browse the repository at this point in the history
* add more information to Module for docview `summarize(...)`

* Add specialization for all Type types to docview `summarize(...)`

Co-Authored-By: Jameson Nash <vtjnash@gmail.com>

Co-authored-by: m-j-w <m-j-w@web.de>
  • Loading branch information
vtjnash and m-j-w committed Jan 25, 2021
1 parent b0fc9ba commit fd46fc6
Show file tree
Hide file tree
Showing 2 changed files with 328 additions and 35 deletions.
80 changes: 51 additions & 29 deletions stdlib/REPL/src/docview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -264,44 +264,66 @@ function summarize(io::IO, λ::Function, binding::Binding)
println(io, "```\n", methods(λ), "\n```")
end

function summarize(io::IO, T::DataType, binding::Binding)
function summarize(io::IO, TT::Type, binding::Binding)
println(io, "# Summary")
println(io, "```")
println(io,
T.abstract ? "abstract type" :
T.mutable ? "mutable struct" :
Base.isstructtype(T) ? "struct" : "primitive type",
" ", T, " <: ", supertype(T)
)
println(io, "```")
if !T.abstract && T.name !== Tuple.name && !isempty(fieldnames(T))
println(io, "# Fields")
T = Base.unwrap_unionall(TT)
if T isa DataType
println(io, "```")
pad = maximum(length(string(f)) for f in fieldnames(T))
for (f, t) in zip(fieldnames(T), T.types)
println(io, rpad(f, pad), " :: ", t)
end
println(io, "```")
end
if !isempty(subtypes(T))
println(io, "# Subtypes")
print(io,
T.abstract ? "abstract type " :
T.mutable ? "mutable struct " :
Base.isstructtype(T) ? "struct " :
"primitive type ")
supert = supertype(T)
println(io, T)
println(io, "```")
for t in subtypes(T)
println(io, t)
if !T.abstract && T.name !== Tuple.name && !isempty(fieldnames(T))
println(io, "# Fields")
println(io, "```")
pad = maximum(length(string(f)) for f in fieldnames(T))
for (f, t) in zip(fieldnames(T), T.types)
println(io, rpad(f, pad), " :: ", t)
end
println(io, "```")
end
println(io, "```")
end
if supertype(T) != Any
println(io, "# Supertype Hierarchy")
println(io, "```")
Base.show_supertypes(io, T)
println(io)
println(io, "```")
subt = subtypes(TT)
if !isempty(subt)
println(io, "# Subtypes")
println(io, "```")
for t in subt
println(io, t)
end
println(io, "```")
end
if supert != Any
println(io, "# Supertype Hierarchy")
println(io, "```")
Base.show_supertypes(io, T)
println(io)
println(io, "```")
end
elseif T isa Union
println(io, "`", binding, "` is of type `", typeof(TT), "`.\n")
println(io, "# Union Composed of Types")
for T1 in Base.uniontypes(T)
println(io, " - `", Base.rewrap_unionall(T1, TT), "`")
end
else # unreachable?
println(io, "`", binding, "` is of type `", typeof(TT), "`.\n")
end
end

function summarize(io::IO, m::Module, binding::Binding)
println(io, "No docstring found for module `", m, "`.\n")
exports = filter!(!=(nameof(m)), names(m))
if isempty(exports)
println(io, "Module does not export any names.")
else
println(io, "# Exported names:")
print(io, " `")
join(io, exports, "`, `")
println(io, "`")
end
end

function summarize(io::IO, @nospecialize(T), binding::Binding)
Expand Down
Loading

0 comments on commit fd46fc6

Please sign in to comment.