Skip to content

Commit

Permalink
add test cases and fixup completion code
Browse files Browse the repository at this point in the history
  • Loading branch information
aviatesk committed Sep 3, 2021
1 parent 9341c94 commit 66b9235
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
20 changes: 10 additions & 10 deletions stdlib/REPL/src/REPLCompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ function appendmacro!(syms, macros, needle, endchar)
end
end

function filtered_mod_names(ffunc::Function, mod::Module, name::AbstractString, all::Bool = false, imported::Bool = false, usings::Bool = false)
ssyms = names(mod, all = all, imported = imported, usings = usings)
function filtered_mod_names(ffunc::Function, mod::Module, name::AbstractString; kwargs...)
ssyms = names(mod; kwargs...)
filter!(ffunc, ssyms)
syms = String[string(s) for s in ssyms]
macros = filter(x -> startswith(x, "@" * name), syms)
Expand Down Expand Up @@ -165,14 +165,14 @@ function complete_symbol(sym::String, ffunc, context_module::Module=Main)
end
# Looking for a binding in a module
if mod == context_module
# Also look in modules we got through `using`
mods = ccall(:jl_module_usings, Any, (Any,), context_module)::Vector
for m in mods
append!(suggestions, filtered_mod_names(p, m::Module, name)
end
append!(suggestions, filtered_mod_names(p, mod, name, true, true, true))
else
append!(suggestions, filtered_mod_names(p, mod, name, true, false, true))
# special case `Core` and `Base` bindings
# - `Core` bindings should be added to every modules
# - `Base` bindings should be added to non-bare modules
append!(suggestions, filtered_mod_names(p, Core, name))
isdefined(mod, :Base) && append!(suggestions, filtered_mod_names(p, Base, name))
append!(suggestions, filtered_mod_names(p, mod, name; all=true, imported=true, usings=true))
else # dot-accessed module context
append!(suggestions, filtered_mod_names(p, mod, name; all=true, imported=false, usings=true))
end
elseif val !== nothing # looking for a property of an instance
for property in propertynames(val, false)
Expand Down
37 changes: 37 additions & 0 deletions stdlib/REPL/test/replcompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1162,3 +1162,40 @@ end
@test_broken length(c) == fieldcount(Regex)
end
end

@testset "complete names from `using`" begin
test_complete_context1(args...; kwargs...) = first(test_complete_context(args...; kwargs...))

let # should work for arbitrary module
M = Module()

use = @eval M module use end
@eval use module def; foo = 42; end

@assert count(==("foo"), test_complete_context1("f", use)) 1
@assert count(==("foo"), test_complete_context1("use.f", M)) 1
@eval use using .def: foo
@test count(==("foo"), test_complete_context1("f", use)) == 1
@test count(==("foo"), test_complete_context1("use.f", M)) == 1 # should work for even dot-accessed module context
end

let # should work for packages
M = Module()

@assert count(==("fuzzyscore"), test_complete_context1("fuzzy", M)) 1
@eval M using REPL: fuzzyscore
@test count(==("fuzzyscore"), test_complete_context1("fuzzy", M)) == 1

@assert count(==("completions"), test_complete_context1("comp", M)) 1
@eval M using REPL.REPLCompletions: completions
@test count(==("completions"), test_complete_context1("comp", M)) == 1
end

let # should for `Base` binding
M = Module()

@assert count(==("@aggressive_constprop"), test_complete_context1("@aggressive_", M)) 1
@eval M using Base: @aggressive_constprop
@test count(==("@aggressive_constprop"), test_complete_context1("@aggressive_", M)) == 1
end
end

0 comments on commit 66b9235

Please sign in to comment.