summaryrefslogtreecommitdiff
path: root/script-beta/provider/semantic-tokens.lua
blob: a8b1b23fadf08576f08a19b091be7d4fa8ba6200 (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
local proto          = require 'proto'
local tokenTypes     = require 'define.TokenTypes'
local tokenModifiers = require 'define.TokenModifiers'
local client         = require 'provider.client'

local isEnable = false

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 enable()
    if isEnable then
        return
    end
    if not client.info.capabilities.textDocument.semanticTokens then
        return
    end
    isEnable = true
    log.debug('Enable semantic tokens.')
    proto.awaitRequest('client/registerCapability', {
        registrations = {
            {
                id = 'semantic-tokens',
                method = 'textDocument/semanticTokens/full',
                registerOptions = {
                    legend = {
                        tokenTypes     = toArray(tokenTypes),
                        tokenModifiers = toArray(tokenModifiers),
                    },
                    range = true,
                    full  = false,
                },
            },
        }
    })
end

local function disable()
    if not isEnable then
        return
    end
    isEnable = false
    log.debug('Disable semantic tokens.')
    proto.awaitRequest('client/unregisterCapability', {
        unregisterations = {
            {
                id = 'semantic-tokens',
                method = 'textDocument/semanticTokens/full',
            },
        }
    })
end

return {
    enable = enable,
    disable = disable,
}