summaryrefslogtreecommitdiff
path: root/server-beta/src/core/hover/init.lua
blob: 9de5f4dab82a5a75eb347c066ca1b5308e30ad04 (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
local files     = require 'files'
local guide     = require 'parser.guide'
local vm        = require 'vm'
local getLabel  = require 'core.hover.label'

local function getHoverAsFunction(source)
    local uri = guide.getRoot(source).uri
    local text = files.getText(uri)
    local values = vm.getValue(source)
    local labels = {}
    for _, value in ipairs(values) do
        if value.type == 'function' then
            labels[#labels+1] = getLabel(value.source)
        end
    end

    local label = table.concat(labels, '\n')
    return {
        label  = label,
        source = source,
    }
end

local function getHover(source)
    local isFunction = vm.hasType(source, 'function')
    if isFunction then
        return getHoverAsFunction(source)
    end
end

return function (uri, offset)
    local ast = files.getAst(uri)
    if not ast then
        return nil
    end
    local hover = guide.eachSourceContain(ast.ast, offset, function (source)
        if source.type == 'local'
        or source.type == 'setlocal'
        or source.type == 'getlocal'
        or source.type == 'setglobal'
        or source.type == 'getglobal'
        or source.type == 'field'
        or source.type == 'method' then
            return getHover(source)
        end
    end)
    return hover
end