summaryrefslogtreecommitdiff
path: root/server-beta/src/core/definition.lua
blob: fd01072ee11c71eb73e36010bff6ac0881f06828 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
local guide = require 'parser.guide'

local m = {}

function m.search(state, ast, source)
    if not source then
        return
    end
    if state.cache[source] then
        return
    end
    state.cache[source] = true
    local f = m['as' .. source.type]
    if not f then
        return
    end
    f(state, ast, source)
end

function m.asgetlocal(state, ast, source)
    local loc = ast.root[source.loc]
    m.search(state, ast, loc)
end

function m.assetlocal(state, ast, source)
    local loc = ast.root[source.loc]
    m.search(state, ast, loc)
    state.callback(source, ast.uri)
end

function m.aslocal(state, ast, source)
    state.callback(source, ast.uri)
    if source.ref then
        for _, ref in ipairs(source.ref) do
            m.search(state, ast, ast.root[ref])
        end
    end
end

function m.globals(state, ast, source)
    local name = source[1]
    guide.eachGloabl(ast.root, function (src, gname)
        if name ~= gname then
            return
        end
        if src.type == 'setglobal' or src.type == 'setfield' then
            state.callback(src, ast.uri)
        end
    end)
end

function m.assetglobal(state, ast, source)
    m.globals(state, ast, source)
end

function m.asgetglobal(state, ast, source)
    m.globals(state, ast, source)
end

return function (ast, text, offset)
    local results = {}
    local state = {
        cache = {},
        callback = function (target, uri)
            results[#results+1] = {
                uri    = uri or ast.uri,
                source = source,
                target = target,
            }
        end
    }
    guide.eachSource(ast.root, offset, function (source)
        m.search(state, ast, source)
    end)
    if #results == 0 then
        return nil
    end
    return results
end