diff --git a/lua/jsonfly/insert.lua b/lua/jsonfly/insert.lua index f4a0996..330bc9e 100644 --- a/lua/jsonfly/insert.lua +++ b/lua/jsonfly/insert.lua @@ -74,15 +74,42 @@ local function write_keys(keys, index) end local insertions = write_keys(keys, index + 1) + local key = keys[index] - lines[#lines + 1] = "\"" .. keys[index].key .. "\": {" + if key.type == "object" then + lines[#lines + 1] = "\"" .. key.key .. "\": {" - for ii=1, #insertions do - lines[#lines + 1] = insertions[ii] + for ii=1, #insertions do + lines[#lines + 1] = insertions[ii] + end + + lines[#lines + 1] = "}" + elseif key.type == "array" then + lines[#lines + 1] = "[" + + for ii=1, #insertions do + lines[#lines + 1] = insertions[ii] + end + + lines[#lines + 1] = "]" + elseif key.type == "array_index" then + local amount = tonumber(key.key) + + -- Write previous empty array objects + for _=1, amount do + lines[#lines + 1] = "{}," + end + + -- Write key + lines[#lines + 1] = "{" + + for ii=1, #insertions do + lines[#lines + 1] = insertions[ii] + end + + lines[#lines + 1] = "}" end - lines[#lines + 1] = "}" - return lines end @@ -151,6 +178,8 @@ function M:insert_new_key(entries, keys, buffer) end end + print(vim.inspect(remaining_keys)) + -- Hacky way to jump to end of object vim.api.nvim_win_set_cursor(0, {entry.position.line_number, entry.position.value_start}) vim.cmd [[execute "normal %"]] diff --git a/lua/jsonfly/utils.lua b/lua/jsonfly/utils.lua index 5caa893..bb3d953 100644 --- a/lua/jsonfly/utils.lua +++ b/lua/jsonfly/utils.lua @@ -1,6 +1,55 @@ ---@class KeyDescription ---@field key string ----@field type "object"|"array"|"string" +---@field type "object"|"array"|"array_index" + +-- Examples: +--{ +-- hello: [ +-- { +-- test: "abc" +-- } +-- ] +--} +-- hello.[0].test +-- { key = "hello", type = "object" } +-- { type = "array" } +-- { type = "array_index", key = 0 } +-- { key = "test", type = "object" } +-- +--{ +-- hello: [ +-- [ +-- { +-- test: "abc" +-- } +-- ] +-- ] +--} +-- hello.[0].[0].test +-- { key = "hello", type = "object" } +-- { type = "array" } +-- { type = "array_index", key = 0 } +-- { type = "array" } +-- { type = "array_index", key = 0 } +-- { key = "test", type = "object" } +-- +--{ +-- hello: [ +-- {}, +-- [ +-- { +-- test: "abc" +-- } +-- ] +-- ] +--} +-- hello.[1].[0].test +-- { key = "hello", type = "object" } +-- { type = "array" } +-- { type = "array_index", key = 1 } +-- { type = "array" } +-- { type = "array_index", key = 0 } +-- { key = "test", type = "object" } local M = {} @@ -87,27 +136,37 @@ function M:extract_key_description(text) local keys = {} local splitted = M:split_by_char(text, ".") - for index=1, #splitted do + + local index = 1 + + while index <= #splitted do local token = splitted[index] if string.sub(token, 1, 1) == "[" then + local array_index = tonumber(string.sub(token, 2, -2)) + keys[#keys + 1] = { - key = tonumber(string.sub(token, 2, -2)), type = "array", } + keys[#keys + 1] = { + key = array_index, + type = "array_index", + } else keys[#keys + 1] = { key = token, type = "object", } end + + index = index + 1 end if #keys == 0 then return { { key = text, - type = "string", + type = "object", } } end