summaryrefslogtreecommitdiff
path: root/server-beta/src/searcher/getLibrary.lua
blob: a2620295c3ed3769f2526db958b1100566df1ead (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
60
local searcher = require 'searcher.searcher'
local library  = require 'library'
local guide    = require 'parser.guide'

local function checkStdLibrary(source)
    local globalName = searcher.getGlobal(source)
    if not globalName then
        return nil
    end
    local name = globalName:match '^s|(.+)$'
    if library.global[name] then
        return library.global[name]
    end
end

local function getLibrary(source)
    local lib = checkStdLibrary(source)
    if lib then
        return lib
    end
    return searcher.eachRef(source, function (info)
        local src = info.source
        if  src.type ~= 'getfield'
        and src.type ~= 'getmethod'
        and src.type ~= 'getindex' then
            return
        end
        local node = src.node
        local nodeGlobalName = searcher.getGlobal(node)
        if not nodeGlobalName then
            return
        end
        local nodeName = nodeGlobalName:match '^s|(.+)$'
        local nodeLib = library.global[nodeName]
        if not nodeLib then
            return
        end
        if not nodeLib.child then
            return
        end
        local key = guide.getKeyString(src)
        local defLib = nodeLib.child[key]
        return defLib
    end)
end

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