Skip to content

Commit 8e9da56

Browse files
tpappmortenpi
authored andcommitted
Add the FUNCTIONNAME abbreviation. (#66)
Fixes #59.
1 parent 75638c6 commit 8e9da56

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

src/DocStringExtensions.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ module DocStringExtensions
7777

7878
# Exports.
7979

80-
export @template, FIELDS, EXPORTS, METHODLIST, IMPORTS, SIGNATURES, TYPEDEF, DOCSTRING
81-
80+
export @template, FIELDS, EXPORTS, METHODLIST, IMPORTS, SIGNATURES, TYPEDEF, DOCSTRING, FUNCTIONNAME
8281

8382
# Includes.
8483

src/abbreviations.jl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,46 @@ function format(::MethodSignatures, buf, doc)
303303
end
304304
end
305305

306+
#
307+
# `FunctionName`
308+
#
309+
310+
"""
311+
The singleton type for [`FUNCTIONNAME`](@ref) abbreviations.
312+
313+
$(:FIELDS)
314+
"""
315+
struct FunctionName <: Abbreviation end
316+
317+
"""
318+
An [`Abbreviation`](@ref) for including the function name matching the method of
319+
the docstring.
320+
321+
# Usage
322+
323+
This is mostly useful for not repeating the function name in docstrings where
324+
the user wants to retain full control of the argument list, or the latter does
325+
not exist (eg generic functions).
326+
327+
Note that the generated docstring snippet is not quoted, use indentation or
328+
explicit quoting.
329+
330+
# Example
331+
332+
```julia
333+
\"""
334+
\$(FUNCTIONNAME)(d, θ)
335+
336+
Calculate the logdensity `d` at `θ`.
337+
338+
Users should define their own methods for `$(FUNCTIONNAME)`.
339+
\"""
340+
function logdensity end
341+
```
342+
"""
343+
const FUNCTIONNAME = FunctionName()
344+
345+
format(::FunctionName, buf, doc) = print(buf, doc.data[:binding].var)
306346

307347
#
308348
# `TypeSignature`

test/tests.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,17 @@ end
182182
@test occursin("\n```\n", str)
183183
end
184184

185+
@testset "function names" begin
186+
doc.data = Dict(
187+
:binding => Docs.Binding(M, :f),
188+
:typesig => Tuple{Any},
189+
:module => M,
190+
)
191+
DSE.format(FUNCTIONNAME, buf, doc)
192+
str = String(take!(buf))
193+
@test str == "f"
194+
end
195+
185196
@testset "type definitions" begin
186197
doc.data = Dict(
187198
:binding => Docs.Binding(M, :AbstractType),

0 commit comments

Comments
 (0)