summaryrefslogtreecommitdiff
path: root/script/method/textDocument/semanticTokens.lua
blob: b201c403a1b3cb0850913bbe71ce58e7f2299070 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
local TokenTypes     = require 'constant.TokenTypes'
local TokenModifiers = require 'constant.TokenModifiers'
local findLib        = require 'core.find_lib'

local timerCache = {}
local constLib = {
    ['_G']                = true,
    ['_VERSION']          = true,
    ['math.pi']           = true,
    ['math.huge']         = true,
    ['math.maxinteger']   = true,
    ['math.mininteger']   = true,
    ['utf8.charpattern']  = true,
    ['io.stdin']          = true,
    ['io.stdout']         = true,
    ['io.stderr']         = true,
    ['package.config']    = true,
    ['package.cpath']     = true,
    ['package.loaded']    = true,
    ['package.loaders']   = true,
    ['package.path']      = true,
    ['package.preload']   = true,
    ['package.searchers'] = true
}

local Care = {
    ['name'] = function (source, sources)
        if source.start == source.finish then
            return
        end
        if source:get 'global' then
            if findLib(source) then
                if source[1] == '_G' then
                    return
                end
                sources[#sources+1] = {
                    start      = source.start,
                    finish     = source.finish,
                    type       = TokenTypes.namespace,
                    modifieres = TokenModifiers.static,
                }
                return
            end
            sources[#sources+1] = {
                start      = source.start,
                finish     = source.finish,
                type       = TokenTypes.namespace,
                modifieres = TokenModifiers.deprecated,
            }
        elseif source:get 'table index' then
            sources[#sources+1] = {
                start      = source.start,
                finish     = source.finish,
                type       = TokenTypes.property,
                modifieres = TokenModifiers.declaration,
            }
        elseif source:bindLocal() then
            if source:get 'arg' then
                sources[#sources+1] = {
                    start      = source.start,
                    finish     = source.finish,
                    type       = TokenTypes.parameter,
                    modifieres = TokenModifiers.declaration,
                }
            end
            if source[1] == '_ENV'
            or source[1] == 'self' then
                return
            end
            sources[#sources+1] = {
                start      = source.start,
                finish     = source.finish,
                type       = TokenTypes.variable,
            }
        end
    end,
}

local function buildTokens(sources, lines)
    local tokens = {}
    local lastLine = 0
    local lastStartChar = 0
    for i, source in ipairs(sources) do
        local row, col = lines:rowcol(source.start)
        local line = row - 1
        local startChar = col - 1
        local deltaLine = line - lastLine
        local deltaStartChar
        if deltaLine == 0 then
            deltaStartChar = startChar - lastStartChar
        else
            deltaStartChar = startChar
        end
        lastLine = line
        lastStartChar = startChar
        local len = i * 5 - 5
        tokens[len + 1] = deltaLine
        tokens[len + 2] = deltaStartChar
        tokens[len + 3] = source.finish - source.start + 1 -- length
        tokens[len + 4] = source.type
        tokens[len + 5] = source.modifieres or 0
    end
    return tokens
end

local function resolveTokens(vm, lines)
    local sources = {}
    for _, source in ipairs(vm.sources) do
        if Care[source.type] then
            Care[source.type](source, sources)
        end
    end

    -- 先进行排序
    table.sort(sources, function (a, b)
        return a.start < b.start
    end)

    log.debug(table.dump(sources))

    local tokens = buildTokens(sources, lines)

    return tokens
end

local function toArray(map)
    local array = {}
    for k in pairs(map) do
        array[#array+1] = k
    end
    table.sort(array, function (a, b)
        return map[a] < map[b]
    end)
    return array
end

local function testTokens(vm, lines)
    local text = vm.text
    local sources = {}
    local init = 1
    while true do
        local start, finish = text:find('[%w_%.]+', init)
        if not start then
            break
        end
        init = finish + 1
        local token = text:sub(start, finish)
        local type = token:match '[%w_]+'
        local mod  = token:match '%.([%w_]+)'
        sources[#sources+1] = {
            start      = start,
            finish     = finish,
            type       = TokenTypes[type],
            modifieres = TokenModifiers[mod] or 0,
        }
    end
    local tokens = buildTokens(sources, lines)
    log.debug(table.dump(sources))
    log.debug(table.dump(tokens))
    return tokens
end

return function (lsp, params)
    local uri = params.textDocument.uri

    if timerCache[uri] then
        timerCache[uri]:remove()
        timerCache[uri] = nil
    end

    return function (response)
        local clock = os.clock()
        timerCache[uri] = ac.loop(0.1, function (t)
            local vm, lines = lsp:getVM(uri)
            if not vm then
                if os.clock() - clock > 10 then
                    t:remove()
                    timerCache[uri] = nil
                    response(nil)
                end
                return
            end

            t:remove()
            timerCache[uri] = nil

            local tokens = resolveTokens(vm, lines)
            --local tokens = testTokens(vm, lines)
            response {
                data = tokens,
            }
        end)
    end
end