Skip to content

Commit bea2fb7

Browse files
KristofferCJeffBezanson
authored andcommitted
show multiple docstrings in REPL more clearly (#25651)
* show multiple docstrings in REPL more clearly this is done by printing a horizontal line after each separate docstring
1 parent a6d1829 commit bea2fb7

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

Diff for: base/docs/Docs.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ function formatdoc(d::DocStr)
179179
for part in d.text
180180
formatdoc(buffer, d, part)
181181
end
182-
Markdown.parse(seekstart(buffer))
182+
Markdown.MD(Any[Markdown.parse(seekstart(buffer))])
183183
end
184184
@noinline formatdoc(buffer, d, part) = print(buffer, part)
185185

Diff for: base/docs/utils.jl

+20-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ end
9494

9595
# REPL help
9696

97-
function helpmode(io::IO, line::AbstractString)
97+
# This is split into helpmode and _helpmode to easier unittest _helpmode
98+
helpmode(io::IO, line::AbstractString) = :(Base.Docs.insert_hlines($io, $(Base.Docs._helpmode(io, line))))
99+
helpmode(line::AbstractString) = helpmode(STDOUT, line)
100+
101+
function _helpmode(io::IO, line::AbstractString)
98102
line = strip(line)
99103
expr =
100104
if haskey(keywords, Symbol(line))
@@ -114,7 +118,21 @@ function helpmode(io::IO, line::AbstractString)
114118
# so that the resulting expressions are evaluated in the Base.Docs namespace
115119
:(Base.Docs.@repl $io $expr)
116120
end
117-
helpmode(line::AbstractString) = helpmode(STDOUT, line)
121+
_helpmode(line::AbstractString) = _helpmode(STDOUT, line)
122+
123+
# Print vertical lines along each docstring if there are multiple docs
124+
function insert_hlines(io::IO, docs)
125+
if !isa(docs, Markdown.MD) || !haskey(docs.meta, :results) || isempty(docs.meta[:results])
126+
return docs
127+
end
128+
v = Any[]
129+
for (n, doc) in enumerate(docs.content)
130+
push!(v, doc)
131+
n == length(docs.content) || push!(v, Markdown.HorizontalRule())
132+
end
133+
return Markdown.MD(v)
134+
end
135+
118136

119137
function repl_search(io::IO, s)
120138
pre = "search:"

Diff for: base/markdown/render/terminal/render.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function term(io::IO, br::LineBreak, columns)
107107
end
108108

109109
function term(io::IO, br::HorizontalRule, columns)
110-
println(io, " " ^ margin, "-" ^ (columns - 2margin))
110+
println(io, " " ^ margin, "" ^ (columns - 2margin))
111111
end
112112

113113
term(io::IO, x, _) = show(io, MIME"text/plain"(), x)

Diff for: test/docs.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -958,9 +958,9 @@ for (line, expr) in Pair[
958958
"\"...\"" => "...",
959959
"r\"...\"" => Expr(:macrocall, Symbol("@r_str"), LineNumberNode(1, :none), "...")
960960
]
961-
@test Docs.helpmode(line) == Expr(:macrocall, Expr(:., Expr(:., :Base, QuoteNode(:Docs)), QuoteNode(Symbol("@repl"))), LineNumberNode(115, doc_util_path), STDOUT, expr)
961+
@test Docs._helpmode(line) == Expr(:macrocall, Expr(:., Expr(:., :Base, QuoteNode(:Docs)), QuoteNode(Symbol("@repl"))), LineNumberNode(119, doc_util_path), STDOUT, expr)
962962
buf = IOBuffer()
963-
@test eval(Base, Docs.helpmode(buf, line)) isa Union{Base.Markdown.MD,Nothing}
963+
@test eval(Base, Docs._helpmode(buf, line)) isa Union{Base.Markdown.MD,Nothing}
964964
end
965965

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

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

996996

997997

0 commit comments

Comments
 (0)