diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2023-02-04 03:51:43 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2023-02-04 03:51:43 +0800 |
commit | 32cdecde294924771f4a2c379bdab83817b7d38e (patch) | |
tree | 63cb49f23e9428355c752ef393c8f9010189c696 | |
parent | c602d39994f92b5780faf9e9f504f2a29e484864 (diff) | |
download | lua-language-server-32cdecde294924771f4a2c379bdab83817b7d38e.zip |
should write complete array or object into json
#1859
-rw-r--r-- | script/client.lua | 22 | ||||
-rw-r--r-- | test/tclient/tests/modify-luarc.lua | 50 |
2 files changed, 62 insertions, 10 deletions
diff --git a/script/client.lua b/script/client.lua index 30680254..9f450b4c 100644 --- a/script/client.lua +++ b/script/client.lua @@ -278,10 +278,11 @@ local function searchPatchInfo(cfg, rawKey) } end +---@param uri uri ---@param cfg table ---@param change config.change ---@return json.patch? -local function makeConfigPatch(cfg, change) +local function makeConfigPatch(uri, cfg, change) local info = searchPatchInfo(cfg, change.key) if change.action == 'add' then if type(info.value) == 'table' and #info.value > 0 then @@ -291,10 +292,10 @@ local function makeConfigPatch(cfg, change) value = change.value, } else - return makeConfigPatch(cfg, { + return makeConfigPatch(uri, cfg, { action = 'set', key = change.key, - value = { change.value }, + value = config.get(uri, change.key), }) end elseif change.action == 'set' then @@ -312,27 +313,28 @@ local function makeConfigPatch(cfg, change) } end elseif change.action == 'prop' then - if type(info.value) == 'table' and #info.value == 0 then + if type(info.value) == 'table' and next(info.value) then return { op = 'add', path = info.key .. '/' .. change.prop, value = change.value, } else - return makeConfigPatch(cfg, { + return makeConfigPatch(uri, cfg, { action = 'set', key = change.key, - value = { [change.prop] = change.value }, + value = config.get(uri, change.key), }) end end return nil end +---@param uri uri ---@param path string ---@param changes config.change[] ---@return string? -local function editConfigJson(path, changes) +local function editConfigJson(uri, path, changes) local text = util.loadFile(path) if not text then m.showMessage('Error', lang.script('CONFIG_LOAD_FAILED', path)) @@ -348,7 +350,7 @@ local function editConfigJson(path, changes) end ---@cast res table for _, change in ipairs(changes) do - local patch = makeConfigPatch(res, change) + local patch = makeConfigPatch(uri, res, change) if patch then text = jsone.edit(text, patch, { indent = ' ' }) end @@ -387,7 +389,7 @@ local function tryModifySpecifiedConfig(uri, finalChanges) if not path then return false end - local newJson = editConfigJson(path, validChanges) + local newJson = editConfigJson(uri, path, validChanges) if not newJson then return false end @@ -420,7 +422,7 @@ local function tryModifyRC(uri, finalChanges, create) if not buf then util.saveFile(path, '') end - local newJson = editConfigJson(path, validChanges) + local newJson = editConfigJson(uri, path, validChanges) if not newJson then return false end diff --git a/test/tclient/tests/modify-luarc.lua b/test/tclient/tests/modify-luarc.lua index 240ae582..62d97a41 100644 --- a/test/tclient/tests/modify-luarc.lua +++ b/test/tclient/tests/modify-luarc.lua @@ -6,6 +6,7 @@ local jsonb = require 'json-beautify' local client = require 'client' local provider = require 'provider' local json = require 'json' +local config = require 'config' local configPath = LOGPATH .. '/modify-luarc.json' @@ -324,4 +325,53 @@ lclient():start(function (languageClient) } } })) + + ------------------------------- + -- merrge other configs -- + ------------------------------- + + util.saveFile(configPath, jsonb.beautify(json.createEmptyObject())) + + provider.updateConfig() + + config.add(nil, 'Lua.diagnostics.globals', 'x') + config.add(nil, 'Lua.diagnostics.globals', 'y') + + client.setConfig({ + { + action = 'add', + key = 'Lua.diagnostics.globals', + value = 'z', + } + }) + + assert(util.equal(jsonc.decode_jsonc(util.loadFile(configPath)), { + ['diagnostics.globals'] = { 'x', 'y', 'z' } + })) + + ------------------------------- + + util.saveFile(configPath, jsonb.beautify(json.createEmptyObject())) + + provider.updateConfig() + + config.prop(nil, 'Lua.runtime.special', 'kx', 'require') + config.prop(nil, 'Lua.runtime.special', 'ky', 'require') + + client.setConfig({ + { + action = 'prop', + key = 'Lua.runtime.special', + prop = 'kz', + value = 'require', + } + }) + + assert(util.equal(jsonc.decode_jsonc(util.loadFile(configPath)), { + ['runtime.special'] = { + ['kx'] = 'require', + ['ky'] = 'require', + ['kz'] = 'require', + } + })) end) |