summaryrefslogtreecommitdiff
path: root/server/src/core/implementation.lua
blob: 1d36bd4dd73f45887aa5744678a0902c0aaf09d7 (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 function parseLocal(vm, loc, lsp)
    local positions = {}
    positions[#positions+1] = {
        loc.source.start,
        loc.source.finish,
    }
    if #positions == 0 then
        return nil
    end
    return positions
end

local function parseValue(vm, value, lsp)
    local positions = {}
    value:eachInfo(function (info)
        if info.type == 'set' then
            positions[#positions+1] = {
                info.source.start,
                info.source.finish,
            }
        end
    end)
    if #positions == 0 then
        return nil
    end
    return positions
end

local function parseValueSimily(vm, source, lsp)
    local key = source[1]
    if not key then
        return nil
    end
    local positions = {}
    for _, other in ipairs(vm.sources) do
        if other == source then
            break
        end
        if other[1] == key and not other:bindLocal() and other:bindValue() and other:action() == 'set' then
            positions[#positions+1] = {
                other.start,
                other.finish,
            }
        end
    end
    if #positions == 0 then
        return nil
    end
    return positions
end

local function parseLabel(vm, label, lsp)
    local positions = {}
    label:eachInfo(function (info)
        if info.type == 'set' then
            positions[#positions+1] = {
                info.source.start,
                info.source.finish,
            }
        end
    end)
    if #positions == 0 then
        return nil
    end
    return positions
end

return function (vm, source, lsp)
    if not source then
        return nil
    end
    if source:bindValue() then
        return parseValue(vm, source:bindValue(), lsp)
            or parseValueSimily(vm, source, lsp)
    end
    if source:bindLabel() then
        return parseLabel(vm, source:bindLabel(), lsp)
    end
end