summaryrefslogtreecommitdiff
path: root/script/provider/client.lua
blob: d208360c300fa03b59f17764bde7030620377aeb (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
local nonil  = require 'without-check-nil'
local util   = require 'utility'
local lang   = require 'language'
local proto  = require 'proto'
local define = require 'proto.define'

local m = {}

function m.client(newClient)
    if newClient then
        m._client = newClient
    else
        return m._client
    end
end

function m.isVSCode()
    if not m._client then
        return false
    end
    if m._isvscode == nil then
        local lname = m._client:lower()
        if lname:find 'vscode'
        or lname:find 'visual studio code' then
            m._isvscode = true
        else
            m._isvscode = false
        end
    end
    return m._isvscode
end

function m.getOption(name)
    nonil.enable()
    local option = m.info.initializationOptions[name]
    nonil.disable()
    return option
end

---show message to client
---@param type '"Error"'|'"Warning"'|'"Info"'|'"Log"'
---@param message any
function m.showMessage(type, message)
    proto.notify('window/showMessage', {
        type = define.MessageType[type] or 3,
        message = message,
    })
end

---set client config
---@param key string
---@param action '"set"'|'"add"'
---@param value any
---@param isGlobal boolean
function m.setConfig(key, action, value, isGlobal)
    if m.getOption 'changeConfiguration' then
        proto.notify('$/command', {
            command   = 'lua.config',
            data      = {
                key    = key,
                action = action,
                value  = value,
                global = isGlobal,
            }
        })
    else
        -- TODO translate
        local message = lang.script('你的客户端不支持从服务侧修改设置,请手动修改如下设置:')
        if action == 'add' then
            message = message .. lang.script('为 `{key}` 添加值 `{value}`', {
                key   = key,
                value = value,
            })
        else
            message = message .. lang.script('将 `{key}` 的值设置为 `{value}`', {
                key   = key,
                value = value,
            })
        end
        m.showMessage('Info', message)
    end
end

function m.init(t)
    log.debug('Client init', util.dump(t))
    m.info = t
    nonil.enable()
    m.client(t.clientInfo.name)
    nonil.disable()
    lang(LOCALE or t.locale)
end

return m