Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add typed method list abbreviation #153

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions src/abbreviations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,116 @@
end








#
# `TypedMethodList`
#

"""
The singleton type for [`TYPEDMETHODLIST`](@ref) abbreviations.

$(:FIELDS)
"""
struct TypedMethodList <: Abbreviation end

"""
An [`Abbreviation`](@ref) for including a list of all the methods that match a documented
`Method`, `Function`, or `DataType` within the current module.

# Examples

The generated markdown text will look similar to the following example where a function
`f` defines two different methods (one that takes a number, and the other a string):

````markdown
```julia
f(x::Int)
```

defined at [`<path>:<line>`](<github-url>).

```julia
f(y::String)
```

defined at [`<path>:<line>`](<github-url>).
````
"""
const TYPEDMETHODLIST = TypedMethodList()

function format(::MethodList, buf, doc)
local binding = doc.data[:binding]
local typesig = doc.data[:typesig]
local modname = doc.data[:module]
local func = Docs.resolve(binding)
local groups = methodgroups(func, typesig, modname; exact = false)
if !isempty(groups)

#START OF COPIED CODE FROM TYPEDMETHODSIGNATURES
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@unnamedunknownusername could this code just be refactored out into a reusable function rather than copying it across?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MichaelHatherly Have seperated out repeated code into a resusable function let me know if I have missed anything

group = groups[end]
println(buf)
println(buf, "```julia")
for (i, method) in enumerate(group)
N = length(arguments(method))
# return a list of tuples that represent type signatures
tuples = find_tuples(typesig)
# The following will find the tuple that matches the number of arguments in the function
# ideally we would check that the method signature matches the Tuple{...} signature
# but that is not straightforward because of how expressive Julia can be
function f(t)
if t isa DataType
return t <: Tuple && length(t.types) == N
elseif t isa UnionAll
return f(t.body)
else
return false

Check warning on line 351 in src/abbreviations.jl

View check run for this annotation

Codecov / codecov/patch

src/abbreviations.jl#L351

Added line #L351 was not covered by tests
end
end

@static if Sys.iswindows() && VERSION < v"1.8"

Check warning on line 355 in src/abbreviations.jl

View check run for this annotation

Codecov / codecov/patch

src/abbreviations.jl#L355

Added line #L355 was not covered by tests
t = tuples[findlast(f, tuples)]
else
t = tuples[findfirst(f, tuples)]
end
printmethod(buf, binding, func, method, t)

println(buf)
end
#END OF COPIED CODE FROM TYPEDMETHODSIGNATURES

println(buf, "```\n")
if !isempty(group)
local method = group[1]
local file = string(method.file)
local line = method.line
local path = cleanpath(file)
local URL = url(method)
isempty(URL) || println(buf, "defined at [`$path:$line`]($URL).")
end
println(buf)
end
return nothing
end















#
# `MethodSignatures`
#
Expand Down