Skip to content

Commit 6999dbb

Browse files
committed
modify config fully supports .luarc.json
fix #831
1 parent 867d84d commit 6999dbb

File tree

7 files changed

+226
-14
lines changed

7 files changed

+226
-14
lines changed

changelog.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# changelog
22

33
## 3.6.5
4+
* `FIX` [#831]
45
* `FIX` [#1729]
56

7+
[#831]: https://github.com./sumneko/lua-language-server/issues/831
68
[#1729]: https://github.com./sumneko/lua-language-server/issues/1729
79

810
`2022-11-29`

locale/en-us/script.lua

+2
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,8 @@ CONFIG_LOAD_ERROR =
562562
'Setting file loading error: {}'
563563
CONFIG_TYPE_ERROR =
564564
'The setting file must be in lua or json format: {}'
565+
CONFIG_MODIFY_FAIL_SYNTAX_ERROR =
566+
'Failed to modify settings, there are syntax errors in the settings file: {}'
565567

566568
PLUGIN_RUNTIME_ERROR =
567569
[[

locale/pt-br/script.lua

+2
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,8 @@ CONFIG_LOAD_ERROR =
562562
'Configurando o erro de carregamento do arquivo: {}'
563563
CONFIG_TYPE_ERROR =
564564
'O arquivo de configuração deve estar no formato LUA ou JSON: {}'
565+
CONFIG_MODIFY_FAIL_SYNTAX_ERROR = -- TODO: need translate!
566+
'Failed to modify settings, there are syntax errors in the settings file: {}'
565567

566568
PLUGIN_RUNTIME_ERROR =
567569
[[

locale/zh-cn/script.lua

+2
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,8 @@ CONFIG_LOAD_ERROR =
562562
'设置文件加载错误:{}'
563563
CONFIG_TYPE_ERROR =
564564
'设置文件必须是lua或json格式:{}'
565+
CONFIG_MODIFY_FAIL_SYNTAX_ERROR =
566+
'修改设置失败,设置文件中有语法错误:{}'
565567

566568
PLUGIN_RUNTIME_ERROR =
567569
[[

locale/zh-tw/script.lua

+2
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,8 @@ CONFIG_LOAD_ERROR =
562562
'設定檔案載入錯誤:{}'
563563
CONFIG_TYPE_ERROR =
564564
'設定檔案必須是lua或json格式:{}'
565+
CONFIG_MODIFY_FAIL_SYNTAX_ERROR = -- TODO: need translate!
566+
'Failed to modify settings, there are syntax errors in the settings file: {}'
565567

566568
PLUGIN_RUNTIME_ERROR =
567569
[[

script/client.lua

+66-14
Original file line numberDiff line numberDiff line change
@@ -223,20 +223,68 @@ local function getValidChanges(uri, changes)
223223
end
224224

225225
---@class json.patch
226-
---@field op 'add' | 'remove' | 'replace' | 'move' | 'copy' | 'test'
226+
---@field op 'add' | 'remove' | 'replace'
227227
---@field path string
228228
---@field value any
229229

230+
---@class json.patchInfo
231+
---@field key string
232+
---@field value any
233+
234+
---@param cfg table
235+
---@param rawKey string
236+
---@return json.patchInfo
237+
local function searchPatchInfo(cfg, rawKey)
238+
239+
---@param key string
240+
---@param parentKey string
241+
---@param parentValue table
242+
---@return json.patchInfo?
243+
local function searchOnce(key, parentKey, parentValue)
244+
if parentValue == nil then
245+
return nil
246+
end
247+
if type(parentValue) ~= 'table' then
248+
return {
249+
key = parentKey,
250+
value = parentValue,
251+
}
252+
end
253+
if parentValue[key] then
254+
return {
255+
key = parentKey .. '/' .. key,
256+
value = parentValue[key],
257+
}
258+
end
259+
for pos in key:gmatch '()%.' do
260+
local k = key:sub(1, pos - 1)
261+
local v = parentValue[k]
262+
local info = searchOnce(key:sub(pos + 1), parentKey .. '/' .. k, v)
263+
if info then
264+
return info
265+
end
266+
end
267+
return nil
268+
end
269+
270+
return searchOnce(rawKey, '', cfg)
271+
or searchOnce(rawKey:gsub('^Lua%.', ''), '', cfg)
272+
or {
273+
key = '/' .. rawKey,
274+
value = nil,
275+
}
276+
end
277+
230278
---@param cfg table
231279
---@param change config.change
232280
---@return json.patch?
233281
local function makeConfigPatch(cfg, change)
234-
local value = cfg[change.key]
282+
local info = searchPatchInfo(cfg, change.key)
235283
if change.action == 'add' then
236-
if type(value) == 'table' and #cfg[change.key] > 0 then
284+
if type(info.value) == 'table' and #info.value > 0 then
237285
return {
238286
op = 'add',
239-
path = '/' .. change.key .. '/-',
287+
path = info.key .. '/-',
240288
value = change.value,
241289
}
242290
else
@@ -247,24 +295,24 @@ local function makeConfigPatch(cfg, change)
247295
})
248296
end
249297
elseif change.action == 'set' then
250-
if value ~= nil then
298+
if info.value ~= nil then
251299
return {
252300
op = 'replace',
253-
path = '/' .. change.key,
301+
path = info.key,
254302
value = change.value,
255303
}
256304
else
257305
return {
258306
op = 'add',
259-
path = '/' .. change.key,
307+
path = info.key,
260308
value = change.value,
261309
}
262310
end
263311
elseif change.action == 'prop' then
264-
if type(value) == 'table' and #value == 0 then
312+
if type(info.value) == 'table' and #info.value == 0 then
265313
return {
266314
op = 'add',
267-
path = '/' .. change.key .. '/' .. change.prop,
315+
path = info.key .. '/' .. change.prop,
268316
value = change.value,
269317
}
270318
else
@@ -286,13 +334,17 @@ local function editConfigJson(path, changes)
286334
if not text then
287335
return nil
288336
end
289-
local cfg = jsonc.decode_jsonc(text)
290-
if type(cfg) ~= 'table' then
291-
cfg = {}
337+
local suc, res = pcall(jsonc.decode_jsonc, text)
338+
if not suc then
339+
m.showMessage('Error', lang.script('CONFIG_MODIFY_FAIL_SYNTAX_ERROR', path .. res:match 'ERROR(.+)$'))
340+
return text
341+
end
342+
if type(res) ~= 'table' then
343+
res = {}
292344
end
293-
---@cast cfg table
345+
---@cast res table
294346
for _, change in ipairs(changes) do
295-
local patch = makeConfigPatch(cfg, change)
347+
local patch = makeConfigPatch(res, change)
296348
if patch then
297349
text = jsone.edit(text, patch, { indent = ' ' })
298350
end

test/tclient/tests/modify-luarc.lua

+150
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,26 @@ lclient():start(function (languageClient)
3939

4040
-------------------------------
4141

42+
util.saveFile(configPath, jsonb.beautify {
43+
['Lua.runtime.version'] = json.null,
44+
})
45+
46+
provider.updateConfig()
47+
48+
client.setConfig({
49+
{
50+
action = 'set',
51+
key = 'Lua.runtime.version',
52+
value = 'LuaJIT',
53+
}
54+
})
55+
56+
assert(util.equal(jsonc.decode_jsonc(util.loadFile(configPath)), {
57+
['Lua.runtime.version'] = 'LuaJIT',
58+
}))
59+
60+
-------------------------------
61+
4262
util.saveFile(configPath, jsonb.beautify(json.createEmptyObject()))
4363

4464
provider.updateConfig()
@@ -174,4 +194,134 @@ lclient():start(function (languageClient)
174194
}
175195
}))
176196

197+
-------------------------------
198+
199+
util.saveFile(configPath, jsonb.beautify {
200+
['runtime.version'] = json.null,
201+
})
202+
203+
provider.updateConfig()
204+
205+
client.setConfig({
206+
{
207+
action = 'set',
208+
key = 'Lua.runtime.version',
209+
value = 'LuaJIT',
210+
}
211+
})
212+
213+
assert(util.equal(jsonc.decode_jsonc(util.loadFile(configPath)), {
214+
['runtime.version'] = 'LuaJIT',
215+
}))
216+
217+
-------------------------------
218+
219+
util.saveFile(configPath, jsonb.beautify {
220+
Lua = {
221+
runtime = {
222+
version = json.null,
223+
}
224+
}
225+
})
226+
227+
provider.updateConfig()
228+
229+
client.setConfig({
230+
{
231+
action = 'set',
232+
key = 'Lua.runtime.version',
233+
value = 'LuaJIT',
234+
}
235+
})
236+
237+
assert(util.equal(jsonc.decode_jsonc(util.loadFile(configPath)), {
238+
Lua = {
239+
runtime = {
240+
version = 'LuaJIT',
241+
}
242+
}
243+
}))
244+
245+
-------------------------------
246+
247+
util.saveFile(configPath, jsonb.beautify {
248+
runtime = {
249+
version = json.null,
250+
}
251+
})
252+
253+
provider.updateConfig()
254+
255+
client.setConfig({
256+
{
257+
action = 'set',
258+
key = 'Lua.runtime.version',
259+
value = 'LuaJIT',
260+
}
261+
})
262+
263+
assert(util.equal(jsonc.decode_jsonc(util.loadFile(configPath)), {
264+
runtime = {
265+
version = 'LuaJIT',
266+
}
267+
}))
268+
269+
-------------------------------
270+
271+
util.saveFile(configPath, jsonb.beautify {
272+
diagnostics = {
273+
disable = {
274+
'unused-local',
275+
}
276+
}
277+
})
278+
279+
provider.updateConfig()
280+
281+
client.setConfig({
282+
{
283+
action = 'add',
284+
key = 'Lua.diagnostics.disable',
285+
value = 'undefined-global',
286+
}
287+
})
288+
289+
assert(util.equal(jsonc.decode_jsonc(util.loadFile(configPath)), {
290+
diagnostics = {
291+
disable = {
292+
'unused-local',
293+
'undefined-global',
294+
}
295+
}
296+
}))
297+
298+
-------------------------------
299+
300+
util.saveFile(configPath, jsonb.beautify {
301+
runtime = {
302+
special = {
303+
import = 'require',
304+
}
305+
}
306+
})
307+
308+
provider.updateConfig()
309+
310+
client.setConfig({
311+
{
312+
action = 'prop',
313+
key = 'Lua.runtime.special',
314+
prop = 'include',
315+
value = 'require',
316+
}
317+
})
318+
319+
assert(util.equal(jsonc.decode_jsonc(util.loadFile(configPath)), {
320+
runtime = {
321+
special = {
322+
import = 'require',
323+
include = 'require',
324+
}
325+
}
326+
}))
177327
end)

0 commit comments

Comments
 (0)