summaryrefslogtreecommitdiff
path: root/script/core/hint.lua
blob: 90182c39955b64e2c4ba703233cb20f3d86705c8 (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
local files = require 'files'
local guide = require 'parser.guide'
local vm    = require 'vm'

local function typeHint(uri, start, finish)
    local ast = files.getAst(uri)
    if not ast then
        return nil
    end
    local edits = {}
    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[1] == '_' then
            return
        end
        -- 排除掉 xx = function 与 xx = {}
        if source.value and (source.value.type == 'function' or source.value.type == 'table') then
            return
        end
        local infer = vm.getInferType(source, 0)
        local src = source
        if source.type == 'tablefield' then
            src = source.field
        elseif source.type == 'tableindex' then
            src = source.index
        end
        edits[#edits+1] = {
            newText = (':%s'):format(infer),
            start   = src.finish,
            finish  = src.finish,
        }
    end)
    return edits
end

return {
    typeHint = typeHint,
}