diff --git a/config/nvim/lua/fschauen/plugins/telescope.lua b/config/nvim/lua/fschauen/plugins/telescope.lua index 54e8255..4fd161a 100644 --- a/config/nvim/lua/fschauen/plugins/telescope.lua +++ b/config/nvim/lua/fschauen/plugins/telescope.lua @@ -9,49 +9,113 @@ M.dependencies = { M.cmd = 'Telescope' -local pick = require('fschauen.telescope').pickers +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', pick.autocommands ' Autocommands' , desc = ' Telescope [a]utocommands' }, - { 'fb', pick.buffers ' Buffers' , desc = ' Telescope [b]uffers' }, - { 'fc', pick.colorscheme ' Colorschemes' , desc = ' Telescope [c]olorschemes' }, - { 'fdd', pick.diagnostics '󰀪 Document Diagnostics' , desc = ' Telescope [d]iagnostics [d]ocument' }, - { 'fdw', pick.diagnostics '󰀪 Workspace Diagnostics', desc = ' Telescope [d]iagnostics [w]orkspace' }, + { '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', pick.find_files ' Files' , desc = ' Telescope [f]ind files' }, - { 'fF', pick.all_files ' ALL files' , desc = ' Telescope all [F]iles' }, - { 'fgr', pick.live_grep ' Live grep' , desc = ' Telescope Live [gr]ep' }, - { 'fgf', pick.git_files ' Git files' , desc = ' Telescope [g]it [f]iles' }, - { 'fgc', pick.git_commits ' Commits' , desc = ' Telescope [g]it [c]ommits' }, - { 'fh', pick.here ' Current buffer' , desc = ' Telescope [b]uffer [h]ere' }, - { 'fH', pick.highlights '󰌶 Highlights' , desc = ' Telescope [H]ighlights' }, + { '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', pick.jumplist ' Jumplist' , desc = ' Telescope [j]umplist' }, - { 'fk', pick.keymaps ' Keymaps' , desc = ' Telescope [k]eymaps' }, - { 'fK', pick.help_tags ' Help tags' , desc = ' Telescope [K] help/documentation' }, - { 'fl', pick.loclist ' Location list' , desc = ' Telescope [l]ocation List' }, - { 'fm', pick.man_pages ' Man pages' , desc = ' Telescope [m]an pages' }, + { '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', pick.vim_options ' Vim options' , desc = ' Telescope vim [o]ptions' }, + { 'fo', pickers.vim_options ' Vim options' , desc = ' Telescope vim [o]ptions' }, --'fp' - { 'fq', pick.quickfix ' Quickfix' , desc = ' Telescope [q]uickfix' }, - { 'fr', pick.lsp_references ' References' , desc = ' Telescope [r]eferences' }, - { 'fR', pick.registers '󱓥 Registers' , desc = ' Telescope [R]registers' }, - { 'fs', pick.lsp_document_symbols '󰫧 Document Symbols ' , desc = ' Telescope lsp document [s]ymbols' }, - { 'fS', pick.lsp_workspace_symbols '󱄑 Workspace Symbols ' , desc = ' Telescope lsp workspace [S]ymbols' }, + { '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', pick.treesitter ' Treesitter symbols' , desc = ' Telescope [T]reesitter Symbols' }, + { 'fT', pickers.treesitter ' Treesitter symbols' , desc = ' Telescope [T]reesitter Symbols' }, --'fu' --'fv' - { 'fw', pick.selection ' Grep' , desc = ' Telescope [w]word under cursor' }, - { 'fw', pick.selection ' Grep', mode = 'v' , desc = ' Telescope [w]ord(s) selected' }, + { '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', pick.spell_suggest '󰓆 Spelling suggestions' , desc = ' Telescope [z] spell suggestions' }, - { 'f.', pick.dotfiles ' Dotfiles' , desc = ' Telescope [.]dotfiles' }, - { 'f:', pick.command_history ' Command history' , desc = ' Telescope [:]command history' }, - { 'f?', pick.commands ' Commands' , desc = ' Telescope commands [?]' }, - { 'f/', pick.search_history ' Search history' , desc = ' Telescope [/]search history' }, - { 'f', pick.resume '󰐎 Resume' , desc = ' Telescope Resume ' }, + { '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 ' }, } M.opts = function() diff --git a/config/nvim/lua/fschauen/telescope.lua b/config/nvim/lua/fschauen/telescope.lua deleted file mode 100644 index 06e0b71..0000000 --- a/config/nvim/lua/fschauen/telescope.lua +++ /dev/null @@ -1,69 +0,0 @@ -M = {} - -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 - -M.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 -}) - -return M -