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,15 +74,42 @@ local function write_keys(keys, index)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local insertions = write_keys(keys, index + 1)
|
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
|
for ii=1, #insertions do
|
||||||
lines[#lines + 1] = insertions[ii]
|
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
|
end
|
||||||
|
|
||||||
lines[#lines + 1] = "}"
|
|
||||||
|
|
||||||
return lines
|
return lines
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -151,6 +178,8 @@ function M:insert_new_key(entries, keys, buffer)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
print(vim.inspect(remaining_keys))
|
||||||
|
|
||||||
-- Hacky way to jump to end of object
|
-- Hacky way to jump to end of object
|
||||||
vim.api.nvim_win_set_cursor(0, {entry.position.line_number, entry.position.value_start})
|
vim.api.nvim_win_set_cursor(0, {entry.position.line_number, entry.position.value_start})
|
||||||
vim.cmd [[execute "normal %"]]
|
vim.cmd [[execute "normal %"]]
|
||||||
|
@ -1,6 +1,55 @@
|
|||||||
---@class KeyDescription
|
---@class KeyDescription
|
||||||
---@field key string
|
---@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 = {}
|
local M = {}
|
||||||
|
|
||||||
@ -87,27 +136,37 @@ function M:extract_key_description(text)
|
|||||||
local keys = {}
|
local keys = {}
|
||||||
|
|
||||||
local splitted = M:split_by_char(text, ".")
|
local splitted = M:split_by_char(text, ".")
|
||||||
for index=1, #splitted do
|
|
||||||
|
local index = 1
|
||||||
|
|
||||||
|
while index <= #splitted do
|
||||||
local token = splitted[index]
|
local token = splitted[index]
|
||||||
|
|
||||||
if string.sub(token, 1, 1) == "[" then
|
if string.sub(token, 1, 1) == "[" then
|
||||||
|
local array_index = tonumber(string.sub(token, 2, -2))
|
||||||
|
|
||||||
keys[#keys + 1] = {
|
keys[#keys + 1] = {
|
||||||
key = tonumber(string.sub(token, 2, -2)),
|
|
||||||
type = "array",
|
type = "array",
|
||||||
}
|
}
|
||||||
|
keys[#keys + 1] = {
|
||||||
|
key = array_index,
|
||||||
|
type = "array_index",
|
||||||
|
}
|
||||||
else
|
else
|
||||||
keys[#keys + 1] = {
|
keys[#keys + 1] = {
|
||||||
key = token,
|
key = token,
|
||||||
type = "object",
|
type = "object",
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
index = index + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
if #keys == 0 then
|
if #keys == 0 then
|
||||||
return {
|
return {
|
||||||
{
|
{
|
||||||
key = text,
|
key = text,
|
||||||
type = "string",
|
type = "object",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user