Skip to content

Commit 88240a4

Browse files
authored
Merge pull request #185 from julia-vscode/sp/improvenamefinding
improved name resolution
2 parents cbcdc99 + afefb9d commit 88240a4

File tree

1 file changed

+35
-33
lines changed

1 file changed

+35
-33
lines changed

src/symbols.jl

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ function maybe_fixup_stdlib_path(path)
135135
end
136136

137137
const _global_method_cache = IdDict{Any,Vector{Any}}()
138-
function methodinfo(@nospecialize(f); types=Tuple, world=typemax(UInt))
138+
function methodinfo(@nospecialize(f); types = Tuple, world = typemax(UInt))
139139
key = (f, types, world)
140140
cached = get(_global_method_cache, key, nothing)
141141
if cached === nothing
@@ -158,7 +158,7 @@ function cache_methods(@nospecialize(f), name, env)
158158
world = typemax(UInt)
159159
ms = Tuple{Module,MethodStore}[]
160160
methods0 = try
161-
methodinfo(f; types=types, world=world)
161+
methodinfo(f; types = types, world = world)
162162
catch err
163163
return ms
164164
end
@@ -230,10 +230,10 @@ else
230230
end
231231
end
232232

233-
function apply_to_everything(f, m=nothing, visited=Base.IdSet{Module}())
233+
function apply_to_everything(f, m = nothing, visited = Base.IdSet{Module}())
234234
if m isa Module
235235
push!(visited, m)
236-
for s in unsorted_names(m, all=true, imported=true)
236+
for s in unsorted_names(m, all = true, imported = true)
237237
(!isdefined(m, s) || s == nameof(m)) && continue
238238
x = getfield(m, s)
239239
f(x)
@@ -250,11 +250,11 @@ end
250250

251251

252252

253-
function oneverything(f, m=nothing, visited=Base.IdSet{Module}())
253+
function oneverything(f, m = nothing, visited = Base.IdSet{Module}())
254254
if m isa Module
255255
push!(visited, m)
256256
state = nothing
257-
for s in unsorted_names(m, all=true)
257+
for s in unsorted_names(m, all = true, imported = true)
258258
!isdefined(m, s) && continue
259259
x = getfield(m, s)
260260
state = f(m, s, x, state)
@@ -270,7 +270,7 @@ function oneverything(f, m=nothing, visited=Base.IdSet{Module}())
270270
end
271271

272272
const _global_symbol_cache_by_mod = IdDict{Module,Base.IdSet{Symbol}}()
273-
function build_namecache(m, s, @nospecialize(x), state::Union{Base.IdSet{Symbol},Nothing}=nothing)
273+
function build_namecache(m, s, @nospecialize(x), state::Union{Base.IdSet{Symbol},Nothing} = nothing)
274274
if state === nothing
275275
state = get(_global_symbol_cache_by_mod, m, nothing)
276276
if state === nothing
@@ -320,10 +320,10 @@ end
320320
usedby(outer, inner) = outer !== inner && isdefined(outer, nameof(inner)) && getproperty(outer, nameof(inner)) === inner && all(isdefined(outer, name) || !isdefined(inner, name) for name in unsorted_names(inner))
321321
istoplevelmodule(m) = parentmodule(m) === m || parentmodule(m) === Main
322322

323-
function getmoduletree(m::Module, amn, visited=Base.IdSet{Module}())
323+
function getmoduletree(m::Module, amn, visited = Base.IdSet{Module}())
324324
push!(visited, m)
325325
cache = ModuleStore(m)
326-
for s in unsorted_names(m, all=true, imported=true)
326+
for s in unsorted_names(m, all = true, imported = true)
327327
!isdefined(m, s) && continue
328328
x = getfield(m, s)
329329
if x isa Module
@@ -352,18 +352,37 @@ function getmoduletree(m::Module, amn, visited=Base.IdSet{Module}())
352352
cache
353353
end
354354

355-
function getenvtree(names=nothing)
355+
function getenvtree(names = nothing)
356356
amn = allmodulenames()
357357
EnvStore(nameof(m) => getmoduletree(m, amn) for m in Base.loaded_modules_array() if names === nothing || nameof(m) in names)
358358
end
359359

