Merge pull request #13 from Myzel394/fix-preview

Fix preview
This commit is contained in:
Myzel394 2024-05-26 00:33:47 +02:00 committed by GitHub
commit ddbc0849cb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 26 additions and 12 deletions

View File

@ -1,9 +1,9 @@
# jsonfly.nvim # jsonfly.nvim
Fly through your JSON files with ease. Fly through your JSON, XML and YAML files with ease.
Search ✨ blazingly fast ✨ for keys via [Telescope](https://github.com/nvim-telescope/telescope.nvim), navigate through your JSON structure with ease, and insert deeply nested keys without fear. Search ✨ blazingly fast ✨ for keys via [Telescope](https://github.com/nvim-telescope/telescope.nvim), navigate through your JSON structure with ease, and insert deeply nested keys without fear.
json(fly) is a Telescope extension that will show you all keys (including nested ones) in your JSON files and allow you to search and jump to them quickly. json(fly) is a Telescope extension that will show you all keys (including nested ones) in your JSON (or XML or YAML) files and allow you to search and jump to them quickly.
It's completely customizable and even supports highlighting of the values. It's completely customizable and even supports highlighting of the values.
<img src="docs/horizontal_layout.png"> <img src="docs/horizontal_layout.png">
@ -48,7 +48,7 @@ Here's how I load it with lazy.nvim with lazy-loading and `<leader>j` as the key
"<leader>j", "<leader>j",
"<cmd>Telescope jsonfly<cr>", "<cmd>Telescope jsonfly<cr>",
desc = "Open json(fly)", desc = "Open json(fly)",
ft = { "json" }, ft = { "json", "xml", "yaml" },
mode = "n" mode = "n"
} }
} }

View File

