feat: Add support for conceal

This commit is contained in:
Myzel394 2024-04-11 22:22:19 +02:00
parent c33912e91c
commit 76a1307052
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185

View File

@ -1,3 +1,11 @@
--- Type definitions
---@class Options
---@field key_max_length number
---@field max_length number
---@field overflow_marker string
---@field conceal boolean|"auto"
---@field prompt_title string
local json = require"jsonfly.json" local json = require"jsonfly.json"
local finders = require "telescope.finders" local finders = require "telescope.finders"
local pickers = require "telescope.pickers" local pickers = require "telescope.pickers"
@ -32,14 +40,23 @@ local function truncate_overflow(value, max_length, overflow_marker)
return value return value
end end
local function create_display_preview(value) ---@param value any
---@param opts Options
local function create_display_preview(value, opts)
local t = type(value) local t = type(value)
local conceal
if opts.conceal == "auto" then
conceal = vim.o.conceallevel > 0
else
conceal = opts.conceal
end
if t == "table" then if t == "table" then
local preview_table = {} local preview_table = {}
for k, v in pairs(value) do for k, v in pairs(value) do
table.insert(preview_table, k .. ": " .. create_display_preview(v.value)) table.insert(preview_table, k .. ": " .. create_display_preview(v.value, opts))
end end
return "{ " .. table.concat(preview_table, ", ") .. " }", "@label.json" return "{ " .. table.concat(preview_table, ", ") .. " }", "@label.json"
@ -48,7 +65,11 @@ local function create_display_preview(value)
elseif t == "number" then elseif t == "number" then
return tostring(value), "@number.json" return tostring(value), "@number.json"
elseif t == "string" then elseif t == "string" then
return value, "@string.json" if conceal then
return value, "@string.json"
else
return "\"" .. value .. "\"", "@string.json"
end
elseif t == "boolean" then elseif t == "boolean" then
return value and "true" or "false", "@boolean.json" return value and "true" or "false", "@boolean.json"
end end
@ -57,12 +78,17 @@ end
return require"telescope".register_extension { return require"telescope".register_extension {
setup = function() end, setup = function() end,
exports = { exports = {
---@param opts Options
jsonfly = function(opts) jsonfly = function(opts)
opts = opts or {} opts = opts or {}
opts.key_max_length = opts.key_max_length or 50 opts.key_max_length = opts.key_max_length or 50
opts.max_length = opts.max_length or 9999 opts.max_length = opts.max_length or 9999
opts.overflow_marker = opts.overflow_marker or "" opts.overflow_marker = opts.overflow_marker or ""
if opts.conceal == nil then
opts.conceal = "auto"
end
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)
local content_lines = vim.api.nvim_buf_get_lines(current_buf, 0, -1, false) local content_lines = vim.api.nvim_buf_get_lines(current_buf, 0, -1, false)
@ -92,7 +118,7 @@ return require"telescope".register_extension {
value = current_buf, value = current_buf,
ordinal = entry.key, ordinal = entry.key,
display = function(_) display = function(_)
local preview, hl_group = create_display_preview(entry.entry.value) local preview, hl_group = create_display_preview(entry.entry.value, opts)
return displayer { return displayer {
{ depth, "TelescopeResultsNumber"}, { depth, "TelescopeResultsNumber"},