diff --git a/README.md b/README.md index 518327a..a2c1bda 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # 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. -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. @@ -48,7 +48,7 @@ Here's how I load it with lazy.nvim with lazy-loading and `j` as the key "j", "Telescope jsonfly", desc = "Open json(fly)", - ft = { "json" }, + ft = { "json", "xml", "yaml" }, mode = "n" } } diff --git a/lua/jsonfly/parsers.lua b/lua/jsonfly/parsers.lua index eee1f5d..9536ff8 100644 --- a/lua/jsonfly/parsers.lua +++ b/lua/jsonfly/parsers.lua @@ -19,6 +19,12 @@ local PRIMITIVE_TYPES = { number = true, boolean = true, } +local CONTAINS_CHILDREN_TYPES = { + [2] = true, -- Module / Javascript Object + [8] = true, -- Field + [18] = true, -- Array + [19] = true, -- Object +} local M = {} @@ -91,7 +97,7 @@ end ---@return string|number|table|boolean|nil function M:parse_lsp_value(result) -- Object - if result.kind == 2 then + if CONTAINS_CHILDREN_TYPES[result.kind] then local value = {} for _, child in ipairs(result.children) do @@ -165,7 +171,7 @@ function M:get_entries_from_lsp_symbols(symbols) } 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) for jindex=1, #sub_keys do diff --git a/lua/jsonfly/utils.lua b/lua/jsonfly/utils.lua index 17c36b8..5256498 100644 --- a/lua/jsonfly/utils.lua +++ b/lua/jsonfly/utils.lua @@ -63,14 +63,18 @@ end ---@param value any ---@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) if t == "table" then + if render_objects == false then + return "", "other" + end local preview_table = {} 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 return "{ " .. table.concat(preview_table, ", ") .. " }", "other" diff --git a/lua/telescope/_extensions/jsonfly.lua b/lua/telescope/_extensions/jsonfly.lua index 720ca46..089484b 100644 --- a/lua/telescope/_extensions/jsonfly.lua +++ b/lua/telescope/_extensions/jsonfly.lua @@ -10,6 +10,7 @@ ---@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 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 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 @@ -55,6 +56,7 @@ local DEFAULT_CONFIG = { }, jump_behavior = "key_start", subkeys_display = "normal", + show_nested_child_preview = true, backend = "lsp", use_cache = 500, commands = { @@ -116,7 +118,7 @@ local function show_picker(entries, buffer, xopts) value = buffer, ordinal = entry.key, 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, " ") @@ -194,17 +196,19 @@ return require("telescope").register_extension { if global_config.backend == "lsp" then local params = vim.lsp.util.make_position_params(xopts.winnr) - vim.lsp.buf_request( + vim.lsp.buf_request_all( current_buf, "textDocument/documentSymbol", params, - function(error, lsp_response) - if error then + function(response) + if response == nil or #response == 0 then run_lua_parser() return 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 cache:cache_buffer(current_buf, entries)