summaryrefslogtreecommitdiff
path: root/server-beta/src/searcher/eachGlobal.lua
blob: 8f556976d5bfae83f73680038b7e0ba9f5e65864 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
local guide = require 'parser.guide'
local searcher = require 'searcher.searcher'

local function eachGlobal(source, callback)
    local root = guide.getRoot(source)
    local env  = root.locals[1]
    local result = {}
    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 result[name] then
            result[name] = {
                key  = name,
                mode = {},
            }
        end
        result[name][#result[name]+1] = info
        result[name].mode[info.mode] = true
    end)
    for _, info in pairs(result) do
        callback(info)
    end
end

function searcher.eachGlobal(source, callback)
    source = guide.getRoot(source)
    local cache = searcher.cache.eachGlobal[source]
    if cache then
        for i = 1, #cache do
            local res = callback(cache[i])
            if res ~= nil then
                return res
            end
        end
        return
    end
    local unlock = searcher.lock('eachGlobal', source)
    if not unlock then
        return
    end
    cache = {}
    searcher.cache.eachGlobal[source] = cache
    local mark = {}
    eachGlobal(source, function (info)
        cache[#cache+1] = info
    end)
    unlock()
    for i = 1, #cache do
        local res = callback(cache[i])
        if res ~= nil then
            return res
        end
    end
end