Skip to content

Commit 1902da1

Browse files
committed
Implement TYPEDSIGNATURENORETURN abbreviation
1 parent a117f47 commit 1902da1

File tree

2 files changed

+64
-40
lines changed

2 files changed

+64
-40
lines changed

src/abbreviations.jl

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ function format(::ModuleExports, buf, doc)
159159
if !isempty(exports)
160160
println(buf)
161161
# Sorting ignores the `@` in macro names and sorts them in with others.
162-
for sym in sort(exports, by = s -> lstrip(string(s), '@'))
162+
for sym in sort(exports, by=s -> lstrip(string(s), '@'))
163163
# Skip the module itself, since that's always exported.
164164
sym === nameof(object) && continue
165165
# We print linked names using Documenter.jl cross-reference syntax
@@ -206,7 +206,7 @@ function format(::ModuleImports, buf, doc)
206206
local imports = unique(ccall(:jl_module_usings, Any, (Any,), object))
207207
if !isempty(imports)
208208
println(buf)
209-
for mod in sort(imports, by = string)
209+
for mod in sort(imports, by=string)
210210
println(buf, " - `", mod, "`")
211211
end
212212
println(buf)
@@ -255,7 +255,7 @@ function format(::MethodList, buf, doc)
255255
local typesig = doc.data[:typesig]
256256
local modname = doc.data[:module]
257257
local func = Docs.resolve(binding)
258-
local groups = methodgroups(func, typesig, modname; exact = false)
258+
local groups = methodgroups(func, typesig, modname; exact=false)
259259
if !isempty(groups)
260260
println(buf)
261261
for group in groups
@@ -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")
@@ -574,7 +585,7 @@ An [`Abbreviation`](@ref) for including the package LICENSE.md.
574585
"""
575586
const LICENSE = License()
576587

577-
function format(::T, buf, doc) where T <: Union{Readme,License}
588+
function format(::T, buf, doc) where T<:Union{Readme,License}
578589
m = get(doc.data, :module, nothing)
579590
m === nothing && return
580591
path = pathof(m)
@@ -647,8 +658,8 @@ end
647658
function included_range(abbr::Template, parts::Vector)
648659
# Select the correct indexing depending on what we find.
649660
build_range(::Template, ::Nothing) = 0:-1
650-
build_range(::Template{:before}, index) = 1:(index - 1)
651-
build_range(::Template{:after}, index) = (index + 1):lastindex(parts)
661+
build_range(::Template{:before}, index) = 1:(index-1)
662+
build_range(::Template{:after}, index) = (index+1):lastindex(parts)
652663
# Search for index from either the front or back.
653664
find_index(::Template{:before}) = findfirst(is_docstr_template, parts)
654665
find_index(::Template{:after}) = findlast(is_docstr_template, parts)
@@ -662,13 +673,13 @@ function template_key(doc::Docs.DocStr)
662673
objname(obj::Union{Function,Module,DataType,UnionAll,Core.IntrinsicFunction}, b::Docs.Binding) = nameof(obj)
663674
objname(obj, b::Docs.Binding) = Symbol("") # Empty to force resolving to `:CONSTANTS` below.
664675
# Select the key returned based on input argument types.
665-
_key(::Module, sig, binding) = :MODULES
676+
_key(::Module, sig, binding) = :MODULES
666677
_key(::Function, ::typeof(Union{}), binding) = ismacro(binding) ? :MACROS : :FUNCTIONS
667-
_key(::Function, sig, binding) = ismacro(binding) ? :MACROS : :METHODS
678+
_key(::Function, sig, binding) = ismacro(binding) ? :MACROS : :METHODS
668679
_key(::DataType, ::typeof(Union{}), binding) = :TYPES
669680
_key(::UnionAll, ::typeof(Union{}), binding) = :TYPES
670-
_key(::DataType, sig, binding) = :METHODS
671-
_key(other, sig, binding) = :DEFAULT
681+
_key(::DataType, sig, binding) = :METHODS
682+
_key(other, sig, binding) = :DEFAULT
672683

673684
binding = doc.data[:binding]
674685
obj = Docs.resolve(binding)

src/utilities.jl

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ Keyword argument `exact = true` matches signatures "exactly" with `==` rather th
5555
groups = methodgroups(f, Union{Tuple{Any}, Tuple{Any, Integer}}, Main; exact = false)
5656
```
5757
"""
58-
function methodgroups(func, typesig, modname; exact = true)
58+
function methodgroups(func, typesig, modname; exact=true)
5959
# Group methods by file and line number.
6060
local methods = getmethods(func, typesig)
61-
local groups = groupby(Tuple{Symbol, Int}, Vector{Method}, methods) do m
61+
local groups = groupby(Tuple{Symbol,Int}, Vector{Method}, methods) do m
6262
(m.file, m.line), m
6363
end
6464

