summaryrefslogtreecommitdiff
path: root/server-beta/src/searcher/getGlobals.lua
blob: 13e69e16590aa286ad4dc24d5b3855c54757c806 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
local guide = require 'parser.guide'
local searcher = require 'searcher.searcher'

local function getGlobals(root)
    local env  = guide.getENV(root)
    local cache = {}
    local mark = {}
    searcher.eachField(env, function (info)
        local src = info.source
        if mark[src] then
            return
        end
        mark[src] = true
        local name = info.key
        if not name then
            return
        end
        if not cache[name] then
            cache[name] = {
                key  = name,
                mode = {},
            }
        end
        cache[name][#cache[name]+1] = info
        cache[name].mode[info.mode] = true
        searcher.cache.getGlobal[src] = name
    end)
    return cache
end

function searcher.getGlobals(source)
    source = guide.getRoot(source)
    local cache = searcher.cache.getGlobals[source]
    if cache ~= nil then
        return cache
    end
    local unlock = searcher.lock('getGlobals', source)
    if not unlock then
        return nil
    end
    cache = getGlobals(source) or false
    searcher.cache.getGlobals[source] = cache
    unlock()
    return cache
end