summaryrefslogtreecommitdiff
path: root/server-beta/src/core/hover/name.lua
blob: a22a8b5ae0357bf5cfef72c9ac9b299a5408ad24 (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
local guide = require 'parser.guide'
local vm    = require 'vm'

local function asLocal(source)
    return guide.getName(source)
end

local function asMethod(source)
    local class = vm.eachField(source.node, function (info)
        if info.key == 's|type' or info.key == 's|__name' or info.key == 's|name' then
            if info.value and info.value.type == 'string' then
                return info.value[1]
            end
        end
    end)
    local node = class or guide.getName(source.node) or '?'
    local method = guide.getName(source)
    return ('%s:%s'):format(node, method)
end

local function asField(source)
    local class = vm.eachField(source.node, function (info)
        if info.key == 's|type' or info.key == 's|__name' or info.key == 's|name' then
            if info.value and info.value.type == 'string' then
                return info.value[1]
            end
        end
    end)
    local node = class or guide.getName(source.node) or '?'
    local method = guide.getName(source)
    return ('%s.%s'):format(node, method)
end

local function asGlobal(source)
    return guide.getName(source)
end

local function buildName(source)
    if source.type == 'local'
    or source.type == 'getlocal'
    or source.type == 'setlocal' then
        return asLocal(source) or ''
    end
    if source.type == 'setglobal'
    or source.type == 'getglobal' then
        return asGlobal(source) or ''
    end
    if source.type == 'setmethod'
    or source.type == 'getmethod' then
        return asMethod(source) or ''
    end
    if source.type == 'setfield'
    or source.tyoe == 'getfield'
    or source.type == 'tablefield' then
        return asField(source) or ''
    end
    local parent = source.parent
    if parent then
        return buildName(parent)
    end
    return ''
end

return buildName