Skip to content

Commit 6f3c3d8

Browse files
committed
add workaround for editting empty object
1 parent 671f1f4 commit 6f3c3d8

File tree

2 files changed

+31
-28
lines changed

2 files changed

+31
-28
lines changed

script/client.lua

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,11 @@ end
231231
---@param change config.change
232232
---@return json.patch?
233233
local function makeConfigPatch(cfg, change)
234+
local value = cfg[change.key]
235+
local parent = cfg
236+
local parentKey = ''
234237
if change.action == 'add' then
235-
if type(cfg[change.key]) == 'table' and #cfg[change.key] > 0 then
238+
if type(value) == 'table' and #cfg[change.key] > 0 then
236239
return {
237240
op = 'add',
238241
path = '/' .. change.key .. '/-',
@@ -246,21 +249,31 @@ local function makeConfigPatch(cfg, change)
246249
})
247250
end
248251
elseif change.action == 'set' then
249-
if cfg[change.key] ~= nil then
250-
return {
251-
op = 'replace',
252-
path = '/' .. change.key,
253-
data = change.value,
254-
}
252+
if next(parent) then
253+
if value ~= nil then
254+
return {
255+
op = 'replace',
256+
path = '/' .. change.key,
257+
data = change.value,
258+
}
259+
else
260+
return {
261+
op = 'add',
262+
path = '/' .. change.key,
263+
data = change.value,
264+
}
265+
end
255266
else
267+
-- TODO: workaround, json-edit cannot edit an empty object
256268
return {
257269
op = 'add',
258-
path = '/' .. change.key,
259-
data = change.value,
270+
path = parentKey,
271+
data = { [change.key] = change.value },
260272
}
261273
end
262274
elseif change.action == 'prop' then
263-
if type(cfg[change.key]) == 'table' and #cfg[change.key] == 0 then
275+
if type(value) == 'table' and #value == 0 and next(value) then
276+
-- TODO: workaround, json-edit cannot edit an empty object
264277
return {
265278
op = 'add',
266279
path = '/' .. change.key .. '/' .. change.prop,

test/tclient/tests/modify-luarc.lua

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ lclient():start(function (languageClient)
2121

2222
-------------------------------
2323

24-
util.saveFile(configPath, jsonb.beautify {
25-
['xxxx'] = 1, -- TODO: bug of json-edit, can not be an empty json
26-
})
24+
util.saveFile(configPath, jsonb.beautify(json.createEmptyObject()))
2725

2826
provider.updateConfig()
2927

@@ -36,15 +34,12 @@ lclient():start(function (languageClient)
3634
})
3735

3836
assert(util.equal(jsonc.decode_jsonc(util.loadFile(configPath)), {
39-
['xxxx'] = 1,
4037
['Lua.runtime.version'] = 'LuaJIT',
4138
}))
4239

4340
-------------------------------
4441

45-
util.saveFile(configPath, jsonb.beautify {
46-
['xxxx'] = 1, -- TODO: bug of json-edit, can not be an empty json
47-
})
42+
util.saveFile(configPath, jsonb.beautify(json.createEmptyObject()))
4843

4944
provider.updateConfig()
5045

@@ -57,7 +52,6 @@ lclient():start(function (languageClient)
5752
})
5853

5954
assert(util.equal(jsonc.decode_jsonc(util.loadFile(configPath)), {
60-
['xxxx'] = 1,
6155
['Lua.diagnostics.disable'] = {
6256
'undefined-global',
6357
}
@@ -112,9 +106,7 @@ lclient():start(function (languageClient)
112106

113107
-------------------------------
114108

115-
util.saveFile(configPath, jsonb.beautify {
116-
['xxxx'] = 1, -- TODO: bug of json-edit, can not be an empty json
117-
})
109+
util.saveFile(configPath, jsonb.beautify(json.createEmptyObject()))
118110

119111
provider.updateConfig()
120112

@@ -128,7 +120,6 @@ lclient():start(function (languageClient)
128120
})
129121

130122
assert(util.equal(jsonc.decode_jsonc(util.loadFile(configPath)), {
131-
['xxxx'] = 1,
132123
['Lua.runtime.special'] = {
133124
['include'] = 'require',
134125
}
@@ -151,12 +142,11 @@ lclient():start(function (languageClient)
151142
}
152143
})
153144

154-
-- TODO: bug of json-edit, can not be an empty json
155-
-- assert(util.equal(jsonc.decode_jsonc(util.loadFile(configPath)), {
156-
-- ['Lua.runtime.special'] = {
157-
-- ['include'] = 'require',
158-
-- }
159-
-- }))
145+
assert(util.equal(jsonc.decode_jsonc(util.loadFile(configPath)), {
146+
['Lua.runtime.special'] = {
147+
['include'] = 'require',
148+
}
149+
}))
160150

161151
-------------------------------
162152

0 commit comments

Comments
 (0)