Skip to content

Commit

Permalink
Merge pull request #12062 from JuliaLang/omm/meta-gensym
Browse files Browse the repository at this point in the history
Use a gensym to store doc metadata
  • Loading branch information
MikeInnes committed Jul 9, 2015
2 parents 686bd24 + bdbf09e commit 477e861
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
28 changes: 16 additions & 12 deletions base/docs/Docs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ export doc

const modules = Module[]

meta() = current_module().META
const META′ = gensym("META")

@eval meta(mod) = mod.$META′

meta() = meta(current_module())

macro init()
META = esc(:META)
META = esc(META)
quote
if !isdefined(:META)
if !isdefined($(Expr(:quote, META′)))
const $META = ObjectIdDict()
doc!($META, @doc_str $("Documentation metadata for `$(current_module())`."))
push!(modules, current_module())
Expand All @@ -30,7 +34,7 @@ end

function doc(obj)
for mod in modules
haskey(mod.META, obj) && return mod.META[obj]
haskey(meta(mod), obj) && return meta(mod)[obj]
end
end

Expand Down Expand Up @@ -97,8 +101,8 @@ end
function doc(f::Function)
docs = []
for mod in modules
if haskey(mod.META, f)
fd = mod.META[f]
if haskey(meta(mod), f)
fd = meta(mod)[f]
length(docs) == 0 && fd.main != nothing && push!(docs, fd.main)
if isa(fd, FuncDoc)
for m in fd.order
Expand All @@ -114,8 +118,8 @@ end

function doc(f::Function, m::Method)
for mod in modules
haskey(mod.META, f) && isa(mod.META[f], FuncDoc) && haskey(mod.META[f].meta, m) &&
return mod.META[f].meta[m]
haskey(meta(mod), f) && isa(meta(mod)[f], FuncDoc) && haskey(meta(mod)[f].meta, m) &&
return meta(mod)[f].meta[m]
end
end

Expand Down Expand Up @@ -168,8 +172,8 @@ end
function doc(f::DataType)
docs = []
for mod in modules
if haskey(mod.META, f)
fd = mod.META[f]
if haskey(meta(mod), f)
fd = meta(mod)[f]
if isa(fd, TypeDoc)
length(docs) == 0 && fd.main != nothing && push!(docs, fd.main)
for m in fd.order
Expand All @@ -189,8 +193,8 @@ isfield(x) = isexpr(x, :.) &&

function fielddoc(T, k)
for mod in modules
if haskey(mod.META, T) && isa(mod.META[T], TypeDoc) && haskey(mod.META[T].fields, k)
return mod.META[T].fields[k]
if haskey(meta(mod), T) && isa(meta(mod)[T], TypeDoc) && haskey(meta(mod)[T].fields, k)
return meta(mod)[T].fields[k]
end
end
Text(sprint(io -> (print(io, "$T has fields: ");
Expand Down
24 changes: 13 additions & 11 deletions test/docs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,12 @@ const K = :K

end

@test DocsTest.META[DocsTest] == doc"DocsTest"
import Base.Docs: meta

@test meta(DocsTest)[DocsTest] == doc"DocsTest"

let f = DocsTest.f
funcdoc = DocsTest.META[f]
funcdoc = meta(DocsTest)[f]
order = [methods(f, sig)[1] for sig in [(Any,), (Any, Any)]]
@test funcdoc.main == nothing
@test funcdoc.order == order
Expand All @@ -154,40 +156,40 @@ let f = DocsTest.f
end

let g = DocsTest.g
funcdoc = DocsTest.META[g]
funcdoc = meta(DocsTest)[g]
@test funcdoc.main == doc"g"
end

let AT = DocsTest.AT
@test DocsTest.META[AT] == doc"AT"
@test meta(DocsTest)[AT] == doc"AT"
end

let BT = DocsTest.BT
@test DocsTest.META[BT] == doc"BT"
@test meta(DocsTest)[BT] == doc"BT"
end

let T = DocsTest.T
typedoc = DocsTest.META[T]
typedoc = meta(DocsTest)[T]
@test typedoc.main == doc"T"
@test typedoc.fields[:x] == doc"T.x"
@test typedoc.fields[:y] == doc"T.y"
end

let IT = DocsTest.IT
typedoc = DocsTest.META[IT]
typedoc = meta(DocsTest)[IT]
@test typedoc.main == doc"IT"
@test typedoc.fields[:x] == doc"IT.x"
@test typedoc.fields[:y] == doc"IT.y"
end

let TA = DocsTest.TA
@test DocsTest.META[TA] == doc"TA"
@test meta(DocsTest)[TA] == doc"TA"
end

let mac = getfield(DocsTest, symbol("@mac"))
funcdoc = DocsTest.META[mac]
funcdoc = meta(DocsTest)[mac]
@test funcdoc.main == doc"@mac"
end

@test DocsTest.META[:G] == doc"G"
@test DocsTest.META[:K] == doc"K"
@test meta(DocsTest)[:G] == doc"G"
@test meta(DocsTest)[:K] == doc"K"

0 comments on commit 477e861

Please sign in to comment.