@ -19,6 +19,12 @@ local PRIMITIVE_TYPES = {
number = true, number = true,
boolean = true, boolean = true,
} }
local CONTAINS_CHILDREN_TYPES = {
[2] = true, -- Module / Javascript Object
[8] = true, -- Field
[18] = true, -- Array
[19] = true, -- Object
}
local M = {} local M = {}
@ -91,7 +97,7 @@ end
---@return string|number|table|boolean|nil ---@return string|number|table|boolean|nil
function M:parse_lsp_value(result) function M:parse_lsp_value(result)
-- Object -- Object
if result.kind == 2 then if CONTAINS_CHILDREN_TYPES[result.kind] then
local value = {} local value = {}
for _, child in ipairs(result.children) do for _, child in ipairs(result.children) do
@ -165,7 +171,7 @@ function M:get_entries_from_lsp_symbols(symbols)
} }
keys[#keys + 1] = entry keys[#keys + 1] = entry
if symbol.kind == 2 or symbol.kind == 18 then if CONTAINS_CHILDREN_TYPES[symbol.kind] then
local sub_keys = M:get_entries_from_lsp_symbols(symbol.children) local sub_keys = M:get_entries_from_lsp_symbols(symbol.children)
for jindex=1, #sub_keys do for jindex=1, #sub_keys do

View File

@ -63,14 +63,18 @@ end
---@param value any ---@param value any
---@param conceal boolean ---@param conceal boolean
function M:create_display_preview(value, conceal) ---@param render_objects boolean
function M:create_display_preview(value, conceal, render_objects)
local t = type(value) local t = type(value)
if t == "table" then if t == "table" then
if render_objects == false then
return "", "other"
end
local preview_table = {} local preview_table = {}
for k, v in pairs(value) do for k, v in pairs(value) do
preview_table[#preview_table + 1] = k .. ": " .. M:create_display_preview(v, conceal) preview_table[#preview_table + 1] = k .. ": " .. M:create_display_preview(v, conceal, render_objects)
end end
return "{ " .. table.concat(preview_table, ", ") .. " }", "other" return "{ " .. table.concat(preview_table, ", ") .. " }", "other"

View File

@ -10,6 +10,7 @@
---@field highlights Highlights - Highlight groups for different types ---@field highlights Highlights - Highlight groups for different types
---@field jump_behavior "key_start"|"value_start" - Behavior for jumping to the location, "key_start" == Jump to the start of the key, "value_start" == Jump to the start of the value, Default: "key_start" ---@field jump_behavior "key_start"|"value_start" - Behavior for jumping to the location, "key_start" == Jump to the start of the key, "value_start" == Jump to the start of the value, Default: "key_start"
---@field subkeys_display "normal"|"waterfall" - Display subkeys in a normal or waterfall style, Default: "normal" ---@field subkeys_display "normal"|"waterfall" - Display subkeys in a normal or waterfall style, Default: "normal"
---@field show_nested_child_preview boolean - Whether to show a preview of nested children, Default: true
---@field backend "lua"|"lsp" - Backend to use for parsing JSON, "lua" = Use our own Lua parser to parse the JSON, "lsp" = Use your LSP to parse the JSON (currently only https://github.com/Microsoft/vscode-json-languageservice is supported). If the "lsp" backend is selected but the LSP fails, it will fallback to the "lua" backend, Default: "lsp" ---@field backend "lua"|"lsp" - Backend to use for parsing JSON, "lua" = Use our own Lua parser to parse the JSON, "lsp" = Use your LSP to parse the JSON (currently only https://github.com/Microsoft/vscode-json-languageservice is supported). If the "lsp" backend is selected but the LSP fails, it will fallback to the "lua" backend, Default: "lsp"
---@field use_cache number - Whether to use cache the parsed JSON. The cache will be activated if the number of lines is greater or equal to this value, By default, the cache is activate when the file if 1000 lines or more; `0` to disable the cache, Default: 500 ---@field use_cache number - Whether to use cache the parsed JSON. The cache will be activated if the number of lines is greater or equal to this value, By default, the cache is activate when the file if 1000 lines or more; `0` to disable the cache, Default: 500
---@field commands Commands - Shortcuts for commands ---@field commands Commands - Shortcuts for commands
@ -55,6 +56,7 @@ local DEFAULT_CONFIG = {
}, },
jump_behavior = "key_start", jump_behavior = "key_start",
subkeys_display = "normal", subkeys_display = "normal",
show_nested_child_preview = true,
backend = "lsp", backend = "lsp",
use_cache = 500, use_cache = 500,
commands = { commands = {
@ -116,7 +118,7 @@ local function show_picker(entries, buffer, xopts)
value = buffer, value = buffer,
ordinal = entry.key, ordinal = entry.key,
display = function(_) display = function(_)
local preview, hl_group_key = utils:create_display_preview(entry.value, conceal) local preview, hl_group_key = utils:create_display_preview(entry.value, conceal, global_config.show_nested_child_preview)
local key = global_config.subkeys_display == "normal" and entry.key or utils:replace_previous_keys(entry.key, " ") local key = global_config.subkeys_display == "normal" and entry.key or utils:replace_previous_keys(entry.key, " ")
@ -194,17 +196,19 @@ return require("telescope").register_extension {
if global_config.backend == "lsp" then if global_config.backend == "lsp" then
local params = vim.lsp.util.make_position_params(xopts.winnr) local params = vim.lsp.util.make_position_params(xopts.winnr)
vim.lsp.buf_request( vim.lsp.buf_request_all(
current_buf, current_buf,
"textDocument/documentSymbol", "textDocument/documentSymbol",
params, params,
function(error, lsp_response) function(response)
if error then if response == nil or #response == 0 then
run_lua_parser() run_lua_parser()
return return
end end
local entries = parsers:get_entries_from_lsp_symbols(lsp_response) local result = response[1].result
local entries = parsers:get_entries_from_lsp_symbols(result)
if allow_cache then if allow_cache then
cache:cache_buffer(current_buf, entries) cache:cache_buffer(current_buf, entries)