summaryrefslogtreecommitdiff
path: root/script/core/semantic-tokens.lua
blob: 291819f2cfad49f35cd22264eb002586ddc81581 (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
local files          = require 'files'
local searcher       = require 'core.searcher'
local await          = require 'await'
local define         = require 'proto.define'
local vm             = require 'vm'
local util           = require 'utility'
local guide          = require 'parser.guide'
local converter      = require 'proto.converter'
local infer          = require 'core.infer'
local config         = require 'config'

local isEnhanced = config.get(nil, 'Lua.color.mode') == 'SemanticEnhanced'

local Care = {}
Care['getglobal'] = function (source, results)
    local isLib = vm.isGlobalLibraryName(source[1])
    local isFunc = false
    local value = source.value

    if value and value.type == 'function' then
        isFunc = true
    elseif source.parent.type == 'call' then
        isFunc = true
    elseif isEnhanced then
        isFunc = infer.hasType(source, 'function')
    end

    local type = isFunc and define.TokenTypes['function'] or define.TokenTypes.variable
    local modifier = isLib and define.TokenModifiers.defaultLibrary or define.TokenModifiers.static

    results[#results+1] = {
        start      = source.start,
        finish     = source.finish,
        type       = type,
        modifieres = modifier,
    }
end
Care['setglobal'] = Care['getglobal']
Care['getmethod'] = function (source, results)
    local method = source.method
    if method and method.type == 'method' then
        results[#results+1] = {
            start      = method.start,
            finish     = method.finish,
            type       = define.TokenTypes.method,
            modifieres = source.type == 'setmethod' and define.TokenModifiers.declaration or nil,
        }
    end
end
Care['setmethod'] = Care['getmethod']
Care['field'] = function (source, results)
    local modifiers = 0
    if source.parent and source.parent.type == 'tablefield' then
        modifiers = define.TokenModifiers.declaration
    end
    if source.parent then
        local value = source.parent.value
        if value and value.type == 'function' then
            results[#results+1] = {
                start      = source.start,
                finish     = source.finish,
                type       = define.TokenTypes.method,
                modifieres = modifiers,
            }
            return
        end
    end
    if isEnhanced and infer.hasType(source, 'function') then
        results[#results+1] = {
            start      = source.start,
            finish     = source.finish,
            type       = define.TokenTypes.method,
            modifieres = modifiers,
        }
        return
    end
    if source.parent.parent.type == 'call' then
        results[#results+1] = {
            start      = source.start,
            finish     = source.finish,
            type       = define.TokenTypes.method,
            modifieres = modifiers,
        }
        return
    end
    results[#results+1] = {
        start      = source.start,
        finish     = source.finish,
        type       = define.TokenTypes.property,
        modifieres = modifiers,
    }
end
Care['getlocal'] = function (source, results)
    local loc = source.node
    -- 1. 值为函数的局部变量 | Local variable whose value is a function
    if loc.refs then
        for _, ref in ipairs(loc.refs) do
            if ref.value and ref.value.type == 'function' then
                results[#results+1] = {
                    start      = source.start,
                    finish     = source.finish,
                    type       = define.TokenTypes['function'],
                }
                return
            end
        end
    end
    -- 2. 对象 | Object
    if  source.parent.type == 'getmethod'
    or  source.parent.type == 'setmethod'
    and source.parent.node == source then
        return
    end
    -- 3. 特殊变量 | Special variable
    if source[1] == '_ENV'
    or source[1] == 'self' then
        return
    end
    -- 4. 函数的参数 | Function parameters
    if loc.parent and loc.parent.type == 'funcargs' then
        results[#results+1] = {
            start      = source.start,
            finish     = source.finish,
            type       = define.TokenTypes.parameter,
            modifieres = define.TokenModifiers.declaration,
        }
        return
    end
    -- 5. References to other functions
    if isEnhanced and infer.hasType(loc, 'function') then
        results[#results+1] = {
            start      = source.start,
            finish     = source.finish,
            type       = define.TokenTypes['function'],
            modifieres = source.type == 'setlocal' and define.TokenModifiers.declaration or nil,
        }
        return
    end
    -- 6. Class declaration
    if isEnhanced then
        -- search all defs
        for _, def in ipairs(vm.getDefs(source)) do
            if def.bindDocs then
                for _, doc in ipairs(def.bindDocs) do
                    if doc.type == "doc.class" and doc.bindSources then
                        for _, src in ipairs(doc.bindSources) do
                            if src == def then
                                results[#results+1] = {
                                    start      = source.start,
                                    finish     = source.finish,
                                    type       = define.TokenTypes.class,
                                }
                                return
                            end
                        end
                    end
                end
            end
        end
    else
        -- only search this local
        if loc.bindDocs then
            for i, doc in ipairs(loc.bindDocs) do
                if doc.type == "doc.class" and doc.bindSources then
                    for _, src in ipairs(doc.bindSources) do
                        if src == loc then
                            results[#results+1] = {
                                start      = source.start,
                                finish     = source.finish,
                                type       = define.TokenTypes.class,
                            }
                            return
                        end
                    end
                end
            end
        end
    end
    -- 6. const 变量 | Const variable
    if loc.attrs then
        for _, attr in ipairs(loc.attrs) do
            local name = attr[1]
            if name == 'const' then
                results[#results+1] = {
                    start      = source.start,
                    finish     = source.finish,
                    type       = define.TokenTypes.variable,
                    modifieres = define.TokenModifiers.static,
                }
                return
            elseif name == 'close' then
                results[#results+1] = {
                    start      = source.start,
                    finish     = source.finish,
                    type       = define.TokenTypes.variable,
                    modifieres = define.TokenModifiers.abstract,
                }
                return
            end
        end
    end
    -- 7. 函数调用 | Function call
    if  source.parent.type == 'call'
    and source.parent.node == source then
        return
    end
    local isLocal = loc.parent ~= guide.getRoot(loc)
    -- 8. 其他 | Other
    results[#results+1] = {
        start      = source.start,
        finish     = source.finish,
        type       = define.TokenTypes.variable,
        modifieres = isLocal and define.TokenModifiers['local'] or nil,
    }
end
Care['setlocal'] = Care['getlocal']
Care['local'] = function (source, results) -- Local declaration, i.e. "local x", "local y = z", or "local function() end"
    if source[1] == '_ENV'
    or source[1] == 'self' then
        return
    end
    if source.parent and source.parent.type == 'funcargs' then
        results[#results+1] = {
            start      = source.start,
            finish     = source.finish,
            type       = define.TokenTypes.parameter,
            modifieres = define.TokenModifiers.declaration,
        }
        return
    end
    if source.value then
        local isFunction = false

        if isEnhanced then
            isFunction = source.value.type == 'function' or infer.hasType(source.value, 'function')
        else
            isFunction = source.value.type == 'function'
        end

        if isFunction then
            -- Function declaration, either a new one or an alias for another one
            results[#results+1] = {
                start      = source.start,
                finish     = source.finish,
                type       = define.TokenTypes['function'],
                modifieres = define.TokenModifiers.declaration,
            }
            return
        end
    end
    if source.value and source.value.type == 'table' and source.bindDocs then
        for _, doc in ipairs(source.bindDocs) do
            if doc.type == "doc.class" then
                -- Class declaration (explicit)
                results[#results+1] = {
                    start      = source.start,
                    finish     = source.finish,
                    type       = define.TokenTypes.class,
                    modifieres = define.TokenModifiers.declaration,
                }
                return
            end
        end
    end
    if source.attrs then
        for _, attr in ipairs(source.attrs) do
            local name = attr[1]
            if name == 'const' then
                results[#results+1] = {
                    start      = source.start,
                    finish     = source.finish,
                    type       = define.TokenTypes.variable,
                    modifieres = define.TokenModifiers.declaration | define.TokenModifiers.static,
                }
                return
            elseif name == 'close' then
                results[#results+1] = {
                    start      = source.start,
                    finish     = source.finish,
                    type       = define.TokenTypes.variable,
                    modifieres = define.TokenModifiers.declaration | define.TokenModifiers.abstract,
                }
                return
            end
        end
    end

    local isLocal = source.parent ~= guide.getRoot(source)
    local modifiers = define.TokenModifiers.declaration

    if isLocal then
        modifiers = modifiers | define.TokenModifiers.definition
    end

    results[#results+1] = {
        start      = source.start,
        finish     = source.finish,
        type       = define.TokenTypes.variable,
        modifieres = modifiers,
    }
end
Care['doc.return.name'] = function (source, results)
    results[#results+1] = {
        start  = source.start,
        finish = source.finish,
        type   = define.TokenTypes.parameter,
    }
end
Care['doc.tailcomment'] = function (source, results)
    results[#results+1] = {
        start  = source.start,
        finish = source.finish,
        type   = define.TokenTypes.comment,
    }
end
Care['doc.type.name'] = function (source, results)
    if source.typeGeneric then
        results[#results+1] = {
            start  = source.start,
            finish = source.finish,
            type   = define.TokenTypes.macro,
        }
    end
end

Care['nonstandardSymbol.comment'] = function (source, results)
    results[#results+1] = {
        start  = source.start,
        finish = source.finish,
        type   = define.TokenTypes.comment,
    }
end
Care['nonstandardSymbol.continue'] = function (source, results)
    results[#results+1] = {
        start  = source.start,
        finish = source.finish,
        type   = define.TokenTypes.keyword,
    }
end

local function buildTokens(uri, results)
    local tokens = {}
    local lastLine = 0
    local lastStartChar = 0
    for i, source in ipairs(results) do
        local startPos  = converter.packPosition(uri, source.start)
        local finishPos = converter.packPosition(uri, source.finish)
        local line      = startPos.line
        local startChar = startPos.character
        local deltaLine = line - lastLine
        local deltaStartChar
        if deltaLine == 0 then
            deltaStartChar = startChar - lastStartChar
        else
            deltaStartChar = startChar
        end
        lastLine = line
        lastStartChar = startChar
        -- see https://microsoft.github.io/language-server-protocol/specifications/specification-3-16/#textDocument_semanticTokens
        local len = i * 5 - 5
        tokens[len + 1] = deltaLine
        tokens[len + 2] = deltaStartChar
        tokens[len + 3] = finishPos.character - startPos.character -- length
        tokens[len + 4] = source.type
        tokens[len + 5] = source.modifieres or 0
    end
    return tokens
end

config.watch(function (key, value)
    if key == 'Lua.color.mode' then
        isEnhanced = value == 'SemanticEnhanced'
    end
end)

---@async
return function (uri, start, finish)
    local state = files.getState(uri)
    local text  = files.getText(uri)
    if not state then
        return nil
    end

    local results = {}
    guide.eachSourceBetween(state.ast, start, finish, function (source) ---@async
        local method = Care[source.type]
        if not method then
            return
        end
        method(source, results)
        await.delay()
    end)

    for _, comm in ipairs(state.comms) do
        if comm.type == 'comment.cshort' then
            results[#results+1] = {
                start  = comm.start,
                finish = comm.finish,
                type   = define.TokenTypes.comment,
            }
        end
    end

    table.sort(results, function (a, b)
        return a.start < b.start
    end)

    local tokens = buildTokens(uri, results)

    return tokens
end