DocStringExtensions
DocStringExtensions
— Module.Extensions for the Julia docsystem.
Introduction
This package provides a collection of useful extensions for Julia's built-in docsystem. These are features that are still regarded as "experimental" and not yet mature enough to be considered for inclusion in Base
, or that have sufficiently niche use cases that including them with the default Julia installation is not seen as valuable enough at this time.
Currently DocStringExtensions.jl
exports a collection of so-called "abbreviations", which can be used to add useful automatically generated information to docstrings. These include information such as:
- simplified method signatures;
- documentation for the individual fields of composite types;
- import and export lists for modules;
- and source-linked lists of methods applicable to a particular docstring.
Users are most welcome to suggest additional abbreviation ideas, or implement and submit them themselves. Julia's strong support for program introspection makes this a reasonably straight forward process.
Details of the currently available abbreviations can be viewed in their individual docstrings listed below in the "Exports" section.
Examples
In simple terms an abbreviation can be used by simply interpolating it into a suitable docstring. For example:
using DocStringExtensions
"""
A short summary of `func`...
$(SIGNATURES)
where `x` and `y` should both be positive.
# Details
Some details about `func`...
"""
func(x, y) = x + y
$(SIGNATURES)
will be replaced in the above docstring with
# Signatures
```julia
func(x, y)
```
The resulting generated content can be viewed via Julia's ?
mode or, if Documenter.jl
is set up, the generated external documentation.
The advantage of using SIGNATURES
(and other abbreviations) is that docstrings are less likely to become out-of-sync with the surrounding code. Note though that references to the argument names x
and y
that have been manually embedded within the docstring are, of course, not updated automatically.
Exports
DOCSTRING
EXPORTS
FIELDS
FUNCTIONNAME
IMPORTS
LICENSE
METHODLIST
README
SIGNATURES
TYPEDEF
TYPEDFIELDS
TYPEDSIGNATURES
@template
Imports
Base
Core
Index
DocStringExtensions.DOCSTRING
DocStringExtensions.EXPORTS
DocStringExtensions.FIELDS
DocStringExtensions.FUNCTIONNAME
DocStringExtensions.IMPORTS
DocStringExtensions.LICENSE
DocStringExtensions.METHODLIST
DocStringExtensions.README
DocStringExtensions.SIGNATURES
DocStringExtensions.TYPEDEF
DocStringExtensions.TYPEDFIELDS
DocStringExtensions.TYPEDSIGNATURES
DocStringExtensions.Abbreviation
DocStringExtensions.DocStringTemplate
DocStringExtensions.FunctionName
DocStringExtensions.License
DocStringExtensions.MethodList
DocStringExtensions.MethodSignatures
DocStringExtensions.ModuleExports
DocStringExtensions.ModuleImports
DocStringExtensions.Readme
DocStringExtensions.TypeDefinition
DocStringExtensions.TypeFields
DocStringExtensions.TypedMethodSignatures
DocStringExtensions.alltypesigs
DocStringExtensions.arguments
DocStringExtensions.cleanpath
DocStringExtensions.comparemethods
DocStringExtensions.format
DocStringExtensions.getmethods
DocStringExtensions.getmethods!
DocStringExtensions.groupby
DocStringExtensions.groupby!
DocStringExtensions.hook!
DocStringExtensions.hook!
DocStringExtensions.isabstracttype
DocStringExtensions.isbitstype
DocStringExtensions.keywords
DocStringExtensions.methodgroups
DocStringExtensions.parsedocs
DocStringExtensions.printmethod
DocStringExtensions.printmethod
DocStringExtensions.url
DocStringExtensions.@template
Reference
DocStringExtensions.DOCSTRING
— Constant.An Abbreviation
used in @template
definitions to represent the location of the docstring body that should be spliced into a template.
This abbreviation must only ever be used in template strings; never normal docstrings.
DocStringExtensions.EXPORTS
— Constant.An Abbreviation
to include all the exported names of a module is a sorted list of Documenter.jl
-style @ref
links.
The names are sorted alphabetically and ignore leading @
characters so that macros are not sorted before other names.
Examples
The markdown text generated by the EXPORTS
abbreviation looks similar to the following:
- [`bar`](@ref)
- [`@baz`](@ref)
- [`foo`](@ref)
DocStringExtensions.FIELDS
— Constant.An Abbreviation
to include the names of the fields of a type as well as any documentation that may be attached to the fields.
Examples
The generated markdown text should look similar to to following example where a type has three fields (x
, y
, and z
) and the last two have documentation attached.
- `x`
- `y`
Unlike the `x` field this field has been documented.
- `z`
Another documented field.
DocStringExtensions.FUNCTIONNAME
— Constant.An Abbreviation
for including the function name matching the method of the docstring.
Usage
This is mostly useful for not repeating the function name in docstrings where the user wants to retain full control of the argument list, or the latter does not exist (eg generic functions).
Note that the generated docstring snippet is not quoted, use indentation or explicit quoting.
Example
"""
$(FUNCTIONNAME)(d, θ)
Calculate the logdensity `d` at `θ`.
Users should define their own methods for `FUNCTIONNAME`.
"""
function logdensity end
DocStringExtensions.IMPORTS
— Constant.An Abbreviation
to include all the imported modules in a sorted list.
Examples
The markdown text generated by the IMPORTS
abbreviation looks similar to the following:
- `Foo`
- `Bar`
- `Baz`
DocStringExtensions.LICENSE
— Constant.LICENSE
An Abbreviation
for including the package LICENSE.md.
The LICENSE.md file is interpreted as "Julia flavored Markdown", which has some differences compared to GitHub flavored markdown, and, for example, [][] link shortcuts are not supported.
DocStringExtensions.METHODLIST
— Constant.An Abbreviation
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):
```julia
f(num)
```
defined at [`<path>:<line>`](<github-url>).
```julia
f(str)
```
defined at [`<path>:<line>`](<github-url>).
DocStringExtensions.README
— Constant.README
An Abbreviation
for including the package README.md.
The README.md file is interpreted as "Julia flavored Markdown", which has some differences compared to GitHub flavored markdown, and, for example, [][] link shortcuts are not supported.
DocStringExtensions.SIGNATURES
— Constant.An Abbreviation
for including a simplified representation of all the method signatures that match the given docstring. See printmethod
for details on the simplifications that are applied.
Examples
The generated markdown text will look similar to the following example where a function f
defines method taking two positional arguments, x
and y
, and two keywords, a
and the b
.
```julia
f(x, y; a, b...)
```
DocStringExtensions.TYPEDEF
— Constant.An Abbreviation
for including a summary of the signature of a type definition. Some of the following information may be included in the output:
- whether the object is an
abstract
type or abitstype
; - mutability (either
type
orstruct
is printed); - the unqualified name of the type;
- any type parameters;
- the supertype of the type if it is not
Any
.
Examples
The generated output for a type definition such as:
"""
$(TYPEDEF)
"""
struct MyType{S, T <: Integer} <: AbstractArray
# ...
end
will look similar to the following:
```julia
struct MyType{S, T<:Integer} <: AbstractArray
```
No information about the fields of the type is printed. Use the FIELDS
abbreviation to include information about the fields of a type.
DocStringExtensions.TYPEDFIELDS
— Constant.Identical to FIELDS
except that it includes the field types.
Examples
The generated markdown text should look similar to to following example where a type has three fields; x
of type String
, y
of type Int
, and z
of type Vector{Any}
.
- `x::String`
- `y::Int`
Unlike the `x` field this field has been documented.
- `z::Array{Any, 1}`
Another documented field.
DocStringExtensions.TYPEDSIGNATURES
— Constant.An Abbreviation
for including a simplified representation of all the method signatures with types that match the given docstring. See printmethod
for details on the simplifications that are applied.
Examples
The generated markdown text will look similar to the following example where a function f
defines method taking two positional arguments, x
and y
, and two keywords, a
and the b
.
```julia
f(x::Int, y::Int; a, b...)
```
DocStringExtensions.@template
— Macro.Defines a docstring template that will be applied to all docstrings in a module that match the specified category or tuple of categories.
Examples
@template DEFAULT =
"""
$(SIGNATURES)
$(DOCSTRING)
"""
DEFAULT
is the default template that is applied to a docstring if no other template definitions match the documented expression. The DOCSTRING
abbreviation is used to mark the location in the template where the actual docstring body will be spliced into each docstring.
@template (FUNCTIONS, METHODS, MACROS) =
"""
$(SIGNATURES)
$(DOCSTRING)
$(METHODLIST)
"""
A tuple of categories can be specified when a docstring template should be used for several different categories.
@template MODULES = ModName
The template definition above will define a template for module docstrings based on the template for modules found in module ModName
.
Supported categories are DEFAULT
, FUNCTIONS
, METHODS
, MACROS
, TYPES
, MODULES
, and CONSTANTS
.
DocStringExtensions.alltypesigs
— Method.alltypesigs(sig)
Returns a Vector
of the Tuple
types contained in sig
.
DocStringExtensions.arguments
— Method.arguments(m)
Returns the list of arguments for a particular method m
.
Examples
f(x; a = 1, b...) = x
args = arguments(first(methods(f)))
DocStringExtensions.cleanpath
— Method.cleanpath(path)
Remove the Pkg.dir
part of a file path
if it exists.
DocStringExtensions.comparemethods
— Method.comparemethods(a, b)
Compare methods a
and b
by file and line number.
DocStringExtensions.format
— Method.format(abbr, buf, doc)
Expand the Abbreviation
abbr
in the context of the DocStr
doc
and write the resulting markdown-formatted text to the IOBuffer
buf
.
DocStringExtensions.getmethods!
— Method.getmethods!(results, f, sig)
A helper method for getmethods
that collects methods in results
.
DocStringExtensions.getmethods
— Method.getmethods(f, sig)
Collect and return all methods of function f
matching signature sig
.
This is similar to methods(f, sig)
, but handles type signatures found in DocStr
objects more consistently that methods
.
DocStringExtensions.groupby!
— Method.groupby!(f, groups, data)
A helper method for groupby
that uses a pre-allocated groups
Dict
.
DocStringExtensions.groupby
— Method.groupby(f, K, V, data)
Group data
using function f
where key type is specified by K
and group type by V
.
The function f
takes a single argument, an element of data
, and should return a 2-tuple of (computed_key, element)
. See the example below for details.
Examples
groupby(Int, Vector{Int}, collect(1:10)) do num
mod(num, 3), num
end
DocStringExtensions.hook!
— Method.hook!(func)
Set the docstring expander function to first call func
before calling the default expander.
To remove a hook that has been applied using this method call hook!()
.
DocStringExtensions.hook!
— Method.hook!()
Reset the docstring expander to only call the default expander function. This clears any 'hook' that has been set using hook!(func)
.
DocStringExtensions.isabstracttype
— Method.isabstracttype(t)
Is the type t
an abstract
type?
DocStringExtensions.isbitstype
— Method.isbitstype(t)
Is the type t
a bitstype
?
DocStringExtensions.keywords
— Method.keywords(func, m)
Returns the list of keywords for a particular method m
of a function func
.
Examples
f(x; a = 1, b...) = x
kws = keywords(f, first(methods(f)))
DocStringExtensions.methodgroups
— Method.methodgroups(func, typesig, modname; exact)
Group all methods of function func
with type signatures typesig
in module modname
. Keyword argument exact = true
matches signatures "exactly" with ==
rather than <:
.
Examples
groups = methodgroups(f, Union{Tuple{Any}, Tuple{Any, Integer}}, Main; exact = false)
DocStringExtensions.parsedocs
— Method.parsedocs(mod)
Parse all docstrings defined within a module mod
.
DocStringExtensions.printmethod
— Method.printmethod(buffer::Base.GenericIOBuffer{Array{UInt8,1}}, binding::Base.Docs.Binding, func::Any, method::Method, typesig::Any) -> Base.GenericIOBuffer{Array{UInt8,1}}
Print a simplified representation of a method signature to buffer
. Some of these simplifications include:
- no
TypeVar
s; - no types;
- no keyword default values;
?
printed where#unused#
arguments are found.
Examples
f(x::Int; a = 1, b...) = x
sig = printmethod(Docs.Binding(Main, :f), f, first(methods(f)))
DocStringExtensions.printmethod
— Method.printmethod(buffer, binding, func, method)
Print a simplified representation of a method signature to buffer
. Some of these simplifications include:
- no
TypeVar
s; - no types;
- no keyword default values;
?
printed where#unused#
arguments are found.
Examples
f(x; a = 1, b...) = x
sig = printmethod(Docs.Binding(Main, :f), f, first(methods(f)))
DocStringExtensions.url
— Method.url(m)
Get the URL (file and line number) where a method m
is defined.
Note that this is based on the implementation of Base.url
, but handles URLs correctly on TravisCI as well.
DocStringExtensions.Abbreviation
— Type.Abbreviation objects are used to automatically generate context-dependent markdown content within documentation strings. Objects of this type interpolated into docstrings will be expanded automatically before parsing the text to markdown.
The singleton type for DOCSTRING
abbreviations.
DocStringExtensions.FunctionName
— Type.The singleton type for FUNCTIONNAME
abbreviations.
DocStringExtensions.License
— Type.The singleton type for LICENSE
abbreviations.
DocStringExtensions.MethodList
— Type.The singleton type for METHODLIST
abbreviations.
The singleton type for SIGNATURES
abbreviations.
The singleton type for EXPORTS
abbreviations.
The singleton type for IMPORTS
abbreviations.
DocStringExtensions.Readme
— Type.The singleton type for README
abbreviations.
The singleton type for TYPEDEF
abbreviations.
DocStringExtensions.TypeFields
— Type.The type for FIELDS
abbreviations.
types
The singleton type for TYPEDSIGNATURES
abbreviations.