summaryrefslogtreecommitdiff
path: root/server/src/matcher/implementation.lua
blob: 9ff3fdcae8f314ed939789b112e6ee754c9d463e (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
80
81
82
83
84
85
86
87
88
local function parseResultAcrossUri(positions, vm, result)
    -- 跨越文件时,遍历的是值的绑定信息
    for _, info in ipairs(result.value) do
        if info.type == 'set' and info.source.uri == result.value.uri then
            positions[1] = {
                info.source.start,
                info.source.finish,
                info.source.uri,
            }
        end
    end
    if #positions == 0 then
        for _, info in ipairs(result.value) do
            if info.type == 'return' and info.source.uri == result.value.uri then
                positions[1] = {
                    info.source.start,
                    info.source.finish,
                    info.source.uri,
                }
            end
        end
    end
    if #positions == 0 then
        positions[1] = {
            0, 0,
            result.value.uri,
        }
    end
end

local function parseResult(vm, result)
    local positions = {}
    local tp = result.type
    if     tp == 'local' then
        if result.value.uri ~= vm.uri then
            parseResultAcrossUri(positions, vm, result)
        else
            for _, info in ipairs(result) do
                if info.type == 'set' then
                    positions[#positions+1] = {
                        info.source.start,
                        info.source.finish,
                        info.source.uri,
                    }
                end
            end
        end
    elseif tp == 'field' then
        if result.value.uri ~= vm.uri then
            parseResultAcrossUri(positions, vm, result)
        else
            for _, info in ipairs(result) do
                if info.type == 'set' then
                    positions[#positions+1] = {
                        info.source.start,
                        info.source.finish,
                        info.source.uri,
                    }
                end
            end
        end
    elseif tp == 'label' then
        for _, info in ipairs(result) do
            if info.type == 'set' then
                positions[#positions+1] = {
                    info.source.start,
                    info.source.finish,
                }
            end
        end
    elseif tp == 'string' then
        -- require 'XXX' 专用
        positions[#positions+1] = {
            0,
            0,
            result.uri,
        }
    end
    return positions
end

return function (vm, result)
    if not result then
        return nil
    end
    local positions = parseResult(vm, result)
    return positions
end