summaryrefslogtreecommitdiff
path: root/server/src/core/hover/name.lua
blob: 52bcfef455d6336a3ef9be4a576910dcfb0f2f3e (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
return function (source)
    local value = source:bindValue()
    if not value then
        return ''
    end
    local func = value:getFunction()
    local declarat
    if func and func:getSource() then
        declarat = func:getSource().name
    else
        declarat = source
    end
    if not declarat then
        -- 如果声明者没有给名字,则找一个合适的名字
        local name = value:eachInfo(function (info, src)
            if info.type == 'local' or info.type == 'set' or info.type == 'return' then
                if src.type == 'name' and src.uri == value.uri then
                    return src[1]
                end
            end
        end)
        return name or ''
    end

    local key
    if declarat:get 'simple' then
        local simple = declarat:get 'simple'
        local chars = {}
        for i, obj in ipairs(simple) do
            if obj.type == 'name' then
                chars[i] = obj[1]
            elseif obj.type == 'index' then
                chars[i] = '[?]'
            elseif obj.type == 'call' then
                chars[i] = '(?)'
            elseif obj.type == ':' then
                chars[i] = ':'
            elseif obj.type == '.' then
                chars[i] = '.'
            else
                chars[i] = '*' .. obj.type
            end
            if obj == declarat then
                break
            end
        end
        key = table.concat(chars)
    elseif declarat.type == 'name' then
        key = declarat[1]
    elseif declarat.type == 'string' then
        key = ('%q'):format(declarat[1])
    elseif declarat.type == 'number' or declarat.type == 'boolean' then
        key = tostring(declarat[1])
    elseif declarat.type == 'simple' then
        local chars = {}
        for i, obj in ipairs(declarat) do
            if obj.type == 'name' then
                chars[i] = obj[1]
            elseif obj.type == 'index' then
                chars[i] = '[?]'
            elseif obj.type == 'call' then
                chars[i] = '(?)'
            elseif obj.type == ':' then
                chars[i] = ':'
            elseif obj.type == '.' then
                chars[i] = '.'
            else
                chars[i] = '*' .. obj.type
            end
        end
        -- 这里有个特殊处理
        -- function mt:func() 以 mt.func 的形式调用时
        -- hover 显示为 mt.func(self)
        if chars[#chars-1] == ':' then
            if not source:get 'object' then
                chars[#chars-1] = '.'
            end
        elseif chars[#chars-1] == '.' then
            if source:get 'object' then
                chars[#chars-1] = ':'
            end
        end
        key = table.concat(chars)
    else
        key = ''
    end
    return key
end