360-
function symbols(env::EnvStore, m::Union{Module,Nothing}=nothing, allnames::Base.IdSet{Symbol}=getallns(), visited=Base.IdSet{Module}())
360+
# faster and more correct split_module_names
361+
all_names(m) = all_names(m, x -> isdefined(m, x))
362+
function all_names(m, pred, symbols = Set(Symbol[]), seen = Set(Module[]))
363+
push!(seen, m)
364+
ns = unsorted_names(m; all = true, imported = false)
365+
for n in ns
366+
isdefined(m, n) || continue
367+
Base.isdeprecated(m, n) && continue
368+
val = getfield(m, n)
369+
if val isa Module && !(val in seen)
370+
all_names(val, pred, symbols, seen)
371+
end
372+
if pred(n)
373+
push!(symbols, n)
374+
end
375+
end
376+
symbols
377+
end
378+
379+
function symbols(env::EnvStore, m::Union{Module,Nothing} = nothing, allnames::Base.IdSet{Symbol} = getallns(), visited = Base.IdSet{Module}())
361380
if m isa Module
362381
cache = _lookup(VarRef(m), env, true)
363382
cache === nothing && return
364383
push!(visited, m)
365-
internalnames, othernames = split_module_names(m, allnames)
366-
for s in internalnames
384+
ns = all_names(m)
385+
for s in ns
367386
!isdefined(m, s) && continue
368387
x = getfield(m, s)
369388
if Base.unwrap_unionall(x) isa DataType # Unions aren't handled here.
@@ -409,23 +428,6 @@ function symbols(env::EnvStore, m::Union{Module,Nothing}=nothing, allnames::Base
409428
cache[s] = GenericStore(VarRef(VarRef(m), s), FakeTypeName(typeof(x)), _doc(x), s in getnames(m))
410429
end
411430
end
412-
for s in othernames
413-
x = getfield(m, s)
414-
if x isa Function
415-
if x isa Core.IntrinsicFunction
416-
cache[s] = VarRef(VarRef(Core.Intrinsics), nameof(x))
417-
else
418-
cache[s] = VarRef(VarRef(parentmodule(x)), nameof(x))
419-
end
420-
elseif x isa DataType
421-
cache[s] = VarRef(VarRef(parentmodule(x)), nameof(x))
422-
elseif x isa Module
423-
cache[s] = VarRef(x)
424-
else
425-
# We'd like to have these as VarRef's but we don't know where they live.
426-
cache[s] = GenericStore(VarRef(VarRef(m), s), FakeTypeName(typeof(x)), _doc(x), s in getnames(m))
427-
end
428-
end
429431
else
430432
for m in Base.loaded_modules_array()
431433
in(m, visited) || symbols(env, m, allnames, visited)
@@ -452,7 +454,7 @@ function load_core()
452454
cache[:Base][Symbol("@.")] = cache[:Base][Symbol("@__dot__")]
453455
cache[:Core][:Main] = GenericStore(VarRef(nothing, :Main), FakeTypeName(Module), _doc(Main), true)
454456
# Add built-ins
455-
builtins = Symbol[nameof(getfield(Core, n).instance) for n in unsorted_names(Core, all=true) if isdefined(Core, n) && getfield(Core, n) isa DataType && isdefined(getfield(Core, n), :instance) && getfield(Core, n).instance isa Core.Builtin]
457+
builtins = Symbol[nameof(getfield(Core, n).instance) for n in unsorted_names(Core, all = true) if isdefined(Core, n) && getfield(Core, n) isa DataType && isdefined(getfield(Core, n), :instance) && getfield(Core, n).instance isa Core.Builtin]
456458
cnames = unsorted_names(Core)
457459
for f in builtins
458460
if !haskey(cache[:Core], f)
@@ -529,7 +531,7 @@ function load_core()
529531
end
530532

531533

532-
function collect_extended_methods(depot::EnvStore, extendeds=Dict{VarRef,Vector{VarRef}}())
534+
function collect_extended_methods(depot::EnvStore, extendeds = Dict{VarRef,Vector{VarRef}}())
533535
for m in depot
534536
collect_extended_methods(m[2], extendeds, m[2].name)
535537
end
@@ -578,4 +580,4 @@ function split_module_names(m::Module, allns)
578580
end
579581

580582
get_all_modules() = let allms = Base.IdSet{Module}(); apply_to_everything(x -> if x isa Module push!(allms, x) end); allms end
581-
get_used_modules(M, allms=get_all_modules()) = [m for m in allms if usedby(M, m)]
583+
get_used_modules(M, allms = get_all_modules()) = [m for m in allms if usedby(M, m)]

0 commit comments

Comments
 (0)