Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring doesn't explain what the sig argument is for.

"""
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be a bit shorter/clearer as

 function doc(binding::Binding, sig::Type = Union{})
+    if defined(binding)
+        result = getdoc(resolve(binding), sig)
+        result === nothing || return Markdown.parse(result)
+    end
     results, groups = DocStr[], MultiDoc[]

with a default getdoc(obj, sig) = nothing method. Passing sig through to getdoc as well may be useful?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call.

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")