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

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

local function isContainArgPos(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 isContainArgPos(arg, pos) then
            return i, arg
        end
    end
    return #args + 1, nil
end

-- 找出范围包含pos的call
local function findCall(vm, pos)
    local results = {}
    for _, call in ipairs(vm.results.calls) do
        if isContainPos(call.args, pos) then
            local n, arg = findArgCount(call.args, pos)
            if arg and arg.type == 'string' then
                return nil
            end
            local var = vm.results.sources[call.lastObj]
            if var then
                results[#results+1] = {
                    func = call.func,
                    var = var,
                    source = call.lastObj,
                    select = n,
                    args = call.args,
                }
            end
        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 = findCall(vm, pos)
    if not calls or #calls == 0 then
        return nil
    end

    local hovers = {}
    for _, call in ipairs(calls) do
        local hvr = hover(call.var, call.source, nil, call.select)
        if hvr and hvr.argLabel then
            hovers[#hovers+1] = hvr
        end
    end

    if #hovers == 0 then
        return nil
    end

    return hovers
end