From 77b61eaf592fe7d1c300ac55625a0f3bb2302b9a Mon Sep 17 00:00:00 2001 From: Martin Larsson Date: Sun, 30 Jun 2024 23:47:25 +0200 Subject: [PATCH] Optimize and slightly refactor the setup for telescope. We now only setup our theme and the picker options once instead of each time we call to open one. --- home/.config/nvim/lua/plugs/telescope.lua | 98 +++++++++++++---------- 1 file changed, 56 insertions(+), 42 deletions(-) diff --git a/home/.config/nvim/lua/plugs/telescope.lua b/home/.config/nvim/lua/plugs/telescope.lua index da3b0c5..b562ac9 100644 --- a/home/.config/nvim/lua/plugs/telescope.lua +++ b/home/.config/nvim/lua/plugs/telescope.lua @@ -5,16 +5,6 @@ return { "nvim-telescope/telescope-fzf-native.nvim", }, config = function() - local dropdown = require("telescope.themes").get_dropdown({ - borderchars = { - prompt = { "─", "│", "─", "│", "┌", "┐", "┘", "└" }, - results = { "─", "│", "─", "│", "├", "┤", "┘", "└" }, - preview = { "─", "│", "─", "│", "┌", "┐", "┘", "└" }, - }, - prompt_title = "", - winblend = 20 - }) - require("telescope").setup({ extensions = { fzf = { @@ -28,38 +18,62 @@ return { require("telescope").load_extension("fzf") local builtin = require("telescope.builtin") - require("utils").add_keymaps({ - n = { - ["to"] = { - cmd = function() - builtin.find_files(vim.tbl_extend("force", dropdown, { - prompt_prefix = "Files> ", - previewer = false, - })) - end - }, - ["tf"] = { - cmd = function() - builtin.current_buffer_fuzzy_find(vim.tbl_extend("force", dropdown, { - prompt_prefix = "Find> " - })) - end - }, - ["ta"] = { - cmd = function() - builtin.live_grep(vim.tbl_extend("force", dropdown, { - prompt_prefix = "Grep> " - })) - end - }, - ["tb"] = { - cmd = function() - builtin.marks(vim.tbl_extend("force", dropdown, { - prompt_prefix = "Marks> " - })) - end - } - } + + local dropdown = require("telescope.themes").get_dropdown({ + borderchars = { + prompt = { "─", "│", "─", "│", "┌", "┐", "┘", "└" }, + results = { "─", "│", "─", "│", "├", "┤", "┘", "└" }, + preview = { "─", "│", "─", "│", "┌", "┐", "┘", "└" }, + }, + prompt_title = "", + winblend = 20 }) + + local pickers = { + { + fn = builtin.find_files, + key = "o", + picker_opts = { + prompt_prefix = "Files> ", + previewer = false, + }, + }, + { + fn = builtin.current_buffer_fuzzy_find, + key = "f", + picker_opts = { + prompt_prefix = "Find> " + }, + }, + { + fn = builtin.live_grep, + key = "a", + picker_opts = { + prompt_prefix = "Grep> " + }, + }, + { + fn = builtin.marks, + key = "b", + picker_opts = { + prompt_prefix = "Marks> " + }, + }, + } + -- Cache the options with the dropdown theme for each picker so we don't + -- recalculate it every time we open a picker + for _, v in ipairs(pickers) do + v.picker_opts = vim.tbl_extend("force", dropdown, v.picker_opts) + end + + local keymaps = { n = {} } + for _, v in ipairs(pickers) do + keymaps.n["t" .. v.key] = { + cmd = function() + v.fn(v.picker_opts) + end + } + end + require("utils").add_keymaps(keymaps) end, }