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 value Entry|table|number|string|boolean|nil
|
||||
---@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 = {}
|
||||
|
||||
---@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
|
||||
---@return Entry[]
|
||||
function M:get_entries_from_lua_json(t)
|
||||
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
|
||||
local entry = {
|
||||
key = k,
|
||||
value = raw_value,
|
||||
value = get_contents_from_json_value(raw_value),
|
||||
position = {
|
||||
line_number = raw_value.newlines,
|
||||
line_number = raw_value.line_number,
|
||||
key_start = raw_value.key_start,
|
||||
value_start = raw_value.value_start,
|
||||
}
|
||||
@ -37,7 +64,7 @@ function M:get_entries_from_lua_json(t)
|
||||
---@type Entry
|
||||
local entry = {
|
||||
key = k .. "." .. sub_key.key,
|
||||
value = sub_key,
|
||||
value = get_contents_from_json_value(sub_key),
|
||||
position = sub_key.position,
|
||||
}
|
||||
|
||||
@ -109,7 +136,7 @@ function M:get_entries_from_lsp_symbols(symbols)
|
||||
key = key,
|
||||
value = M:parse_lsp_value(symbol),
|
||||
position = {
|
||||
line_number = symbol.range.start.line + 2,
|
||||
line_number = symbol.range.start.line + 1,
|
||||
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
|
||||
-- We assume a default JSON file like:
|
||||
|
@ -47,22 +47,13 @@ local opts = {
|
||||
},
|
||||
jump_behavior = "key_start",
|
||||
subkeys_display = "normal",
|
||||
backend = "lsp",
|
||||
backend = "lua",
|
||||
}
|
||||
|
||||
return require"telescope".register_extension {
|
||||
setup = function(extension_config)
|
||||
opts = vim.tbl_deep_extend("force", opts, extension_config or {})
|
||||
end,
|
||||
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)
|
||||
---@param results Entry[]
|
||||
---@param buffer number
|
||||
local function show_picker(results, buffer)
|
||||
local filename = vim.api.nvim_buf_get_name(buffer)
|
||||
|
||||
local displayer = entry_display.create {
|
||||
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, {
|
||||
prompt_title = opts.prompt_title,
|
||||
finder = finders.new_table {
|
||||
results = keys,
|
||||
results = results,
|
||||
---@param entry Entry
|
||||
entry_maker = function(entry)
|
||||
local _, raw_depth = entry.key:gsub("%.", ".")
|
||||
local depth = (raw_depth or 0) + 1
|
||||
|
||||
return make_entry.set_default_entry_mt({
|
||||
value = current_buf,
|
||||
value = buffer,
|
||||
ordinal = entry.key,
|
||||
display = function(_)
|
||||
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, " ")
|
||||
|
||||
print(vim.inspect(entry))
|
||||
|
||||
return displayer {
|
||||
{ depth, "TelescopeResultsNumber"},
|
||||
{
|
||||
@ -119,7 +104,7 @@ return require"telescope".register_extension {
|
||||
}
|
||||
end,
|
||||
|
||||
bufnr = current_buf,
|
||||
bufnr = buffer,
|
||||
filename = filename,
|
||||
lnum = entry.position.line_number,
|
||||
col = opts.jump_behavior == "key_start"
|
||||
@ -134,59 +119,45 @@ return require"telescope".register_extension {
|
||||
sorting_strategy = "ascending",
|
||||
}):find()
|
||||
end
|
||||
)
|
||||
|
||||
-- pickers.new(opts, {
|
||||
-- prompt_title = opts.prompt_title,
|
||||
-- finder = finders.new_table {
|
||||
-- results = keys,
|
||||
-- entry_maker = function(entry)
|
||||
-- local _, raw_depth = entry.key:gsub("%.", ".")
|
||||
-- local depth = (raw_depth or 0) + 1
|
||||
--
|
||||
-- return make_entry.set_default_entry_mt({
|
||||
-- value = current_buf,
|
||||
-- ordinal = entry.key,
|
||||
-- display = function(_)
|
||||
-- local preview, hl_group_key = create_display_preview(entry.entry.value, opts)
|
||||
--
|
||||
-- local key = opts.subkeys_display == "normal" and entry.key or replace_previous_keys(entry.key, " ")
|
||||
--
|
||||
-- return displayer {
|
||||
-- { depth, "TelescopeResultsNumber"},
|
||||
-- {
|
||||
-- truncate_overflow(
|
||||
-- key,
|
||||
-- opts.key_max_length,
|
||||
-- opts.overflow_marker
|
||||
-- ),
|
||||
-- "@property.json",
|
||||
-- },
|
||||
-- {
|
||||
-- truncate_overflow(
|
||||
-- preview,
|
||||
-- opts.max_length,
|
||||
-- opts.overflow_marker
|
||||
-- ),
|
||||
-- opts.highlights[hl_group_key] or "TelescopeResultsString",
|
||||
-- },
|
||||
-- }
|
||||
-- end,
|
||||
--
|
||||
-- bufnr = current_buf,
|
||||
-- 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()
|
||||
return require"telescope".register_extension {
|
||||
setup = function(extension_config)
|
||||
opts = vim.tbl_deep_extend("force", opts, extension_config or {})
|
||||
end,
|
||||
exports = {
|
||||
jsonfly = function(xopts)
|
||||
local current_buf = vim.api.nvim_get_current_buf()
|
||||
local content_lines = vim.api.nvim_buf_get_lines(current_buf, 0, -1, false)
|
||||
local content = table.concat(content_lines, "\n")
|
||||
|
||||
function run_lua_parser()
|
||||
local parsed = json:decode(content)
|
||||
local keys = parsers:get_entries_from_lua_json(parsed)
|
||||
|
||||
show_picker(keys, current_buf)
|
||||
end
|
||||
|
||||
if opts.backend == "lsp" then
|
||||
local params = vim.lsp.util.make_position_params(xopts.winnr)
|
||||
|
||||
vim.lsp.buf_request(
|
||||
current_buf,
|
||||
"textDocument/documentSymbol",
|
||||
params,
|
||||
function(error, lsp_response)
|
||||
if error then
|
||||
run_lua_parser()
|
||||
return
|
||||
end
|
||||
|
||||
local result = parsers:get_entries_from_lsp_symbols(lsp_response)
|
||||
|
||||
show_picker(result, current_buf)
|
||||
end
|
||||
)
|
||||
else
|
||||
run_lua_parser()
|
||||
end
|
||||
end
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user