summaryrefslogtreecommitdiff
path: root/script/core/hint.lua
blob: 62d2f7bfcda2e49a3bc6d419325cba68af7bbc50 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
local files    = require 'files'
local infer    = require 'core.infer'
local vm       = require 'vm'
local config   = require 'config'
local guide    = require 'parser.guide'
local await    = require 'await'
local define   = require 'proto.define'

local function typeHint(uri, results, start, finish)
    local ast = files.getState(uri)
    if not ast then
        return
    end
    local mark = {}
    guide.eachSourceBetween(ast.ast, start, finish, function (source)
        if  source.type ~= 'local'
        and source.type ~= 'setglobal'
        and source.type ~= 'tablefield'
        and source.type ~= 'tableindex'
        and source.type ~= 'setfield'
        and source.type ~= 'setindex' then
            return
        end
        if source.dummy then
            return
        end
        if source[1] == '_' then
            return
        end
        if source.value and guide.isLiteral(source.value) then
            return
        end
        if source.parent.type == 'funcargs' then
            if not config.get 'Lua.hint.paramType' then
                return
            end
        else
            if not config.get 'Lua.hint.setType' then
                return
            end
        end
        await.delay()
        local view = infer.searchAndViewInfers(source)
        if view == 'any'
        or view == 'nil' then
            return
        end
        local src = source
        if source.type == 'tablefield' then
            src = source.field
        elseif source.type == 'tableindex' then
            src = source.index
        end
        if not src then
            return
        end
        if mark[src] then
            return
        end
        mark[src] = true
        results[#results+1] = {
            text   = ':' .. view,
            offset = src.finish,
            kind   = define.InlayHintKind.Type,
            where  = 'right',
        }
    end)
end

local function getArgNames(func)
    if not func.args or #func.args == 0 then
        return nil
    end
    local names = {}
    for _, arg in ipairs(func.args) do
        if arg.type == '...' then
            break
        end
        names[#names+1] = arg[1] or ''
    end
    if #names == 0 then
        return nil
    end
    return names
end

local function hasLiteralArgInCall(call)
    if not call.args then
        return false
    end
    for _, arg in ipairs(call.args) do
        if guide.isLiteral(arg) then
            return true
        end
    end
    return false
end

local function paramName(uri, results, start, finish)
    local paramConfig = config.get 'Lua.hint.paramName'
    if not paramConfig or paramConfig == 'None' then
        return
    end
    local ast = files.getState(uri)
    if not ast then
        return
    end
    local mark = {}
    guide.eachSourceBetween(ast.ast, start, finish, function (source)
        if source.type ~= 'call' then
            return
        end
        if paramConfig == 'Literal' and not hasLiteralArgInCall(source) then
            return
        end
        if not source.args then
            return
        end
        await.delay()
        local defs = vm.getDefs(source.node)
        if not defs then
            return
        end
        local args
        for _, def in ipairs(defs) do
            if def.value then
                def = def.value
            end
            if def.type == 'function' then
                args = getArgNames(def)
                if args then
                    break
                end
            end
        end
        if not args then
            return
        end
        local firstIndex = 1
        if source.node and source.node.type == 'getmethod' then
            firstIndex = 2
        end
        for i = firstIndex, #source.args do
            local arg = source.args[i]
            if  not mark[arg]
            and (paramConfig ~= 'Literal' or guide.isLiteral(arg)) then
                mark[arg] = true
                if args[i] and args[i] ~= '' then
                    results[#results+1] = {
                        text   = args[i] .. ':',
                        offset = arg.start,
                        kind   = define.InlayHintKind.Parameter,
                        where  = 'left',
                    }
                end
            end
        end
    end)
end

return function (uri, start, finish)
    local results = {}
    typeHint(uri, results, start, finish)
    paramName(uri, results, start, finish)
    return results
end