Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix preview #13

Merged
merged 3 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

<img src="docs/horizontal_layout.png">
Expand Down Expand Up @@ -48,7 +48,7 @@ Here's how I load it with lazy.nvim with lazy-loading and `<leader>j` as the key
"<leader>j",
"<cmd>Telescope jsonfly<cr>",
desc = "Open json(fly)",
ft = { "json" },
ft = { "json", "xml", "yaml" },
mode = "n"
}
}
Expand Down
10 changes: 8 additions & 2 deletions lua/jsonfly/parsers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions lua/jsonfly/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 9 additions & 5 deletions lua/telescope/_extensions/jsonfly.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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, " ")

Expand Down Expand Up @@ -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)
Expand Down
Loading