Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve REPL completions #30569

Merged
merged 4 commits into from
Apr 7, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
23 changes: 17 additions & 6 deletions stdlib/REPL/src/REPLCompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ get_value(sym, fn) = (sym, true)
function get_value_getfield(ex::Expr, fn)
# Example :((top(getfield))(Base,:max))
val, found = get_value_getfield(ex.args[2],fn) #Look up Base in Main and returns the module
found || return (nothing, false)
(found && length(ex.args) >= 3) || return (nothing, false)
return get_value_getfield(ex.args[3], val) #Look up max in Base and returns the function if found.
end
get_value_getfield(sym, fn) = get_value(sym, fn)
Expand Down Expand Up @@ -407,7 +407,7 @@ function try_get_type(sym::Expr, fn::Module)
elseif sym.head === :ref
# some simple cases of `expand`
return try_get_type(Expr(:call, GlobalRef(Base, :getindex), sym.args...), fn)
elseif sym.head === :.
elseif sym.head === :. && sym.args[2] isa QuoteNode # second check catches broadcasting
return try_get_type(Expr(:call, GlobalRef(Core, :getfield), sym.args...), fn)
end
return (Any, false)
Expand All @@ -432,7 +432,14 @@ function complete_methods(ex_org::Expr, context_module=Main)::Vector{Completion}
args_ex = Any[]
func, found = get_value(ex_org.args[1], context_module)
!found && return Completion[]
for ex in ex_org.args[2:end]

funargs = ex_org.args[2:end]
# handle broadcasting
if ex_org.head === :. && ex_org.args[2] isa Expr
funargs = ex_org.args[2].args
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't seem right to me --- in f.(args the function won't necessarily be called on those arguments, but their elements (if arrays). This is a bit tricky to handle, so I'd leave it out for now.

Copy link
Member Author

Choose a reason for hiding this comment

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

Doh, of course. I've pushed a commit such that we always return all methods that match the number of arguments and completely disregard any argument types.

IMHO that's better than not having any autocompletions for broadcasted functions, but lemme know if you don't think so and I'll remove that codepath :)

end

for ex in funargs
val, found = get_type(ex, context_module)
push!(args_ex, val)
end
Expand Down Expand Up @@ -610,12 +617,16 @@ function completions(string, pos, context_module=Main)::Completions

# Make sure that only bslash_completions is working on strings
inc_tag==:string && return String[], 0:-1, false

if inc_tag == :other && should_method_complete(partial)
frange, method_name_end = find_start_brace(partial)
ex = Meta.parse(partial[frange] * ")", raise=false, depwarn=false)
if isa(ex, Expr) && ex.head==:call
return complete_methods(ex, context_module), first(frange):method_name_end, false

if isa(ex, Expr)
if ex.head==:call
return complete_methods(ex, context_module), first(frange):method_name_end, false
elseif ex.head==:. && ex.args[2] isa Expr && ex.args[2].head==:tuple
return complete_methods(ex, context_module), first(frange):(method_name_end - 1), false
end
end
elseif inc_tag == :comment
return Completion[], 0:-1, false
Expand Down
19 changes: 19 additions & 0 deletions stdlib/REPL/test/replcompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,16 @@ let s = "type_test.xx.y"
@test s[r] == "y"
end

let s = ":(function foo(::Int) end).args[1].args[2]."
c, r = test_complete_context(s)
@test c == Any[]
end

let s = "log(log.(x),"
c, r = test_complete_context(s)
@test !isempty(c)
end

let s = "Base.return_types(getin"
c, r = test_complete_context(s)
@test "getindex" in c
Expand All @@ -981,6 +991,15 @@ let s = "test(1,1, "
@test s[r] == "test"
end

let s = "test.(1,1, "
c, r, res = test_complete_context(s)
@test !res
@test c[1] == string(first(methods(Main.CompletionFoo.test, Tuple{Int, Int})))
@test length(c) == 3
@test r == 1:4
@test s[r] == "test"
end

let s = "prevind(\"θ\",1,"
c, r, res = test_complete_context(s)
@test c[1] == string(first(methods(prevind, Tuple{String, Int})))
Expand Down