fix: Pass global config forward

This commit is contained in:
Myzel394 2024-05-12 21:51:43 +02:00
parent acd899ce82
commit 3207a4fbef
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185

View File

@ -32,14 +32,14 @@ local insert = require"jsonfly.insert"
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"
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 entry_display = require "telescope.pickers.entry_display"
local action_state = require "telescope.actions.state" local action_state = require "telescope.actions.state"
---@type Options ---@type Options
local opts = { local DEFAULT_CONFIG = {
key_max_length = 50, key_max_length = 50,
key_exact_length = false, key_exact_length = false,
max_length = 9999, max_length = 9999,
@ -62,34 +62,37 @@ local opts = {
} }
} }
local global_config = {}
---@param entries Entry[] ---@param entries Entry[]
---@param buffer number ---@param buffer number
local function show_picker(entries, buffer) local function show_picker(entries, buffer, xopts)
local config = vim.tbl_deep_extend("force", global_config, xopts or {})
local filename = vim.api.nvim_buf_get_name(buffer) local filename = vim.api.nvim_buf_get_name(buffer)
local displayer = entry_display.create { local displayer = entry_display.create {
separator = " ", separator = " ",
items = { items = {
{ width = 1 }, { width = 1 },
opts.key_exact_length and { width = opts.key_max_length } or { remaining = true }, global_config.key_exact_length and { width = global_config.key_max_length } or { remaining = true },
{ remaining = true }, { remaining = true },
}, },
} }
---@type boolean ---@type boolean
local conceal local conceal
if opts.conceal == "auto" then if global_config.conceal == "auto" then
conceal = vim.o.conceallevel > 0 conceal = vim.o.conceallevel > 0
else else
conceal = opts.conceal == true conceal = global_config.conceal == true
end end
pickers.new(opts, { pickers.new(config, {
prompt_title = opts.prompt_title, prompt_title = global_config.prompt_title,
attach_mappings = function(_, map) attach_mappings = function(_, map)
map( map(
opts.commands.add_key[1], global_config.commands.add_key[1],
opts.commands.add_key[2], global_config.commands.add_key[2],
function(prompt_bufnr) function(prompt_bufnr)
local current_picker = action_state.get_current_picker(prompt_bufnr) local current_picker = action_state.get_current_picker(prompt_bufnr)
local input = current_picker:_get_prompt() local input = current_picker:_get_prompt()
@ -115,25 +118,25 @@ local function show_picker(entries, buffer)
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)
local key = opts.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, " ")
return displayer { return displayer {
{ depth, "TelescopeResultsNumber"}, { depth, "TelescopeResultsNumber"},
{ {
utils:truncate_overflow( utils:truncate_overflow(
key, key,
opts.key_max_length, global_config.key_max_length,
opts.overflow_marker global_config.overflow_marker
), ),
"@property.json", "@property.json",
}, },
{ {
utils:truncate_overflow( utils:truncate_overflow(
preview, preview,
opts.max_length, global_config.max_length,
opts.overflow_marker global_config.overflow_marker
), ),
opts.highlights[hl_group_key] or "TelescopeResultsString", global_config.highlights[hl_group_key] or "TelescopeResultsString",
}, },
} }
end, end,
@ -141,22 +144,22 @@ local function show_picker(entries, buffer)
bufnr = buffer, bufnr = buffer,
filename = filename, filename = filename,
lnum = entry.position.line_number, lnum = entry.position.line_number,
col = opts.jump_behavior == "key_start" col = global_config.jump_behavior == "key_start"
and entry.position.key_start and entry.position.key_start
-- Use length ("#" operator) as vim jumps to the bytes, not characters -- Use length ("#" operator) as vim jumps to the bytes, not characters
or entry.position.value_start or entry.position.value_start
}, opts) }, config)
end, end,
}, },
previewer = conf.grep_previewer(opts), previewer = conf.grep_previewer(config),
sorter = conf.generic_sorter(opts), sorter = conf.generic_sorter(config),
sorting_strategy = "ascending", sorting_strategy = "ascending",
}):find() }):find()
end end
return require"telescope".register_extension { return require("telescope").register_extension {
setup = function(extension_config) setup = function(extension_config)
opts = vim.tbl_deep_extend("force", opts, extension_config or {}) global_config = vim.tbl_deep_extend("force", DEFAULT_CONFIG, extension_config or {})
end, end,
exports = { exports = {
jsonfly = function(xopts) jsonfly = function(xopts)
@ -165,13 +168,13 @@ return require"telescope".register_extension {
local cached_entries = cache:get_cache(current_buf) local cached_entries = cache:get_cache(current_buf)
if cached_entries ~= nil then if cached_entries ~= nil then
show_picker(cached_entries, current_buf) show_picker(cached_entries, current_buf, xopts)
return return
end end
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)
local content = table.concat(content_lines, "\n") local content = table.concat(content_lines, "\n")
local allow_cache = opts.use_cache > 0 and #content_lines >= opts.use_cache local allow_cache = global_config.use_cache > 0 and #content_lines >= global_config.use_cache
if allow_cache then if allow_cache then
cache:register_listeners(current_buf) cache:register_listeners(current_buf)
@ -185,10 +188,10 @@ return require"telescope".register_extension {
cache:cache_buffer(current_buf, entries) cache:cache_buffer(current_buf, entries)
end end
show_picker(entries, current_buf) show_picker(entries, current_buf, xopts)
end end
if opts.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(
@ -207,7 +210,7 @@ return require"telescope".register_extension {
cache:cache_buffer(current_buf, entries) cache:cache_buffer(current_buf, entries)
end end
show_picker(entries, current_buf) show_picker(entries, current_buf, xopts)
end end
) )
else else