@@ -223,20 +223,68 @@ local function getValidChanges(uri, changes)
223223end
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 ?
233281local 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
0 commit comments