Skip to content

Commit 5c0e188

Browse files
committed
Allow instance docstrings
Duplicates improvement made to Base's `Enum`s: JuliaLang/julia#61955
1 parent 02d2c9c commit 5c0e188

3 files changed

Lines changed: 38 additions & 6 deletions

File tree

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ version = "0.1.9"
77
julia = "1"
88

99
[extras]
10+
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
1011
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
1112
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1213

1314
[targets]
14-
test = ["Serialization", "Test"]
15+
test = ["Markdown", "Serialization", "Test"]

src/BitFlags.jl

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,20 @@ function _bitflag_impl(__module__::Module, __source__, scope::Union{Symbol, Noth
259259
maskzero, maskother = false, zero(basetype)
260260
i = oneunit(basetype)
261261
two = oneunit(basetype) + oneunit(basetype)
262+
docs = Dict{Symbol,Expr}()
262263

263264
for s in syms
264265
s isa LineNumberNode && continue
266+
if isexpr(s, :macrocall, 4) && (
267+
sa = s.args[1];
268+
sa === Symbol("@doc") || sa == GlobalRef(Core, Symbol("@doc"))
269+
)
270+
doc = s
271+
doc.args[3] = Core.eval(__module__, doc.args[3]) # evaluates string macros, e.g. md"doc string"
272+
s = s.args[4]
273+
else
274+
doc = nothing
275+
end
265276
if s isa Symbol
266277
if (i == typemin(basetype)) && (maskother & typemax(basetype) != 0)
267278
throw(ArgumentError("overflow in value \"$s\" of BitFlag $typename"))
@@ -303,6 +314,9 @@ function _bitflag_impl(__module__::Module, __source__, scope::Union{Symbol, Noth
303314
lo = min(lo, i)
304315
hi = max(hi, i)
305316
end
317+
if doc !== nothing
318+
docs[sym] = doc
319+
end
306320
i = iszero(i) ? oneunit(i) : two*i
307321
end
308322

@@ -322,7 +336,12 @@ function _bitflag_impl(__module__::Module, __source__, scope::Union{Symbol, Noth
322336
@inbounds for ii in 1:length(names)
323337
sym, val = names[ii], values[ii]
324338
instances[ii] = :(bitcast($etypename, $val))
325-
flagconsts[ii] = :(const $(esc(sym)) = bitcast($etypename, $val))
339+
flagex = :(const $(esc(sym)) = bitcast($etypename, $val))
340+
if haskey(docs, sym)
341+
docs[sym].args[4] = flagex
342+
flagex = docs[sym]
343+
end
344+
flagconsts[ii] = flagex
326345
end
327346
namemap = NamedTuple{(names...,)}((values...,))
328347

test/runtests.jl

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
using BitFlags
2-
using Test, Serialization
2+
using Markdown
3+
using Serialization
4+
using Test
35

46
# workaround for https://github.com/JuliaLang/julia/issues/54664
57
function extractdoc(doc)
68
# On v1.11 without REPL loaded, the @doc macro returns a Base.Docs.DocStr object;
79
# extract the stored string from the object.
8-
doc isa Base.Docs.DocStr && return doc.text[1]
10+
if doc isa Base.Docs.DocStr
11+
doc = !isnothing(doc.object) ? doc.object : first(doc.text)
12+
end
913
# Otherwise, assume we get something like a Markdown.MD object and just turn it into
1014
# a string. (Strip trailing newline for consistency with above form.)
1115
return strip(string(doc))
@@ -104,11 +108,19 @@ end
104108

105109
#@testset "Documentation" begin
106110
# docstring literal
107-
"""My Docstring""" @bitflag DocFlag1 docflag1a
111+
"""My Docstring""" @bitflag DocFlag1 begin
112+
"""first flag"""
113+
docflag1a
114+
end
108115
@test extractdoc(@doc(DocFlag1)) == "My Docstring"
116+
@test extractdoc(@doc(docflag1a)) == "first flag"
109117
# docstring macro for non-string literals
110-
@doc raw"""Raw Docstring""" @bitflag DocFlag2 docflag2a
118+
@doc raw"""Raw Docstring""" @bitflag DocFlag2 begin
119+
@doc md"""**second flag**"""
120+
docflag2a
121+
end
111122
@test extractdoc(@doc(DocFlag2)) == "Raw Docstring"
123+
@test extractdoc(@doc(docflag2a)) == "**second flag**"
112124
#end
113125

114126
#@testset "Type properties" begin

0 commit comments

Comments
 (0)