local M = { 'nvim-telescope/telescope.nvim' } M.dependencies = { 'nvim-telescope/telescope-fzf-native.nvim', build = 'cmake -S. -Bbuild -DCMAKE_BUILD_TYPE=Release ' .. '&& cmake --build build --config Release ' .. '&& cmake --install build --prefix build', } M.cmd = 'Telescope' local builtin = function(picker, opts) return function(title) return function() local args = vim.tbl_extend('keep', { prompt_title = title }, opts or {}) return require('telescope.builtin')[picker](args) end end end ---Preserve register contents over function call. ---@param reg string: register to save, must be a valid register name. ---@param func function: function that may freely clobber the register. ---@return any: return value of calling `func`. local with_saved_register = function(reg, func) local saved = vim.fn.getreg(reg) local result = func() vim.fn.setreg(reg, saved) return result end ---Get selected text. ---@return string: selected text, or work under cursor if not in visual mode. local get_selected_text = function() if vim.fn.mode() ~= 'v' then return vim.fn.expand '' end return with_saved_register('v', function() vim.cmd [[noautocmd sil norm "vy]] return vim.fn.getreg 'v' end) end local pickers = setmetatable({ all_files = builtin('find_files', { hidden = true, no_ignore = true, no_ignore_parent = true, }), colorscheme = builtin('colorscheme', { enable_preview = true, }), diagnostics = builtin('diagnostics', { bufnr = 0 }), dotfiles = builtin('find_files', { cwd = '~/.dotfiles', hidden = true, }), selection = function(title) return function() local text = get_selected_text() return require('telescope.builtin').grep_string { prompt_title = string.format(title .. ': %s ', text), search = text, } end end, here = builtin('current_buffer_fuzzy_find'), }, { -- Fall back to telescope's built-in pickers if a custom one is not defined -- above, but make sure to keep the title we defined. __index = function(_, picker) return builtin(picker) end }) M.keys = { { 'fa', pickers.autocommands ' Autocommands' , desc = ' Telescope [a]utocommands' }, { 'fb', pickers.buffers ' Buffers' , desc = ' Telescope [b]uffers' }, { 'fc', pickers.colorscheme ' Colorschemes' , desc = ' Telescope [c]olorschemes' }, { 'fdd', pickers.diagnostics '󰀪 Document Diagnostics' , desc = ' Telescope [d]iagnostics [d]ocument' }, { 'fdw', pickers.diagnostics '󰀪 Workspace Diagnostics', desc = ' Telescope [d]iagnostics [w]orkspace' }, --'fe' { 'ff', pickers.find_files ' Files' , desc = ' Telescope [f]ind files' }, { 'fF', pickers.all_files ' ALL files' , desc = ' Telescope all [F]iles' }, { 'fgr', pickers.live_grep ' Live grep' , desc = ' Telescope Live [gr]ep' }, { 'fgf', pickers.git_files ' Git files' , desc = ' Telescope [g]it [f]iles' }, { 'fgc', pickers.git_commits ' Commits' , desc = ' Telescope [g]it [c]ommits' }, { 'fh', pickers.here ' Current buffer' , desc = ' Telescope [b]uffer [h]ere' }, { 'fH', pickers.highlights '󰌶 Highlights' , desc = ' Telescope [H]ighlights' }, --'fi' { 'fj', pickers.jumplist ' Jumplist' , desc = ' Telescope [j]umplist' }, { 'fk', pickers.keymaps ' Keymaps' , desc = ' Telescope [k]eymaps' }, { 'fK', pickers.help_tags ' Help tags' , desc = ' Telescope [K] help/documentation' }, { 'fl', pickers.loclist ' Location list' , desc = ' Telescope [l]ocation List' }, { 'fm', pickers.man_pages ' Man pages' , desc = ' Telescope [m]an pages' }, --'fn' { 'fo', pickers.vim_options ' Vim options' , desc = ' Telescope vim [o]ptions' }, --'fp' { 'fq', pickers.quickfix ' Quickfix' , desc = ' Telescope [q]uickfix' }, { 'fr', pickers.lsp_references ' References' , desc = ' Telescope [r]eferences' }, { 'fR', pickers.registers '󱓥 Registers' , desc = ' Telescope [R]registers' }, { 'fs', pickers.lsp_document_symbols '󰫧 Document Symbols ' , desc = ' Telescope lsp document [s]ymbols' }, { 'fS', pickers.lsp_workspace_symbols '󱄑 Workspace Symbols ' , desc = ' Telescope lsp workspace [S]ymbols' }, --'ft' used in todo_comments { 'fT', pickers.treesitter ' Treesitter symbols' , desc = ' Telescope [T]reesitter Symbols' }, --'fu' --'fv' { 'fw', pickers.selection ' Grep' , desc = ' Telescope [w]word under cursor' }, { 'fw', pickers.selection ' Grep', mode = 'v' , desc = ' Telescope [w]ord(s) selected' }, --'fx' --'fy' { 'fz', pickers.spell_suggest '󰓆 Spelling suggestions' , desc = ' Telescope [z] spell suggestions' }, { 'f.', pickers.dotfiles ' Dotfiles' , desc = ' Telescope [.]dotfiles' }, { 'f:', pickers.command_history ' Command history' , desc = ' Telescope [:]command history' }, { 'f?', pickers.commands ' Commands' , desc = ' Telescope commands [?]' }, { 'f/', pickers.search_history ' Search history' , desc = ' Telescope [/]search history' }, { 'f', pickers.resume '󰐎 Resume' , desc = ' Telescope Resume ' }, } local icons = require('fschauen.icons') M.config = function() local actions = require('telescope.actions') local layout = require('telescope.actions.layout') local trouble = vim.F.npcall(require, 'trouble.providers.telescope') or {} local mappings = { [''] = actions.cycle_history_next, [''] = actions.cycle_history_prev, [''] = actions.preview_scrolling_down, [''] = actions.preview_scrolling_up, [''] = layout.cycle_layout_next, [''] = layout.toggle_mirror, [''] = actions.close, [''] = actions.smart_send_to_qflist + actions.open_qflist, [''] = actions.smart_send_to_loclist + actions.open_loclist, [''] = trouble.smart_open_with_trouble, } require('telescope').setup { defaults = { mappings = { i = mappings, n = mappings, }, prompt_prefix = '  ', selection_caret = icons.ui.Play, multi_icon = icons.ui.Checkmark, scroll_strategy = 'limit', -- Don't wrap around in results. dynamic_preview_title = true, layout_strategy = 'flex', layout_config = { width = 0.9, height = 0.9, flex = { flip_columns = 130 }, horizontal = { preview_width = 0.5, preview_cutoff = 130 }, vertical = { preview_height = 0.5 }, }, cycle_layout_list = { 'horizontal', 'vertical' }, }, pickers = { buffers = { mappings = { n = { x = actions.delete_buffer, }, }, }, colorscheme = { theme = 'dropdown', }, spell_suggest = { theme = 'cursor', }, }, extensions = { file_browser = { theme = 'ivy' }, }, } require('telescope').load_extension 'fzf' vim.api.nvim_create_autocmd('User', { desc = 'Enable line number in Telescope previewers.', group = vim.api.nvim_create_augroup('fschauen.telescope', { clear = true } ), pattern = 'TelescopePreviewerLoaded', command = 'setlocal number' }) end return M