diff --git a/config/nvim/lua/fschauen/plugins/telescope.lua b/config/nvim/lua/fschauen/plugins/telescope.lua index fe0258a..3607a5b 100644 --- a/config/nvim/lua/fschauen/plugins/telescope.lua +++ b/config/nvim/lua/fschauen/plugins/telescope.lua @@ -1,3 +1,20 @@ +local prefix = require('fschauen.telescope').prefix +local pickers = require('fschauen.telescope').pickers +local actions = require('fschauen.telescope').actions + +local mappings = { + [''] = actions.cycle_history_next, + [''] = actions.cycle_history_prev, + [''] = actions.preview_scrolling_down, + [''] = actions.preview_scrolling_up, + [''] = actions.cycle_layout_next, + [''] = actions.toggle_mirror, + [''] = actions.close, + [''] = actions.smart_send_to_qflist_and_open, + [''] = actions.smart_send_to_loclist_and_open, + [''] = actions.smart_open_with_trouble +} + return { 'nvim-telescope/telescope.nvim', @@ -10,177 +27,102 @@ return { '&& cmake --install build --prefix build' }, }, + lazy = true, + cmd = 'Telescope', + opts = { + defaults = { + mappings = { + i = mappings, + n = mappings, + }, - config = function() - local telescope = require 'telescope' - local actions = require 'telescope.actions' - local actions_layout = require 'telescope.actions.layout' + prompt_prefix = '  ', -- Alternatives:  ❯ + selection_caret = ' ', -- Alternatives:  ➔  - local mappings = { - [''] = actions_layout.cycle_layout_next, - [''] = actions_layout.toggle_mirror, - [''] = actions.smart_send_to_qflist + actions.open_qflist, - [''] = actions.smart_send_to_loclist + actions.open_loclist, - [''] = actions.close, + multi_icon = '󰄬 ', -- Alternatives: 󰄬   + scroll_strategy = 'limit', -- Don't wrap around in results. - [''] = actions.preview_scrolling_down, - [''] = actions.preview_scrolling_up, + dynamic_preview_title = true, - [''] = actions.cycle_history_next, - [''] = actions.cycle_history_prev, - } - - local trouble = vim.F.npcall(require, 'trouble.providers.telescope') - if trouble then - mappings[''] = trouble.open_with_trouble - end - - telescope.setup { - defaults = { - -- ╭────────╮ - -- │ Keymap │ - -- ╰────────╯ - mappings = { i = mappings, n = mappings }, - - -- ╭────────╮ - -- │ Prompt │ - -- ╰────────╯ - prompt_prefix = ' ❯ ', - selection_caret = ' ', -- Other ideas:  ➔  - - -- ╭─────────╮ - -- │ Results │ - -- ╰─────────╯ - multi_icon = ' ', - scroll_strategy = 'limit', -- Don't wrap around in results. - - -- ╭─────────╮ - -- │ Preview │ - -- ╰─────────╯ - dynamic_preview_title = true, - - -- ╭────────╮ - -- │ Layout │ - -- ╰────────╯ - 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 }, + 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, + }, }, - cycle_layout_list = { 'horizontal', 'vertical' }, }, - - extensions = { - file_browser = { theme = 'ivy' }, + colorscheme = { + theme = 'dropdown', }, - } - - local themes = require 'telescope.themes' - local ts = require 'telescope.builtin' - local my = { - all_files = function(opts) - ts.find_files(vim.tbl_extend('keep', opts or {}, { - hidden = true, - no_ignore = true, - no_ignore_parent = true, - })) - end, - colorschemes = function(opts) - ts.colorscheme(themes.get_dropdown(vim.tbl_extend('keep', opts or {}, { - enable_preview = true, - }))) - end, - diagnostics = function(opts) - ts.diagnostics(vim.tbl_extend('keep', opts or {}, { bufnr = 0 })) - end, - dotfiles = function(opts) - ts.find_files(vim.tbl_extend('keep', opts or {}, { - cwd = '~/.dotfiles', - hidden = true, - })) - end, - selection = function(_) - local selected = require('fschauen.util').get_selected_text() - ts.grep_string { - prompt_title = string.format('  Grep: %s ', selected), - search = selected, - } - end, - spell_suggest = function(opts) - ts.spell_suggest(themes.get_cursor(opts)) - end, - here = function(opts) - ts.current_buffer_fuzzy_find(opts) - end, - } - - local map = function(keymap) - for mode, list in pairs(keymap) do - for _, row in ipairs(list) do - local lhs, picker, title, desc = row[1], row[2], row[3], row[4] - local rhs = function() picker { prompt_title = ' ' .. title .. ' ' } end - vim.keymap.set(mode, 'f' .. lhs, rhs, { desc = ' ' .. desc }) - end - end - end - - map { - -- ╭────╮ ╭──────╮ ╭────────────╮ ╭───────────────────╮ - -- │keys│ │picker│ │prompt title│ │mapping description│ - -- ╰────╯ ╰──────╯ ╰────────────╯ ╰───────────────────╯ - n = { - { 'a', ts.autocommands , ' Autocommands' , '[a]utocommands' }, - { 'b', ts.buffers , ' Buffers' , '[b]uffers' }, - { 'c', my.colorschemes , ' Colorschemes' , '[c]olorschemes' }, - { 'dd', my.diagnostics , '󰀪 Document Diagnostics' , '[d]iagnostics [d]ocument' }, - { 'dw', ts.diagnostics , '󰀪 Workspace Diagnostics', '[d]iagnostics [w]orkspace' }, - -- e - { 'f', ts.find_files , ' Files' , '[f]ind files' }, - { 'F', my.all_files , ' ALL files' , 'all [F]iles' }, - { 'gr', ts.live_grep , ' Live grep' , 'Live [gr]ep' }, - { 'gf', ts.git_files , ' Git files' , '[g]it [f]iles' }, - { 'gc', ts.git_commits , ' Commits' , '[g]it [c]ommits' }, - { 'h', my.here , ' Current buffer' , '[b]uffer [h]ere' }, - { 'H', ts.highlights , '󰌶 Highlights' , '[H]ighlights' }, - -- i - { 'j', ts.jumplist , ' Jumplist' , '[j]umplist' }, - { 'k', ts.keymaps , ' Keymaps' , '[k]eymaps' }, - { 'K', ts.help_tags , ' Help tags' , '[K] help/documentation' }, - { 'l', ts.loclist , ' Location list' , '[l]ocation List' }, - { 'm', ts.man_pages , ' Man pages' , '[m]an pages' }, - -- n - { 'o', ts.vim_options , ' Vim options' , 'vim [o]ptions' }, - -- p - { 'q', ts.quickfix , ' Quickfix' , '[q]uickfix' }, - { 'r', ts.lsp_references , ' References' , '[r]eferences' }, - { 'R', ts.registers , '󱓥 Registers' , '[R]registers' }, - { 's', ts.lsp_document_symbols , '󰫧 Document Symbols ' , 'lsp document [s]ymbols' }, - { 'S', ts.lsp_workspace_symbols , '󱄑 Workspace Symbols ' , 'lsp workspace [S]ymbols' }, - --'t' used in todo-comments - { 'T', ts.treesitter , ' Treesitter symbols' , '[T]reesitter Symbols' }, - -- u - -- v - { 'w', my.selection , '' --[[dynamic]] , '[w]word under cursor' }, - -- x - -- y - { 'z', my.spell_suggest , '󰓆 Spelling suggestions' , '[z] spell suggestions' }, - { '.', my.dotfiles , ' Dotfiles' , '[.]dotfiles' }, - { ':', ts.command_history , ' Command history' , '[:]command history' }, - { '?', ts.commands , ' Commands' , 'commands [?]' }, - { '/', ts.search_history , ' Search history' , '[/]search history' }, - {'', ts.resume , '󰐎 Resume' , 'Resume' }, + spell_suggest = { + theme = 'cursor', }, - v = { - { 's', my.selection , '' --[[dynamic]] , 'visual [s]election' }, - } - } - - telescope.load_extension 'fzf' - telescope.load_extension 'file_browser' - vim.keymap.set('n', 'fB', 'Telescope file_browser', { desc = ' [f]ile [B]rowser' }) - end, + }, + extensions = { + file_browser = { + theme = 'ivy' + }, + }, + }, + keys = { + { prefix .. 'a', pickers.autocommands (' Autocommands' ), { desc = ' [a]utocommands' }}, + { prefix .. 'b', pickers.buffers (' Buffers' ), { desc = ' [b]uffers' }}, + { prefix .. 'B', 'Telescope file_browser' , { desc = ' file [B]rowser' }}, + { prefix .. 'c', pickers.colorscheme (' Colorschemes' ), { desc = ' [c]olorschemes' }}, + { prefix .. 'dd', pickers.diagnostics ('󰀪 Document Diagnostics' ), { desc = ' [d]iagnostics [d]ocument' }}, + { prefix .. 'dw', pickers.diagnostics ('󰀪 Workspace Diagnostics'), { desc = ' [d]iagnostics [w]orkspace' }}, + -- 'e' + { prefix .. 'f', pickers.find_files (' Files' ), { desc = ' [f]ind files' }}, + { prefix .. 'F', pickers.all_files (' ALL files' ), { desc = ' all [F]iles' }}, + { prefix .. 'gr', pickers.live_grep (' Live grep' ), { desc = ' Live [gr]ep' }}, + { prefix .. 'gf', pickers.git_files (' Git files' ), { desc = ' [g]it [f]iles' }}, + { prefix .. 'gc', pickers.git_commits (' Commits' ), { desc = ' [g]it [c]ommits' }}, + { prefix .. 'h', pickers.here (' Current buffer' ), { desc = ' [b]uffer [h]ere' }}, + { prefix .. 'H', pickers.highlights ('󰌶 Highlights' ), { desc = ' [H]ighlights' }}, + -- 'i' + { prefix .. 'j', pickers.jumplist (' Jumplist' ), { desc = ' [j]umplist' }}, + { prefix .. 'k', pickers.keymaps (' Keymaps' ), { desc = ' [k]eymaps' }}, + { prefix .. 'K', pickers.help_tags (' Help tags' ), { desc = ' [K] help/documentation' }}, + { prefix .. 'l', pickers.loclist (' Location list' ), { desc = ' [l]ocation List' }}, + { prefix .. 'm', pickers.man_pages (' Man pages' ), { desc = ' [m]an pages' }}, + -- 'n' + { prefix .. 'o', pickers.vim_options (' Vim options' ), { desc = ' vim [o]ptions' }}, + -- 'p' + { prefix .. 'q', pickers.quickfix (' Quickfix' ), { desc = ' [q]uickfix' }}, + { prefix .. 'r', pickers.lsp_references (' References' ), { desc = ' [r]eferences' }}, + { prefix .. 'R', pickers.registers ('󱓥 Registers' ), { desc = ' [R]registers' }}, + { prefix .. 's', pickers.lsp_document_symbols ('󰫧 Document Symbols ' ), { desc = ' lsp document [s]ymbols' }}, + { prefix .. 'S', pickers.lsp_workspace_symbols ('󱄑 Workspace Symbols ' ), { desc = ' lsp workspace [S]ymbols' }}, + -- 't' used in todo-commenpickers + { prefix .. 'T', pickers.treesitter (' Treesitter symbols' ), { desc = ' [T]reesitter Symbols' }}, + -- 'u' + -- 'v' + { prefix .. 'w', pickers.selection (--[[dynamic]]) , { desc = ' [w]word under cursor' }}, + { prefix .. 'w', pickers.selection (--[[dynamic]]), mode = 'v' , { desc = ' visual [s]election' }}, + -- 'x' + -- 'y' + { prefix .. 'z', pickers.spell_suggest ('󰓆 Spelling suggestions') , { desc = ' [z] spell suggestions' }}, + { prefix .. '.', pickers.dotfiles (' Dotfiles' ) , { desc = ' [.]dotfiles' }}, + { prefix .. ':', pickers.command_history (' Command history' ) , { desc = ' [:]command history' }}, + { prefix .. '?', pickers.commands (' Commands' ) , { desc = ' commands [?]' }}, + { prefix .. '/', pickers.search_history (' Search history' ) , { desc = ' [/]search history' }}, + { prefix .. '', pickers.resume ('󰐎 Resume' ) , { desc = ' Resume ' }}, + }, + config = function(_, opts) + require('telescope').setup(opts) + require('telescope').load_extension 'fzf' + require('telescope').load_extension 'file_browser' + end } diff --git a/config/nvim/lua/fschauen/telescope.lua b/config/nvim/lua/fschauen/telescope.lua new file mode 100644 index 0000000..23a1724 --- /dev/null +++ b/config/nvim/lua/fschauen/telescope.lua @@ -0,0 +1,93 @@ +M = {} + +M.prefix = 'f' + +local builtin = function() return require('telescope.builtin') end +local actions = function() return require('telescope.actions') end +local layout = function() return require('telescope.actions.layout') end + +M.actions = { + cycle_history_next = function(...) + actions().cycle_history_next(...) + end, + cycle_history_prev = function(...) + actions().cycle_history_prev(...) + end, + preview_scrolling_down = function(...) + actions().preview_scrolling_down(...) + end, + preview_scrolling_up = function(...) + actions().preview_scrolling_up(...) + end, + cycle_layout_next = function(...) + layout().cycle_layout_next(...) + end, + toggle_mirror = function(...) + layout().toggle_mirror(...) + end, + close = function(...) + actions().close(...) + end, + delete_buffer = function(...) + actions().delete_buffer(...) + end, + smart_send_to_qflist_and_open = function(...) + actions().smart_send_to_qflist(...) + actions().open_qflist(...) + end, + smart_send_to_loclist_and_open = function(...) + actions().smart_send_to_loclist(...) + actions().open_loclist(...) + end, + smart_open_with_trouble = function(...) + local trouble = vim.F.npcall(require, 'trouble.providers.telescope') + if trouble then trouble.smart_open_with_trouble(...) end + end, +} + +local config_builtin = function(picker, opts) + return function(title) + return function() + opts = opts or {} + local args = vim.tbl_extend('keep', { prompt_title = title }, opts) + builtin()[picker](args) + end + end +end + +M.pickers = setmetatable({ + all_files = config_builtin('find_files', { + hidden = true, + no_ignore = true, + no_ignore_parent = true, + }), + colorscheme = config_builtin('colorscheme', { + enable_preview = true, + }), + diagnostics = config_builtin('diagnostics', { + bufnr = 0 + }), + dotfiles = config_builtin('find_files', { + cwd = '~/.dotfiles', + hidden = true, + }), + selection = function(_) + return function() + local text = require('fschauen.util').get_selected_text() + builtin().grep_string { + prompt_title = string.format('  Grep: %s ', text), + search = text, + } + end + end, + here = config_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 config_builtin(picker) + end +}) + +return M +