@@ -74,7 +74,7 @@ function methodgroups(func, typesig, modname; exact = true)
7474
end
7575

7676
# Sort the groups by file and line.
77-
sort!(results, lt = comparemethods, by = first)
77+
sort!(results, lt=comparemethods, by=first)
7878

7979
return results
8080
end
@@ -164,7 +164,7 @@ function groupby!(f, groups, data)
164164
key, value = f(each)
165165
push!(get!(groups, key, []), value)
166166
end
167-
return sort!(collect(groups), by = first)
167+
return sort!(collect(groups), by=first)
168168
end
169169

170170
"""
@@ -183,7 +183,7 @@ groupby(Int, Vector{Int}, collect(1:10)) do num
183183
end
184184
```
185185
"""
186-
groupby(f, K, V, data) = groupby!(f, Dict{K, V}(), data)
186+
groupby(f, K, V, data) = groupby!(f, Dict{K,V}(), data)
187187

188188
"""
189189
$(:SIGNATURES)
@@ -218,21 +218,21 @@ Decides whether a length of method is too big to be visually appealing.
218218
"""
219219
method_length_over_limit(len::Int) = len > 60
220220

221-
function printmethod_format(buffer::IOBuffer, binding::String, args::Vector{String}, kws::Vector{String}; return_type = "")
221+
function printmethod_format(buffer::IOBuffer, binding::String, args::Vector{String}, kws::Vector{String}; return_type="")
222222

223223
sep_delim = " "
224224
paren_delim = ""
225225
indent = ""
226226

227227
if method_length_over_limit(
228-
length(binding) +
229-
1 +
230-
sum(length.(args)) +
231-
sum(length.(kws)) +
232-
2*max(0, length(args)-1) +
233-
2*length(kws) +
234-
1 +
235-
length(return_type))
228+
length(binding) +
229+
1 +
230+
sum(length.(args)) +
231+
sum(length.(kws)) +
232+
2 * max(0, length(args) - 1) +
233+
2 * length(kws) +
234+
1 +
235+
length(return_type))
236236

237237
sep_delim = "\n"
238238
paren_delim = "\n"
@@ -241,10 +241,10 @@ function printmethod_format(buffer::IOBuffer, binding::String, args::Vector{Stri
241241

242242
print(buffer, binding)
243243
print(buffer, "($paren_delim")
244-
join(buffer, Ref(indent).*args, ",$sep_delim")
244+
join(buffer, Ref(indent) .* args, ",$sep_delim")
245245
if !isempty(kws)
246246
print(buffer, ";$sep_delim")
247-
join(buffer, Ref(indent).*kws, ",$sep_delim")
247+
join(buffer, Ref(indent) .* kws, ",$sep_delim")
248248
end
249249
print(buffer, "$paren_delim)")
250250
print(buffer, return_type)
@@ -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))
@@ -379,29 +379,42 @@ function printmethod(buffer::IOBuffer, binding::Docs.Binding, func, method::Meth
379379
# or Tuple{Type{T}, String, Union{Nothing, Function}} where T<:Number
380380
# in the other case, it's usually something like Tuple{Vector{Int}}.
381381
argtypes = typesig isa UnionAll ?
382-
[get_typesig(t, t) for t in find_inner_tuple_type(typesig).types] :
383-
collect(typesig.types)
382+
[get_typesig(t, t) for t in find_inner_tuple_type(typesig).types] :
383+
collect(typesig.types)
384384

385-
args = map(args, argtypes) do arg,t
385+
args = map(args, argtypes) do arg, t
386386
type = ""
387387
suffix = ""
388388
if isvarargtype(t)
389389
t = vararg_eltype(t)
390390
suffix = "..."
391391
end
392-
if t!==Any
392+
if t !== Any
393393
type = "::$t"
394394
end
395395

396396
"$arg$type$suffix"
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)