mirror of
https://github.com/Myzel394/jsonfly.nvim.git
synced 2025-06-18 04:05:26 +02:00
fix: Properly write arrays; improve parser
This commit is contained in:
parent
b8ba2ec072
commit
55bdb800fa
@ -74,14 +74,41 @@ 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]
|
||||
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
|
||||
|
||||
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 %"]]
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user