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
|
local nonil = require 'without-check-nil'
local util = require 'utility'
local lang = require 'language'
local proto = require 'proto'
local define = require 'proto.define'
local config = require 'config'
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
function m.getAbility(name)
local current = m.info.capabilities
while true do
local parent, nextPos = name:match '^([^%.]+)()'
if not parent then
break
end
current = current[parent]
if not current then
return nil
end
if nextPos > #name then
break
else
name = name:sub(nextPos + 1)
end
end
return current
end
local function packMessage(...)
local strs = table.pack(...)
for i = 1, strs.n do
strs[i] = tostring(strs[i])
end
return table.concat(strs, '\t')
end
---@alias message.type '"Error"'|'"Warning"'|'"Info"'|'"Log"'
---show message to client
---@param type message.type
function m.showMessage(type, ...)
local message = packMessage(...)
proto.notify('window/showMessage', {
type = define.MessageType[type] or 3,
message = message,
})
proto.notify('window/logMessage', {
type = define.MessageType[type] or 3,
message = message,
})
end
---@param type message.type
---@param message string
---@param titles string[]
---@return string action
function m.awaitRequestMessage(type, message, titles)
proto.notify('window/logMessage', {
type = define.MessageType[type] or 3,
message = message,
})
local actions = {}
for i, title in ipairs(titles) do
actions[i] = {
title = title,
}
end
local item = proto.awaitRequest('window/showMessageRequest', {
type = type,
message = message,
actions = actions,
})
if not item then
return nil
end
return item.title
end
---@param type message.type
function m.logMessage(type, ...)
local message = packMessage(...)
proto.notify('window/logMessage', {
type = define.MessageType[type] or 4,
message = message,
})
end
---@class config.change
---@field key string
---@field prop? string
---@field value any
---@field action '"add"'|'"set"'|'"prop"'
---@field isGlobal? boolean
---@field uri? uri
---@param changes config.change[]
---@param onlyMemory boolean
function m.setConfig(changes, onlyMemory)
local finalChanges = {}
for _, change in ipairs(changes) do
if change.action == 'add' then
local suc = config.add(change.key, change.value)
if suc then
finalChanges[#finalChanges+1] = change
end
elseif change.action == 'set' then
local suc = config.set(change.key, change.value)
if suc then
finalChanges[#finalChanges+1] = change
end
elseif change.action == 'prop' then
local suc = config.prop(change.key, change.prop, change.value)
if suc then
finalChanges[#finalChanges+1] = change
end
end
change.uri = m.info.rootUri
end
if onlyMemory then
return
end
if #finalChanges == 0 then
return
end
if m.getOption 'changeConfiguration' then
proto.notify('$/command', {
command = 'lua.config',
data = finalChanges,
})
else
local messages = {}
messages[1] = lang.script('WINDOW_CLIENT_NOT_SUPPORT_CONFIG')
for _, change in ipairs(finalChanges) do
if change.action == 'add' then
messages[#messages+1] = lang.script('WINDOW_MANUAL_CONFIG_ADD', change)
elseif change.action == 'set' then
messages[#messages+1] = lang.script('WINDOW_MANUAL_CONFIG_SET', change)
elseif change.action == 'prop' then
messages[#messages+1] = lang.script('WINDOW_MANUAL_CONFIG_PROP', change)
end
end
local message = table.concat(messages, '\n')
m.showMessage('Info', message)
end
end
---@alias textEdit {start: integer, finish: integer, text: string}
---@param uri uri
---@param edits textEdit[]
function m.editText(uri, edits)
local files = require 'files'
local textEdits = {}
uri = files.getOriginUri(uri)
for i, edit in ipairs(edits) do
textEdits[i] = define.textEdit(files.range(uri, edit.start, edit.finish), edit.text)
end
proto.request('workspace/applyEdit', {
edit = {
changes = {
[uri] = textEdits,
}
}
})
end
local function hookPrint()
if TEST then
return
end
print = function (...)
m.logMessage('Log', ...)
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)
hookPrint()
end
return m
|