Skip to content

Commit

Permalink
Simplify @repl implementation.
Browse files Browse the repository at this point in the history
Enable `apropos` search via help mode by passing a string or regex:

    help?> "foo"
    help?> r"foo"

This shouldn't interfere with any docstring lookup since we don't encourage
documenting specific strings or regex objects. Docs for `@r_str` can still
be found by searching for either an empty regex `r""` or `@r_str` directly.

Enable specifying method signatures using

    help?> f(::Float64, ::Int)

as well as the current behaviour

    help?> f(x, y)

where `x` and `y` are already defined. This is quite useful when you don't
have an instance of the required type. Prior to this an error was thrown
in `gen_call_with_extracted_types` when using the `::` syntax.

(cherry picked from commit 8c236e9)
  • Loading branch information
MichaelHatherly authored and tkelman committed Sep 28, 2015
1 parent 13b75de commit 035c10d
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions base/docs/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,40 @@ end

repl_corrections(s) = repl_corrections(STDOUT, s)

macro repl(ex)
macro repl(ex) repl(ex) end

function repl(s::Symbol)
quote
# Fuzzy Searching
$(isexpr(ex, Symbol)) && repl_search($(string(ex)))
if $(isa(ex, Symbol)) &&
!(isdefined($(current_module()), $(Expr(:quote, ex))) ||
haskey(keywords, $(Expr(:quote, ex))))
repl_corrections($(string(ex)))
else
if $(isfield(ex) ? :(isa($(esc(ex.args[1])), DataType)) : false)
$(isfield(ex) ? :(fielddoc($(esc(ex.args[1])), $(ex.args[2]))) : nothing)
repl_search($(string(s)))
($(isdefined(s) || haskey(keywords, s))) || repl_corrections($(string(s)))
$(_repl(s))
end
end

isregex(x) = isexpr(x, :macrocall, 2) && x.args[1] == symbol("@r_str") && !isempty(x.args[2])

repl(ex::Expr) = isregex(ex) ? :(apropos($ex)) : _repl(ex)

repl(str::AbstractString) = :(apropos($str))

repl(other) = nothing

function _repl(x)
docs = :(@doc $(esc(x)))
try
# Handles function call syntax where each argument is a symbol.
isexpr(x, :call) && (docs = Base.gen_call_with_extracted_types(doc, x))
end
if isfield(x)
quote
if isa($(esc(x.args[1])), DataType)
fielddoc($(esc(x.args[1])), $(esc(x.args[2])))
else
$((isa(ex,Symbol) || isfield(ex) || isexpr(ex,:macrocall)) ? :(@doc ($(esc(ex)))) : Base.gen_call_with_extracted_types(doc, ex))
$docs
end
end
else
docs
end
end

Expand Down

0 comments on commit 035c10d

Please sign in to comment.