mirror of
https://github.com/Myzel394/jsonfly.nvim.git
synced 2025-06-18 12:15:25 +02:00
fix: Fix lua parser
This commit is contained in:
parent
a923438f67
commit
e4bd8b0b64
@ -7,21 +7,48 @@
|
|||||||
---@field key string
|
---@field key string
|
||||||
---@field value Entry|table|number|string|boolean|nil
|
---@field value Entry|table|number|string|boolean|nil
|
||||||
---@field position EntryPosition
|
---@field position EntryPosition
|
||||||
|
--
|
||||||
|
---@class JSONEntry
|
||||||
|
---@field value JSONEntry|string|number|boolean|nil
|
||||||
|
---@field line_number number
|
||||||
|
---@field value_start number
|
||||||
|
---@field key_start number
|
||||||
|
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
|
---@param entry JSONEntry
|
||||||
|
local function get_contents_from_json_value(entry)
|
||||||
|
local value = entry.value
|
||||||
|
|
||||||
|
if type(value) == "table" then
|
||||||
|
-- Recursively get the contents of the table
|
||||||
|
local contents = {}
|
||||||
|
|
||||||
|
for k, v in pairs(value) do
|
||||||
|
contents[k] = get_contents_from_json_value(v)
|
||||||
|
end
|
||||||
|
|
||||||
|
return contents
|
||||||
|
else
|
||||||
|
return entry.value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
---@param t table
|
---@param t table
|
||||||
---@return Entry[]
|
---@return Entry[]
|
||||||
function M:get_entries_from_lua_json(t)
|
function M:get_entries_from_lua_json(t)
|
||||||
local keys = {}
|
local keys = {}
|
||||||
|
|
||||||
for k, raw_value in pairs(t) do
|
for k, _raw_value in pairs(t) do
|
||||||
|
---@type JSONEntry
|
||||||
|
local raw_value = _raw_value
|
||||||
---@type Entry
|
---@type Entry
|
||||||
local entry = {
|
local entry = {
|
||||||
key = k,
|
key = k,
|
||||||
value = raw_value,
|
value = get_contents_from_json_value(raw_value),
|
||||||
position = {
|
position = {
|
||||||
line_number = raw_value.newlines,
|
line_number = raw_value.line_number,
|
||||||
key_start = raw_value.key_start,
|
key_start = raw_value.key_start,
|
||||||
value_start = raw_value.value_start,
|
value_start = raw_value.value_start,
|
||||||
}
|
}
|
||||||
@ -37,7 +64,7 @@ function M:get_entries_from_lua_json(t)
|
|||||||
---@type Entry
|
---@type Entry
|
||||||
local entry = {
|
local entry = {
|
||||||
key = k .. "." .. sub_key.key,
|
key = k .. "." .. sub_key.key,
|
||||||
value = sub_key,
|
value = get_contents_from_json_value(sub_key),
|
||||||
position = sub_key.position,
|
position = sub_key.position,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,7 +136,7 @@ function M:get_entries_from_lsp_symbols(symbols)
|
|||||||
key = key,
|
key = key,
|
||||||
value = M:parse_lsp_value(symbol),
|
value = M:parse_lsp_value(symbol),
|
||||||
position = {
|
position = {
|
||||||
line_number = symbol.range.start.line + 2,
|
line_number = symbol.range.start.line + 1,
|
||||||
key_start = symbol.range.start.character + 2,
|
key_start = symbol.range.start.character + 2,
|
||||||
-- The LSP doesn't return the start of the value, so we'll just assume it's 3 characters after the key
|
-- The LSP doesn't return the start of the value, so we'll just assume it's 3 characters after the key
|
||||||
-- We assume a default JSON file like:
|
-- We assume a default JSON file like:
|
||||||
|
@ -47,22 +47,13 @@ local opts = {
|
|||||||
},
|
},
|
||||||
jump_behavior = "key_start",
|
jump_behavior = "key_start",
|
||||||
subkeys_display = "normal",
|
subkeys_display = "normal",
|
||||||
backend = "lsp",
|
backend = "lua",
|
||||||
}
|
}
|
||||||
|
|
||||||
return require"telescope".register_extension {
|
---@param results Entry[]
|
||||||
setup = function(extension_config)
|
---@param buffer number
|
||||||
opts = vim.tbl_deep_extend("force", opts, extension_config or {})
|
local function show_picker(results, buffer)
|
||||||
end,
|
local filename = vim.api.nvim_buf_get_name(buffer)
|
||||||
exports = {
|
|
||||||
jsonfly = function(xopts)
|
|
||||||
local current_buf = vim.api.nvim_get_current_buf()
|
|
||||||
local filename = vim.api.nvim_buf_get_name(current_buf)
|
|
||||||
local content_lines = vim.api.nvim_buf_get_lines(current_buf, 0, -1, false)
|
|
||||||
local content = table.concat(content_lines, "\n")
|
|
||||||
|
|
||||||
local parsed = json:decode(content)
|
|
||||||
local keys = parsers:get_entries_from_lua_json(parsed)
|
|
||||||
|
|
||||||
local displayer = entry_display.create {
|
local displayer = entry_display.create {
|
||||||
separator = " ",
|
separator = " ",
|
||||||
@ -73,31 +64,25 @@ return require"telescope".register_extension {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
local params = vim.lsp.util.make_position_params(xopts.winnr)
|
|
||||||
local result = vim.lsp.buf_request(
|
|
||||||
current_buf,
|
|
||||||
"textDocument/documentSymbol",
|
|
||||||
params,
|
|
||||||
function(_, result)
|
|
||||||
local keys = parsers:get_entries_from_lsp_symbols(result)
|
|
||||||
|
|
||||||
pickers.new(opts, {
|
pickers.new(opts, {
|
||||||
prompt_title = opts.prompt_title,
|
prompt_title = opts.prompt_title,
|
||||||
finder = finders.new_table {
|
finder = finders.new_table {
|
||||||
results = keys,
|
results = results,
|
||||||
---@param entry Entry
|
---@param entry Entry
|
||||||
entry_maker = function(entry)
|
entry_maker = function(entry)
|
||||||
local _, raw_depth = entry.key:gsub("%.", ".")
|
local _, raw_depth = entry.key:gsub("%.", ".")
|
||||||
local depth = (raw_depth or 0) + 1
|
local depth = (raw_depth or 0) + 1
|
||||||
|
|
||||||
return make_entry.set_default_entry_mt({
|
return make_entry.set_default_entry_mt({
|
||||||
value = current_buf,
|
value = buffer,
|
||||||
ordinal = entry.key,
|
ordinal = entry.key,
|
||||||
display = function(_)
|
display = function(_)
|
||||||
local preview, hl_group_key = utils:create_display_preview(entry.value, opts)
|
local preview, hl_group_key = utils:create_display_preview(entry.value, opts)
|
||||||
|
|
||||||
local key = opts.subkeys_display == "normal" and entry.key or utils:replace_previous_keys(entry.key, " ")
|
local key = opts.subkeys_display == "normal" and entry.key or utils:replace_previous_keys(entry.key, " ")
|
||||||
|
|
||||||
|
print(vim.inspect(entry))
|
||||||
|
|
||||||
return displayer {
|
return displayer {
|
||||||
{ depth, "TelescopeResultsNumber"},
|
{ depth, "TelescopeResultsNumber"},
|
||||||
{
|
{
|
||||||
@ -119,7 +104,7 @@ return require"telescope".register_extension {
|
|||||||
}
|
}
|
||||||
end,
|
end,
|
||||||
|
|
||||||
bufnr = current_buf,
|
bufnr = buffer,
|
||||||
filename = filename,
|
filename = filename,
|
||||||
lnum = entry.position.line_number,
|
lnum = entry.position.line_number,
|
||||||
col = opts.jump_behavior == "key_start"
|
col = opts.jump_behavior == "key_start"
|
||||||
@ -134,59 +119,45 @@ return require"telescope".register_extension {
|
|||||||
sorting_strategy = "ascending",
|
sorting_strategy = "ascending",
|
||||||
}):find()
|
}):find()
|
||||||
end
|
end
|
||||||
)
|
|
||||||
|
|
||||||
-- pickers.new(opts, {
|
return require"telescope".register_extension {
|
||||||
-- prompt_title = opts.prompt_title,
|
setup = function(extension_config)
|
||||||
-- finder = finders.new_table {
|
opts = vim.tbl_deep_extend("force", opts, extension_config or {})
|
||||||
-- results = keys,
|
end,
|
||||||
-- entry_maker = function(entry)
|
exports = {
|
||||||
-- local _, raw_depth = entry.key:gsub("%.", ".")
|
jsonfly = function(xopts)
|
||||||
-- local depth = (raw_depth or 0) + 1
|
local current_buf = vim.api.nvim_get_current_buf()
|
||||||
--
|
local content_lines = vim.api.nvim_buf_get_lines(current_buf, 0, -1, false)
|
||||||
-- return make_entry.set_default_entry_mt({
|
local content = table.concat(content_lines, "\n")
|
||||||
-- value = current_buf,
|
|
||||||
-- ordinal = entry.key,
|
function run_lua_parser()
|
||||||
-- display = function(_)
|
local parsed = json:decode(content)
|
||||||
-- local preview, hl_group_key = create_display_preview(entry.entry.value, opts)
|
local keys = parsers:get_entries_from_lua_json(parsed)
|
||||||
--
|
|
||||||
-- local key = opts.subkeys_display == "normal" and entry.key or replace_previous_keys(entry.key, " ")
|
show_picker(keys, current_buf)
|
||||||
--
|
end
|
||||||
-- return displayer {
|
|
||||||
-- { depth, "TelescopeResultsNumber"},
|
if opts.backend == "lsp" then
|
||||||
-- {
|
local params = vim.lsp.util.make_position_params(xopts.winnr)
|
||||||
-- truncate_overflow(
|
|
||||||
-- key,
|
vim.lsp.buf_request(
|
||||||
-- opts.key_max_length,
|
current_buf,
|
||||||
-- opts.overflow_marker
|
"textDocument/documentSymbol",
|
||||||
-- ),
|
params,
|
||||||
-- "@property.json",
|
function(error, lsp_response)
|
||||||
-- },
|
if error then
|
||||||
-- {
|
run_lua_parser()
|
||||||
-- truncate_overflow(
|
return
|
||||||
-- preview,
|
end
|
||||||
-- opts.max_length,
|
|
||||||
-- opts.overflow_marker
|
local result = parsers:get_entries_from_lsp_symbols(lsp_response)
|
||||||
-- ),
|
|
||||||
-- opts.highlights[hl_group_key] or "TelescopeResultsString",
|
show_picker(result, current_buf)
|
||||||
-- },
|
end
|
||||||
-- }
|
)
|
||||||
-- end,
|
else
|
||||||
--
|
run_lua_parser()
|
||||||
-- bufnr = current_buf,
|
end
|
||||||
-- filename = filename,
|
|
||||||
-- lnum = entry.entry.newlines + 1,
|
|
||||||
-- col = opts.jump_behavior == "key_start"
|
|
||||||
-- and entry.entry.key_start
|
|
||||||
-- -- Use length ("#" operator) as vim jumps to the bytes, not characters
|
|
||||||
-- or entry.entry.value_start
|
|
||||||
-- }, opts)
|
|
||||||
-- end,
|
|
||||||
-- },
|
|
||||||
-- previewer = conf.grep_previewer(opts),
|
|
||||||
-- sorter = conf.generic_sorter(opts),
|
|
||||||
-- sorting_strategy = "ascending",
|
|
||||||
-- }):find()
|
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user