Skip to content

Commit c061537

Browse files
Richard Palethorpe (rpalethorpe)mortenpi
authored andcommitted
Add TYPEDFIELDS to display field names (#77)
1 parent c6ac7cd commit c061537

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

src/DocStringExtensions.jl

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

7878
# Exports.
7979

80-
export @template, FIELDS, EXPORTS, METHODLIST, IMPORTS, SIGNATURES, TYPEDSIGNATURES, TYPEDEF, DOCSTRING, FUNCTIONNAME
80+
export @template, FIELDS, TYPEDFIELDS, EXPORTS, METHODLIST, IMPORTS
81+
export SIGNATURES, TYPEDSIGNATURES, TYPEDEF, DOCSTRING, FUNCTIONNAME
8182
export README, LICENSE
8283

8384
# Includes.

src/abbreviations.jl

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ Docs.formatdoc(buf::IOBuffer, doc::Docs.DocStr, part::Abbreviation) = format(par
3535
#
3636

3737
"""
38-
The singleton type for [`FIELDS`](@ref) abbreviations.
38+
The type for [`FIELDS`](@ref) abbreviations.
3939
4040
$(:FIELDS)
4141
"""
42-
struct TypeFields <: Abbreviation end
42+
struct TypeFields <: Abbreviation
43+
types::Bool
44+
end
4345

4446
"""
4547
An [`Abbreviation`](@ref) to include the names of the fields of a type as well as any
@@ -64,9 +66,33 @@ attached.
6466
Another documented field.
6567
```
6668
"""
67-
const FIELDS = TypeFields()
69+
const FIELDS = TypeFields(false)
70+
71+
"""
72+
Identical to [`FIELDS`](@ref) except that it includes the field types.
73+
74+
# Examples
75+
76+
The generated markdown text should look similar to to following example where
77+
a type has three fields; `x` of type `String`, `y` of type `Int`, and `z` of
78+
type `Vector{Any}`.
79+
80+
```markdown
81+
82+
- `x::String`
83+
84+
- `y::Int`
6885
69-
function format(::TypeFields, buf, doc)
86+
Unlike the `x` field this field has been documented.
87+
88+
- `z::Array{Any, 1}`
89+
90+
Another documented field.
91+
```
92+
"""
93+
const TYPEDFIELDS = TypeFields(true)
94+
95+
function format(abbrv::TypeFields, buf, doc)
7096
local docs = get(doc.data, :fields, Dict())
7197
local binding = doc.data[:binding]
7298
local object = Docs.resolve(binding)
@@ -76,7 +102,11 @@ function format(::TypeFields, buf, doc)
76102
if !isempty(fields)
77103
println(buf)
78104
for field in fields
79-
print(buf, " - `", field, "`\n")
105+
if abbrv.types
106+
println(buf, " - `", field, "::", fieldtype(object, field), "`")
107+
else
108+
println(buf, " - `", field, "`")
109+
end
80110
# Print the field docs if they exist and aren't a `doc"..."` docstring.
81111
if haskey(docs, field) && isa(docs[field], AbstractString)
82112
println(buf)

test/tests.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ end
9999
@test occursin(" - `c`", str)
100100
@test occursin("one", str)
101101
@test occursin("two", str)
102+
103+
DSE.format(TYPEDFIELDS, buf, doc)
104+
str = String(take!(buf))
105+
@test occursin(" - `a::Any`", str)
106+
@test occursin(" - `b::Any`", str)
107+
@test occursin(" - `c::Any`", str)
108+
@test occursin("one", str)
109+
@test occursin("two", str)
102110
end
103111

104112
@testset "method lists" begin

0 commit comments

Comments
 (0)