mirror of
https://github.com/Myzel394/jsonfly.nvim.git
synced 2025-06-18 12:15:25 +02:00
feat: Add propery preview; Add basic treesitter support
This commit is contained in:
parent
c6c630babe
commit
c33912e91c
@ -3,6 +3,7 @@ local finders = require "telescope.finders"
|
|||||||
local pickers = require "telescope.pickers"
|
local pickers = require "telescope.pickers"
|
||||||
local conf = require("telescope.config").values
|
local conf = require("telescope.config").values
|
||||||
local make_entry = require "telescope.make_entry"
|
local make_entry = require "telescope.make_entry"
|
||||||
|
local entry_display = require "telescope.pickers.entry_display"
|
||||||
|
|
||||||
local function get_recursive_keys(t)
|
local function get_recursive_keys(t)
|
||||||
local keys = {}
|
local keys = {}
|
||||||
@ -23,11 +24,44 @@ local function get_recursive_keys(t)
|
|||||||
return keys
|
return keys
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function truncate_overflow(value, max_length, overflow_marker)
|
||||||
|
if vim.fn.strdisplaywidth(value) > max_length then
|
||||||
|
return value:sub(1, max_length - vim.fn.strdisplaywidth(overflow_marker)) .. overflow_marker
|
||||||
|
end
|
||||||
|
|
||||||
|
return value
|
||||||
|
end
|
||||||
|
|
||||||
|
local function create_display_preview(value)
|
||||||
|
local t = type(value)
|
||||||
|
|
||||||
|
if t == "table" then
|
||||||
|
local preview_table = {}
|
||||||
|
|
||||||
|
for k, v in pairs(value) do
|
||||||
|
table.insert(preview_table, k .. ": " .. create_display_preview(v.value))
|
||||||
|
end
|
||||||
|
|
||||||
|
return "{ " .. table.concat(preview_table, ", ") .. " }", "@label.json"
|
||||||
|
elseif t == "nil" then
|
||||||
|
return "null", "@constant.builtin.json"
|
||||||
|
elseif t == "number" then
|
||||||
|
return tostring(value), "@number.json"
|
||||||
|
elseif t == "string" then
|
||||||
|
return value, "@string.json"
|
||||||
|
elseif t == "boolean" then
|
||||||
|
return value and "true" or "false", "@boolean.json"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
return require"telescope".register_extension {
|
return require"telescope".register_extension {
|
||||||
setup = function() end,
|
setup = function() end,
|
||||||
exports = {
|
exports = {
|
||||||
jsonfly = function(opts)
|
jsonfly = function(opts)
|
||||||
opts = opts or {}
|
opts = opts or {}
|
||||||
|
opts.key_max_length = opts.key_max_length or 50
|
||||||
|
opts.max_length = opts.max_length or 9999
|
||||||
|
opts.overflow_marker = opts.overflow_marker or "…"
|
||||||
|
|
||||||
local current_buf = vim.api.nvim_get_current_buf()
|
local current_buf = vim.api.nvim_get_current_buf()
|
||||||
local filename = vim.api.nvim_buf_get_name(current_buf)
|
local filename = vim.api.nvim_buf_get_name(current_buf)
|
||||||
@ -37,27 +71,47 @@ return require"telescope".register_extension {
|
|||||||
local parsed = json:decode(content)
|
local parsed = json:decode(content)
|
||||||
local keys = get_recursive_keys(parsed)
|
local keys = get_recursive_keys(parsed)
|
||||||
|
|
||||||
-- print(vim.inspect(keys))
|
local displayer = entry_display.create {
|
||||||
|
separator = " ",
|
||||||
|
items = {
|
||||||
|
{ width = 1 },
|
||||||
|
{ width = opts.key_max_length },
|
||||||
|
{ remaining = true },
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
pickers.new(opts, {
|
pickers.new(opts, {
|
||||||
prompt_title = "colors",
|
prompt_title = "colors",
|
||||||
finder = finders.new_table {
|
finder = finders.new_table {
|
||||||
results = keys,
|
results = keys,
|
||||||
entry_maker = function(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({
|
return make_entry.set_default_entry_mt({
|
||||||
value = vim.inspect(entry.entry[0]),
|
value = current_buf,
|
||||||
ordinal = entry.key,
|
ordinal = entry.key,
|
||||||
display = entry.key,
|
display = function(_)
|
||||||
|
local preview, hl_group = create_display_preview(entry.entry.value)
|
||||||
|
|
||||||
|
return displayer {
|
||||||
|
{ depth, "TelescopeResultsNumber"},
|
||||||
|
{ entry.key .. ": ", "@property.json" },
|
||||||
|
{
|
||||||
|
truncate_overflow(
|
||||||
|
preview,
|
||||||
|
opts.max_length,
|
||||||
|
opts.overflow_marker
|
||||||
|
),
|
||||||
|
hl_group,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
end,
|
||||||
|
|
||||||
bufnr = current_buf,
|
bufnr = current_buf,
|
||||||
filename = filename,
|
filename = filename,
|
||||||
lnum = entry.entry.newlines + 1,
|
lnum = entry.entry.newlines + 1,
|
||||||
col = 2,
|
col = entry.entry.relative_start,
|
||||||
start = 2,
|
|
||||||
finish = 8,
|
|
||||||
|
|
||||||
indicator = 0,
|
|
||||||
extra = 0,
|
|
||||||
}, opts)
|
}, opts)
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user