Skip to content

Commit 77d4c9d

Browse files
committed
Add formatting for long docstrings
1 parent 947bbad commit 77d4c9d

File tree

1 file changed

+79
-17
lines changed

1 file changed

+79
-17
lines changed

src/utilities.jl

Lines changed: 79 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,20 @@ function printmethod(buffer::IOBuffer, binding::Docs.Binding, func, method::Meth
197197
# TODO: print qualified?
198198
print(buffer, binding.var)
199199
print(buffer, "(")
200-
join(buffer, arguments(method), ", ")
200+
201+
#=
202+
Calculate how long the string of the args and kwargs are. If too long,
203+
break signature up into a nicely formatted multiline method signature.
204+
=#
205+
nl_delim, nl = get_format_delimiters(args, kws; delim=" ", break_point=40)
206+
207+
join(buffer, arguments(method), ", $nl_delim")
201208
local kws = keywords(func, method)
202209
if !isempty(kws)
203-
print(buffer, "; ")
204-
join(buffer, kws, ", ")
210+
print(buffer, "; $nl_delim")
211+
join(buffer, kws, ", $nl_delim")
205212
end
206-
print(buffer, ")")
213+
print(buffer, "$nl)")
207214
return buffer
208215
end
209216

@@ -276,8 +283,9 @@ function printmethod(buffer::IOBuffer, binding::Docs.Binding, func, method::Meth
276283
print(buffer, binding.var)
277284
print(buffer, "(")
278285
local args = arguments(method)
286+
local kws = keywords(func, method)
279287
local where_syntax = []
280-
288+
281289
# find inner tuple type
282290
function f(t)
283291
# t is always either a UnionAll which represents a generic type or a Tuple where each parameter is the argument
@@ -308,43 +316,61 @@ function printmethod(buffer::IOBuffer, binding::Docs.Binding, func, method::Meth
308316
typ
309317
end
310318

311-
for (i, sym) in enumerate(args)
319+
function get_type_at_i(typesig, i)
312320
if typesig isa UnionAll
313321
# e.g. Tuple{Vector{T}} where T<:Number
314322
# or Tuple{String, T, T} where T<:Number
315323
# or Tuple{Type{T}, String, Union{Nothing, Function}} where T<:Number
316324
t = [x for x in f(typesig).types]
317-
t = [get_typesig(x, x) for x in t][i]
325+
[get_typesig(x, x) for x in t][i]
318326
else
319327
# e.g. Tuple{Vector{Int}}
320-
t = typesig.types[i]
328+
typesig.types[i]
321329
end
330+
end
331+
332+
#=
333+
Calculate how long the string of the args and kwargs are. If too long,
334+
break signature up into a nicely formatted multiline method signature.
335+
=#
336+
nl_delim, nl = get_format_delimiters(
337+
args,
338+
kws;
339+
type_info = join([string(get_type_at_i(typesig, i)) for i in eachindex(args)]),
340+
delim=" ",
341+
break_point=40
342+
)
343+
344+
for (i, sym) in enumerate(args)
345+
t = get_type_at_i(typesig, i)
346+
322347
if isvarargtype(t)
323348
elt = vararg_eltype(t)
324349
if elt === Any
325-
print(buffer, "$sym...")
350+
print(buffer, "$nl_delim$sym...")
326351
else
327-
print(buffer, "$sym::$elt...")
352+
print(buffer, "$nl_delim$sym::$elt...")
328353
end
329354
elseif t === Any
330-
print(buffer, sym)
355+
print(buffer, "$nl_delim$sym")
331356
else
332-
print(buffer, "$sym::$t")
357+
print(buffer, "$nl_delim$sym::$t")
333358
end
334359

335360
if i != length(args)
336361
print(buffer, ", ")
337362
end
338363
end
339-
local kws = keywords(func, method)
364+
340365
if !isempty(kws)
341-
print(buffer, "; ")
342-
join(buffer, kws, ", ")
366+
print(buffer, "; $nl_delim")
367+
join(buffer, kws, ", $nl_delim")
343368
end
344-
print(buffer, ")")
369+
print(buffer, "$nl)")
345370
rt = Base.return_types(func, typesig)
346371
if length(rt) >= 1 && rt[1] !== Nothing && rt[1] !== Union{}
347-
print(buffer, " -> $(rt[1])")
372+
formatted_rt = format_return_type_string(rt[1])
373+
print(buffer, " -> $formatted_rt")
348374
end
349375
buffer
350376
end
@@ -509,3 +535,39 @@ if !isdefined(Base, :ismutabletype)
509535
return isa(t, DataType) && t.mutable
510536
end
511537
end
538+
539+
"""
540+
$(:SIGNATURES)
541+
542+
Return two strings used to format method signatures by breaking long signatures
543+
into multiline signatures. If the line width of the `args` and `kws` of a method
544+
is longer than `break_point`, then return a new line joined with `delim` and a
545+
new line character. Otherwise, return an empty string, so that the formatting is
546+
unaffected.
547+
"""
548+
function get_format_delimiters(args, kws; delim=" ", break_point=40, type_info = "")
549+
estimated_line_width = length(join(string.(args))) +
550+
length(join(string.(kws))) + length(type_info)
551+
552+
nl_delim = estimated_line_width > break_point ? "\n$delim" : ""
553+
nl = estimated_line_width > break_point ? "\n" : ""
554+
555+
return nl_delim, nl
556+
end
557+
558+
"""
559+
$(:SIGNATURES)
560+
561+
Format the return type to look prettier.
562+
"""
563+
function format_return_type_string(rt; delim = " ", break_point=40)
564+
if startswith(string(rt), "Tuple{") && length(string(rt)) > break_point
565+
string(
566+
"Tuple{\n",
567+
join([string(x) for x in rt.types],",\n$delim"),
568+
"\n}",
569+
)
570+
else
571+
string(rt)
572+
end
573+
end

0 commit comments

Comments
 (0)