Skip to content

Commit 8f1797d

Browse files
committed
Implement TYPEDSIGNATURESNORETURN abbreviation
1 parent 8365f34 commit 8f1797d

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

src/DocStringExtensions.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ module DocStringExtensions
7676
# Exports.
7777

7878
export @template, FIELDS, TYPEDFIELDS, EXPORTS, METHODLIST, IMPORTS
79-
export SIGNATURES, TYPEDSIGNATURES, TYPEDEF, DOCSTRING, FUNCTIONNAME
79+
export SIGNATURES, TYPEDSIGNATURES, TYPEDSIGNATURESNORETURN, TYPEDEF, DOCSTRING, FUNCTIONNAME
8080
export README, LICENSE
8181
# export interpolation
8282

src/abbreviations.jl

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,13 @@ end
336336
#
337337

338338
"""
339-
The singleton type for [`TYPEDSIGNATURES`](@ref) abbreviations.
339+
The type for [`TYPEDSIGNATURES`](@ref) abbreviations.
340340
341341
$(:FIELDS)
342342
"""
343-
struct TypedMethodSignatures <: Abbreviation end
343+
struct TypedMethodSignatures <: Abbreviation
344+
return_types::Bool
345+
end
344346

345347
"""
346348
An [`Abbreviation`](@ref) for including a simplified representation of all the method
@@ -358,9 +360,17 @@ f(x::Int, y::Int; a, b...)
358360
```
359361
````
360362
"""
361-
const TYPEDSIGNATURES = TypedMethodSignatures()
363+
const TYPEDSIGNATURES = TypedMethodSignatures(true)
364+
365+
"""
366+
An alternative to [`TYPEDSIGNATURES`](@ref) that omits the return type.
367+
368+
The return type shown by [`TYPEDSIGNATURES`](@ref) is often `-> Any`, which is usually not
369+
correct. It is nicer to then just omit the type completely.
370+
"""
371+
const TYPEDSIGNATURESNORETURN = TypedMethodSignatures(false)
362372

363-
function format(::TypedMethodSignatures, buf, doc)
373+
function format(tms::TypedMethodSignatures, buf, doc)
364374
local binding = doc.data[:binding]
365375
local typesig = doc.data[:typesig]
366376
local modname = doc.data[:module]
@@ -395,7 +405,8 @@ function format(::TypedMethodSignatures, buf, doc)
395405
else
396406
t = tuples[findfirst(f, tuples)]
397407
end
398-
printmethod(buf, binding, func, method, t)
408+
printmethod(buf, binding, func, method, t;
409+
print_return_types=tms.return_types)
399410
println(buf)
400411
end
401412
println(buf, "\n```\n")

src/utilities.jl

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ f(x::Int; a = 1, b...) = x
338338
sig = printmethod(Docs.Binding(Main, :f), f, first(methods(f)))
339339
```
340340
"""
341-
function printmethod(buffer::IOBuffer, binding::Docs.Binding, func, method::Method, typesig)
341+
function printmethod(buffer::IOBuffer, binding::Docs.Binding, func, method::Method, typesig; print_return_types=true)
342342
# TODO: print qualified?
343343
local args = string.(arguments(method))
344344
local kws = string.(keywords(func, method))
@@ -397,11 +397,24 @@ function printmethod(buffer::IOBuffer, binding::Docs.Binding, func, method::Meth
397397
end
398398

399399
rt = Base.return_types(func, typesig)
400+
return_type_string = if (
401+
print_return_types &&
402+
length(rt) >= 1 &&
403+
rt[1] !== Nothing &&
404+
rt[1] !== Union{}
405+
)
406+
" -> $(rt[1])"
407+
else
408+
""
409+
end
400410

401-
return printmethod_format(buffer, string(binding.var), args, string.(kws);
402-
return_type =
403-
length(rt) >= 1 && rt[1] !== Nothing && rt[1] !== Union{} ?
404-
" -> $(rt[1])" : "")
411+
return printmethod_format(
412+
buffer,
413+
string(binding.var),
414+
args,
415+
string.(kws);
416+
return_type=return_type_string
417+
)
405418
end
406419

407420
printmethod(b, f, m) = String(take!(printmethod(IOBuffer(), b, f, m)))

0 commit comments

Comments
 (0)