@@ -19,13 +19,15 @@ const builtin_completions =
1919
2020const identifier_pattern = r" ^@?[_\p {L}][\p {Xwd}!]*+$"
2121
22+ _names(mod; all = false , imported = false ) = filter!(x -> ! Base. isdeprecated(mod, Symbol(x)), names(mod, all= all, imported= imported))
23+
2224moduleusings(mod) = ccall(:jl_module_usings, Any, (Any,), mod)
2325
24- filtervalid(names) = @>> names map(string) filter(x-> ismatch (identifier_pattern, x))
26+ filtervalid(names) = @>> names map(string) filter(x-> occursin (identifier_pattern, x))
2527
2628accessible(mod:: Module ) =
27- [names (mod, true , true );
28- map(names , moduleusings(mod)). .. ] |> unique |> filtervalid
29+ [_names (mod, all = true , imported = true );
30+ map(_names , moduleusings(mod)). .. ] |> unique |> filtervalid
2931
3032Base. getindex(b:: Binding ) = isdefined(b. mod, b. var) ? getfield(b. mod, b. var) : nothing
3133
@@ -37,7 +39,7 @@ completiontype(x) =
3739
3840const meta_cache = Dict{Any,Any}()
3941
40- function withmeta(completion:: AString , mod:: Module )
42+ function withmeta(completion:: AbstractString , mod:: Module )
4143 isdefined(mod, Symbol(completion)) || return completion
4244 b = Binding(mod, Symbol(completion))
4345 mod = b. mod
@@ -51,7 +53,7 @@ function withmeta(completion::AString, mod::Module)
5153 c
5254end
5355
54- for name in map(string, names (Base))
56+ for name in map(string, _names (Base))
5557 meta_cache[(Base, name)] = withmeta(name, Base)
5658end
5759
@@ -60,9 +62,9 @@ withmeta(completions::Vector, mod::Module) =
6062
6163function namecompletions_(mod:: Module , qualified = false )
6264 if ! qualified
63- [withmeta(filter!(x -> ! Base . isdeprecated(mod, Symbol(x)), accessible(mod) ), mod); builtin_completions]
65+ [withmeta(accessible(mod), mod); builtin_completions]
6466 else
65- withmeta(filter!(x -> ! Base . isdeprecated(mod, Symbol(x)), filtervalid(names (mod, true ) )), mod)
67+ withmeta(filtervalid(_names (mod, all = true )), mod)
6668 end
6769end
6870
@@ -92,11 +94,11 @@ function pathprefix(line)
9294end
9395
9496function stringmeta(cs, prefix)
95- map(c -> d (:text => c, :_prefix => prefix, :type => :file), cs)
97+ map(c -> Dict (:text => c, :_prefix => prefix, :type => :file), cs)
9698end
9799
98100function pathmeta(cs, path, prefix)
99- stringmeta(map(c -> replace(joinpath(path, c), r" ^\. /" , " " ), cs), prefix)
101+ stringmeta(map(c -> replace(joinpath(path, c), r" ^\. /" => " " ), cs), prefix)
100102end
101103
102104function children(dir, ext = " " ; depth = 0 , out = String[], prefix = " " )
@@ -140,37 +142,29 @@ function pathcompletions(line)
140142 elseif func == " open"
141143 pathmeta(children(joinpath(pwd(), dir)), dir, path)
142144 end
145+ catch e
146+ []
143147 end
144148end
145149
146150# Package Completions
147151# –––––––––––––––––––
152+ # not sure why this errors with "ArgumentError: Module Pkg3 not found in current path."
148153
149- packages(dir = Pkg. dir()) =
150- @>> dir readdir filter(x-> ! ismatch(r" ^\. |^METADATA$|^REQUIRE$" , x))
151-
152- all_packages() = packages(Pkg. dir(" METADATA" ))
154+ import Pkg
153155
154- required_packages() =
155- @>> Pkg. dir(" REQUIRE" ) readstring lines
156+ pkgmeta(xs) = [Dict(:text => x, :type => " package" ) for x in xs]
156157
157- unused_packages() = setdiff(all_packages(), required_packages())
158-
159- pkgmeta(xs) = map(x -> d(:text=> x, :type=> " package" ), xs)
158+ function stdlibs()
159+ srcdir = joinpath(Sys. BINDIR," .." ," .." ," stdlib" ," v0.7" )
160+ releasedir = joinpath(Sys. BINDIR," .." ," share" ," julia" ," stdlib" ," v0.7" )
161+ return ispath(srcdir) ? readdir(srcdir) :
162+ ispath(releasedir) ? readdir(releasedir) : []
163+ end
160164
161165function pkgcompletions(line)
162- if ismatch(r" ^using" , line)
163- return pkgmeta(packages())
164- end
165- m = funcprefix(line)
166- m == nothing && return
167- func, _ = m
168- if func in [" Pkg.add" , " Pkg.clone" , " Pkg.build" , " Pkg.test" ]
169- pkgmeta(all_packages())
170- elseif func in [" Pkg.pin" , " Pkg.checkout" ]
171- pkgmeta(packages())
172- elseif func in [" Pkg.rm" ]
173- pkgmeta(required_packages())
166+ if occursin(r" ^using|^import" , line)
167+ return pkgmeta(sort!(append!(collect(keys(Pkg. API. installed())), stdlibs())))
174168 end
175169end
176170
179173
180174const providers = [pkgcompletions, pathcompletions]
181175
182- function completions(line:: AString , mod:: Module = Main; default = true )
176+ function completions(line:: AbstractString , mod:: Module = Main; default = true )
183177 for provider in providers
184178 cs = provider(line)
185179 cs ≠ nothing && return cs
0 commit comments