vim/telescope: major refactor to enable lazy loading

This commit is contained in:
Fernando Schauenburg 2023-08-08 17:18:22 +02:00
parent dc5f34260e
commit d99de8a907
2 changed files with 199 additions and 164 deletions

View file

@ -1,3 +1,20 @@
local prefix = require('fschauen.telescope').prefix
local pickers = require('fschauen.telescope').pickers
local actions = require('fschauen.telescope').actions
local mappings = {
['<c-j>'] = actions.cycle_history_next,
['<c-k>'] = actions.cycle_history_prev,
['<s-down>'] = actions.preview_scrolling_down,
['<s-up>'] = actions.preview_scrolling_up,
['<c-y>'] = actions.cycle_layout_next,
['<c-o>'] = actions.toggle_mirror,
['<c-c>'] = actions.close,
['<c-q>'] = actions.smart_send_to_qflist_and_open,
['<c-l>'] = actions.smart_send_to_loclist_and_open,
['<c-b>'] = actions.smart_open_with_trouble
}
return { return {
'nvim-telescope/telescope.nvim', 'nvim-telescope/telescope.nvim',
@ -10,177 +27,102 @@ return {
'&& cmake --install build --prefix build' '&& cmake --install build --prefix build'
}, },
}, },
lazy = true,
cmd = 'Telescope',
opts = {
defaults = {
mappings = {
i = mappings,
n = mappings,
},
config = function() prompt_prefix = '', -- Alternatives: 
local telescope = require 'telescope' selection_caret = '', -- Alternatives:  ➔ 
local actions = require 'telescope.actions'
local actions_layout = require 'telescope.actions.layout'
local mappings = { multi_icon = '󰄬 ', -- Alternatives: 󰄬  
['<c-y>'] = actions_layout.cycle_layout_next, scroll_strategy = 'limit', -- Don't wrap around in results.
['<c-o>'] = actions_layout.toggle_mirror,
['<c-q>'] = actions.smart_send_to_qflist + actions.open_qflist,
['<c-l>'] = actions.smart_send_to_loclist + actions.open_loclist,
['<c-c>'] = actions.close,
['<s-down>'] = actions.preview_scrolling_down, dynamic_preview_title = true,
['<s-up>'] = actions.preview_scrolling_up,
['<c-j>'] = actions.cycle_history_next, layout_strategy = 'flex',
['<c-k>'] = actions.cycle_history_prev, layout_config = {
} width = 0.9,
height = 0.9,
local trouble = vim.F.npcall(require, 'trouble.providers.telescope') flex = { flip_columns = 130 },
if trouble then horizontal = { preview_width = 0.5, preview_cutoff = 130 },
mappings['<c-b>'] = trouble.open_with_trouble vertical = { preview_height = 0.5 },
end },
cycle_layout_list = { 'horizontal', 'vertical' },
telescope.setup { },
defaults = { pickers = {
-- ╭────────╮ buffers = {
-- │ Keymap │ mappings = {
-- ╰────────╯ n = {
mappings = { i = mappings, n = mappings }, x = actions.delete_buffer,
},
-- ╭────────╮
-- │ 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 },
}, },
cycle_layout_list = { 'horizontal', 'vertical' },
}, },
colorscheme = {
extensions = { theme = 'dropdown',
file_browser = { theme = 'ivy' },
}, },
} spell_suggest = {
theme = 'cursor',
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, '<leader>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' },
{'<leader>', ts.resume , '󰐎 Resume' , 'Resume' },
}, },
v = { },
{ 's', my.selection , '' --[[dynamic]] , 'visual [s]election' }, extensions = {
} file_browser = {
} theme = 'ivy'
},
telescope.load_extension 'fzf' },
telescope.load_extension 'file_browser' },
vim.keymap.set('n', '<leader>fB', '<cmd>Telescope file_browser<cr>', { desc = ' [f]ile [B]rowser' }) keys = {
end, { prefix .. 'a', pickers.autocommands (' Autocommands' ), { desc = ' [a]utocommands' }},
{ prefix .. 'b', pickers.buffers (' Buffers' ), { desc = ' [b]uffers' }},
{ prefix .. 'B', '<cmd>Telescope file_browser<cr>' , { 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 .. '<leader>', pickers.resume ('󰐎 Resume' ) , { desc = ' Resume ' }},
},
config = function(_, opts)
require('telescope').setup(opts)
require('telescope').load_extension 'fzf'
require('telescope').load_extension 'file_browser'
end
} }

View file

@ -0,0 +1,93 @@
M = {}
M.prefix = '<leader>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