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

local function mergeTypesInLibrary(types)
    if type(types) == 'table' then
        return table.concat(types, '|')
    else
        return types
    end
end

local function asLibrary(source, oop)
    if not source.args then
        return ''
    end
    local args = {}
    for i = 1, #source.args do
        local arg = source.args[i]
        local name = arg.name
        if name then
            args[i] = ('%s: %s'):format(name, mergeTypesInLibrary(arg.type))
        else
            args[i] = ('%s'):format(mergeTypesInLibrary(arg.type))
        end
    end
    local methodDef
    local parent = source.parent
    if parent and parent.type == 'setmethod' then
        methodDef = true
    end
    if not methodDef and oop then
        return table.concat(args, ', ', 2)
    else
        return table.concat(args, ', ')
    end
end

local function asFunction(source, oop)
    if not source.args then
        return ''
    end
    local args = {}
    for i = 1, #source.args do
        local arg = source.args[i]
        local name = arg.name or guide.getName(arg)
        if name then
            args[i] = ('%s: %s'):format(name, vm.getInferType(arg))
        else
            args[i] = ('%s'):format(vm.getInferType(arg))
        end
    end
    local methodDef
    local parent = source.parent
    if parent and parent.type == 'setmethod' then
        methodDef = true
    end
    if not methodDef and oop then
        return table.concat(args, ', ', 2)
    else
        return table.concat(args, ', ')
    end
end

return function (source, oop)
    if source.type == 'library' then
        return asLibrary(source.value, oop)
    elseif source.library then
        return asLibrary(source, oop)
    end
    if source.type == 'function' then
        return asFunction(source, oop)
    end
    return ''
end