From 0ef1cba81f6e5053b9870db60d3ff09465945ecc Mon Sep 17 00:00:00 2001 From: Fernando Schauenburg Date: Sat, 12 Aug 2023 02:35:21 +0200 Subject: [PATCH] vim/telescope: simplify creation of custom mappings --- .../nvim/lua/fschauen/plugins/telescope.lua | 113 +++++++++--------- config/nvim/lua/fschauen/telescope.lua | 41 ------- 2 files changed, 59 insertions(+), 95 deletions(-) diff --git a/config/nvim/lua/fschauen/plugins/telescope.lua b/config/nvim/lua/fschauen/plugins/telescope.lua index 4a52cf6..573b16a 100644 --- a/config/nvim/lua/fschauen/plugins/telescope.lua +++ b/config/nvim/lua/fschauen/plugins/telescope.lua @@ -1,18 +1,3 @@ -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', @@ -24,55 +9,75 @@ return { }, cmd = 'Telescope', keys = require('fschauen.keymap').telescope, - opts = { - defaults = { - mappings = { - i = mappings, - n = mappings, - }, + opts = function() + local actions = require('telescope.actions') + local layout = require('telescope.actions.layout') + local trouble = vim.F.npcall(require, 'trouble.providers.telescope') or {} - prompt_prefix = '  ', -- Alternatives:  ❯ - selection_caret = ' ', -- Alternatives:  ➔  + 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, + } - multi_icon = '󰄬 ', -- Alternatives: 󰄬   - 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 = { + return { + defaults = { mappings = { - n = { - x = actions.delete_buffer, + i = mappings, + n = mappings, + }, + + prompt_prefix = '  ', -- Alternatives:  ❯ + selection_caret = ' ', -- Alternatives:  ➔  + + multi_icon = '󰄬 ', -- Alternatives: 󰄬   + 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', + }, }, - colorscheme = { - theme = 'dropdown', + extensions = { + file_browser = { + theme = 'ivy' + }, }, - spell_suggest = { - theme = 'cursor', - }, - }, - extensions = { - file_browser = { - theme = 'ivy' - }, - }, - }, + } + end, config = function(_, opts) require('telescope').setup(opts) 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 } ), diff --git a/config/nvim/lua/fschauen/telescope.lua b/config/nvim/lua/fschauen/telescope.lua index 625dd0b..96436c9 100644 --- a/config/nvim/lua/fschauen/telescope.lua +++ b/config/nvim/lua/fschauen/telescope.lua @@ -1,47 +1,6 @@ M = {} 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)