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

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

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

local function getHoverAsValue(source)
    local label = getLabel(source, source)
    return {
        label  = label,
        source = source,
    }
end

local function getHover(source)
    local isFunction = vm.hasType(source, 'function')
    if isFunction then
        return getHoverAsFunction(source)
    else
        return getHoverAsValue(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