Skip to content

Commit

Permalink
show multiple docstrings in REPL more clearly (#25651)
Browse files Browse the repository at this point in the history
* show multiple docstrings in REPL more clearly

this is done by printing a horizontal line after each separate docstring
  • Loading branch information
KristofferC authored and JeffBezanson committed Jan 22, 2018
1 parent a6d1829 commit bea2fb7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion base/docs/Docs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ function formatdoc(d::DocStr)
for part in d.text
formatdoc(buffer, d, part)
end
Markdown.parse(seekstart(buffer))
Markdown.MD(Any[Markdown.parse(seekstart(buffer))])
end
@noinline formatdoc(buffer, d, part) = print(buffer, part)

Expand Down
22 changes: 20 additions & 2 deletions base/docs/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ end

# REPL help

function helpmode(io::IO, line::AbstractString)
# This is split into helpmode and _helpmode to easier unittest _helpmode
helpmode(io::IO, line::AbstractString) = :(Base.Docs.insert_hlines($io, $(Base.Docs._helpmode(io, line))))
helpmode(line::AbstractString) = helpmode(STDOUT, line)

function _helpmode(io::IO, line::AbstractString)
line = strip(line)
expr =
if haskey(keywords, Symbol(line))
Expand All @@ -114,7 +118,21 @@ function helpmode(io::IO, line::AbstractString)
# so that the resulting expressions are evaluated in the Base.Docs namespace
:(Base.Docs.@repl $io $expr)
end
helpmode(line::AbstractString) = helpmode(STDOUT, line)
_helpmode(line::AbstractString) = _helpmode(STDOUT, line)

# Print vertical lines along each docstring if there are multiple docs
function insert_hlines(io::IO, docs)
if !isa(docs, Markdown.MD) || !haskey(docs.meta, :results) || isempty(docs.meta[:results])
return docs
end
v = Any[]
for (n, doc) in enumerate(docs.content)
push!(v, doc)
n == length(docs.content) || push!(v, Markdown.HorizontalRule())
end
return Markdown.MD(v)
end


function repl_search(io::IO, s)
pre = "search:"
Expand Down
2 changes: 1 addition & 1 deletion base/markdown/render/terminal/render.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function term(io::IO, br::LineBreak, columns)
end

function term(io::IO, br::HorizontalRule, columns)
println(io, " " ^ margin, "-" ^ (columns - 2margin))
println(io, " " ^ margin, "" ^ (columns - 2margin))
end

term(io::IO, x, _) = show(io, MIME"text/plain"(), x)
Expand Down
8 changes: 4 additions & 4 deletions test/docs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -958,9 +958,9 @@ for (line, expr) in Pair[
"\"...\"" => "...",
"r\"...\"" => Expr(:macrocall, Symbol("@r_str"), LineNumberNode(1, :none), "...")
]
@test Docs.helpmode(line) == Expr(:macrocall, Expr(:., Expr(:., :Base, QuoteNode(:Docs)), QuoteNode(Symbol("@repl"))), LineNumberNode(115, doc_util_path), STDOUT, expr)
@test Docs._helpmode(line) == Expr(:macrocall, Expr(:., Expr(:., :Base, QuoteNode(:Docs)), QuoteNode(Symbol("@repl"))), LineNumberNode(119, doc_util_path), STDOUT, expr)
buf = IOBuffer()
@test eval(Base, Docs.helpmode(buf, line)) isa Union{Base.Markdown.MD,Nothing}
@test eval(Base, Docs._helpmode(buf, line)) isa Union{Base.Markdown.MD,Nothing}
end

@test sprint(Base.Docs.repl_latex, "") == "\"\" can be typed by \\sqrt<tab>\n\n"
Expand Down Expand Up @@ -990,8 +990,8 @@ dynamic_test.x = "test 2"
@test @doc(dynamic_test) == "test 2 Union{}"
@test @doc(dynamic_test(::String)) == "test 2 Tuple{String}"

@test Docs._repl(:(dynamic_test(1.0))) == Expr(:escape, Expr(:macrocall, Symbol("@doc"), LineNumberNode(196, doc_util_path), :(dynamic_test(::typeof(1.0)))))
@test Docs._repl(:(dynamic_test(::String))) == Expr(:escape, Expr(:macrocall, Symbol("@doc"), LineNumberNode(196, doc_util_path), :(dynamic_test(::String))))
@test Docs._repl(:(dynamic_test(1.0))) == Expr(:escape, Expr(:macrocall, Symbol("@doc"), LineNumberNode(214, doc_util_path), :(dynamic_test(::typeof(1.0)))))
@test Docs._repl(:(dynamic_test(::String))) == Expr(:escape, Expr(:macrocall, Symbol("@doc"), LineNumberNode(214, doc_util_path), :(dynamic_test(::String))))



Expand Down

0 comments on commit bea2fb7

Please sign in to comment.