summaryrefslogtreecommitdiff
path: root/script/provider/semantic-tokens.lua
blob: 4f2d81772bd741586336502417bae7222972cecb (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
local proto          = require 'proto'
local define         = require 'proto.define'
local client         = require 'client'
local json           = require "json"
local config         = require 'config'
local lang           = require 'language'
local nonil          = require 'without-check-nil'

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 dontShowAgain = false
local function enable()
    if isEnable then
        return
    end
    nonil.enable()
    if not client.info.capabilities.textDocument.semanticTokens.dynamicRegistration then
        return
    end
    nonil.disable()
    isEnable = true
    log.debug('Enable semantic tokens.')
    proto.request('client/registerCapability', {
        registrations = {
            {
                id = 'semantic-tokens',
                method = 'textDocument/semanticTokens',
                registerOptions = {
                    legend = {
                        tokenTypes     = toArray(define.TokenTypes),
                        tokenModifiers = toArray(define.TokenModifiers),
                    },
                    range = true,
                    full  = false,
                },
            },
        }
    })
    if config.get 'editor.semanticHighlighting.enabled' == 'configuredByTheme' and not dontShowAgain then
        proto.request('window/showMessageRequest', {
            type    = define.MessageType.Info,
            message = lang.script.WINDOW_CHECK_SEMANTIC,
            actions = {
                {
                    title = lang.script.WINDOW_APPLY_SETTING,
                },
                {
                    title = lang.script.WINDOW_DONT_SHOW_AGAIN,
                },
            }
        }, function (item)
            if not item then
                return
            end
            if item.title == lang.script.WINDOW_APPLY_SETTING then
                client.setConfig {
                    {
                        key      = 'editor.semanticHighlighting.enabled',
                        action   = 'set',
                        value    = true,
                        global   = true,
                    }
                }
            end
            if item.title == lang.script.WINDOW_DONT_SHOW_AGAIN then
                dontShowAgain = true
            end
        end)
    end
end

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

local function refresh()
    log.debug('Refresh semantic tokens.')
    proto.notify('workspace/semanticTokens/refresh', json.null)
end

config.watch(function (key, value)
    if key == 'Lua.color.mode' then
        if value == 'Semantic' then
            enable()
        else
            disable()
        end
    end
    if key:find '^Lua.runtime'
    or key:find '^Lua.workspace'
    or key:find '^files' then
        refresh()
    end
end)

return {
    enable  = enable,
    disable = disable,
    refresh = refresh,
}