Skip to content

Commit

Permalink
Merge pull request #20142 from JuliaLang/jmm/dynamic_doc
Browse files Browse the repository at this point in the history
RFC: Add support for dynamic doc strings
  • Loading branch information
malmaud authored Jan 24, 2017
2 parents 003f70c + 31b2458 commit 3b97b4f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
16 changes: 16 additions & 0 deletions base/docs/Docs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,28 @@ end
# Docstring lookup.
# =================

"""
getdoc(x::T, sig) -> String
Return the dynamic docstring associated with object `x`, or `nothing` to use
the binding's documentation.
"""
getdoc(x, sig) = getdoc(x)
getdoc(x) = nothing

"""
Docs.doc(binding, sig)
Returns all documentation that matches both `binding` and `sig`.
If `getdoc` returns a non-`nothing` result on the value of the binding, then a
dynamic docstring is returned instead of one based on the binding itself.
"""
function doc(binding::Binding, sig::Type = Union{})
if defined(binding)
result = getdoc(resolve(binding), sig)
result === nothing || return result
end
results, groups = DocStr[], MultiDoc[]
# Lookup `binding` and `sig` for matches in all modules of the docsystem.
for mod in modules
Expand Down
14 changes: 14 additions & 0 deletions test/docs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -922,3 +922,17 @@ let save_color = Base.have_color
eval(Base, :(have_color = $save_color))
end
end


# Dynamic docstrings

type DynamicDocType
x
end

Base.Docs.getdoc(d::DynamicDocType) = Base.Markdown.parse(d.x)

dynamic_test = DynamicDocType("test 1")
@test docstrings_equal(@doc(dynamic_test), doc"test 1")
dynamic_test.x = "test 2"
@test docstrings_equal(@doc(dynamic_test), doc"test 2")

0 comments on commit 3b97b4f

Please sign in to comment.