summaryrefslogtreecommitdiff
path: root/server/src/matcher/signature.lua
blob: 60316a5f502c0109f9be14a0186f421a063360b2 (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
local hover = require 'matcher.hover'

local function isContainPos(obj, pos)
    if obj.start <= pos and obj.finish + 1 >= pos then
        return true
    end
    return false
end

local function findArgCount(args, pos)
    for i, arg in ipairs(args) do
        if isContainPos(arg, pos) then
            return i
        end
    end
    return #args + 1
end

-- 找出范围包含pos的,且有dirty标记的call
local function findDirtyCall(vm, pos)
    local results = {}
    for _, call in ipairs(vm.results.calls) do
        if call.args.dirty and isContainPos(call.args, pos) then
            local n = findArgCount(call.args, pos)
            results[#results+1] = {
                func = call.func,
                var = vm.results.sources[call.lastObj],
                source = call.lastObj,
                select = n,
                args = call.args,
            }
        end
    end
    -- 可能处于 'func1(func2(' 的嵌套中,因此距离越远的函数层级越低
    table.sort(results, function (a, b)
        return a.args.start < b.args.start
    end)
    return results
end

return function (vm, pos)
    local calls = findDirtyCall(vm, pos)
    if #calls == 0 then
        return nil
    end

    local hovers = {}
    for i, call in ipairs(calls) do
        hovers[i] = hover(call.var, call.source, nil, call.select)
    end

    return hovers
end