feat: make telescope lazy loaded by command and key mappings

This commit is contained in:
Zhao 2024-03-08 19:43:08 +08:00 committed by hronro
parent 66e2a5a425
commit a19a4c2c90
No known key found for this signature in database
GPG key ID: C0F9A2F31D85AB54

148
init.lua
View file

@ -296,7 +296,6 @@ require('lazy').setup({
{ -- Fuzzy Finder (files, lsp, etc)
'nvim-telescope/telescope.nvim',
event = 'VimEnter',
branch = '0.1.x',
dependencies = {
'nvim-lua/plenary.nvim',
@ -318,6 +317,117 @@ require('lazy').setup({
-- Useful for getting pretty icons, but requires a Nerd Font.
{ 'nvim-tree/nvim-web-devicons', enabled = vim.g.have_nerd_font },
},
-- Lazy load telescope when the `Telescope` command is executed.
cmd = 'Telescope',
-- And also lazy load telescope on these key mappings.
-- For more builtin function, see `:help telescope.builtin`.
keys = {
{
'<leader>sh',
function()
require('telescope.builtin').help_tags()
end,
desc = '[S]earch [H]elp',
},
{
'<leader>sk',
function()
require('telescope.builtin').keymaps()
end,
desc = '[S]earch [K]eymaps',
},
{
'<leader>sf',
function()
require('telescope.builtin').find_files()
end,
desc = '[S]earch [F]iles',
},
{
'<leader>ss',
function()
require('telescope.builtin').builtin()
end,
desc = '[S]earch [S]elect telescope.builtin',
},
{
'<leader>sw',
function()
require('telescope.builtin').grep_string()
end,
desc = '[S]earch current [W]ord',
},
{
'<leader>sg',
function()
require('telescope.builtin').live_grep()
end,
desc = '[S]earch by [G]rep',
},
{
'<leader>sd',
function()
require('telescope.builtin').diagnostics()
end,
desc = '[S]earch [D]iagnostics',
},
{
'<leader>sr',
function()
require('telescope.builtin').resume()
end,
desc = '[S]earch [R]esume',
},
{
'<leader>s.',
function()
require('telescope.builtin').oldfiles()
end,
desc = '[S]earch Recent Files ("." for repeat)',
},
{
'<leader><leader>',
function()
require('telescope.builtin').buffers()
end,
desc = '[ ] Find existing buffers',
},
-- Slightly advanced example of overriding default behavior and theme
{
'<leader>/',
function()
-- You can pass additional configuration to telescope to change theme, layout, etc.
require('telescope.builtin').current_buffer_fuzzy_find(require('telescope.themes').get_dropdown {
winblend = 10,
previewer = false,
})
end,
desc = '[/] Fuzzily search in current buffer',
},
-- Also possible to pass additional configuration options.
-- See `:help telescope.builtin.live_grep()` for information about particular keys
{
'<leader>s/',
function()
require('telescope.builtin').live_grep {
grep_open_files = true,
prompt_title = 'Live Grep in Open Files',
}
end,
desc = '[S]earch [/] in Open Files',
},
-- Shortcut for searching your neovim configuration files
{
'<leader>sn',
function()
require('telescope.builtin').find_files { cwd = vim.fn.stdpath 'config' }
end,
desc = '[S]earch [N]eovim files',
},
},
config = function()
-- Telescope is a fuzzy finder that comes with a lot of different things that
-- it can fuzzy find! It's more than just a "file finder", it can search
@ -360,42 +470,6 @@ require('lazy').setup({
-- Enable telescope extensions, if they are installed
pcall(require('telescope').load_extension, 'fzf')
pcall(require('telescope').load_extension, 'ui-select')
-- See `:help telescope.builtin`
local builtin = require 'telescope.builtin'
vim.keymap.set('n', '<leader>sh', builtin.help_tags, { desc = '[S]earch [H]elp' })
vim.keymap.set('n', '<leader>sk', builtin.keymaps, { desc = '[S]earch [K]eymaps' })
vim.keymap.set('n', '<leader>sf', builtin.find_files, { desc = '[S]earch [F]iles' })
vim.keymap.set('n', '<leader>ss', builtin.builtin, { desc = '[S]earch [S]elect Telescope' })
vim.keymap.set('n', '<leader>sw', builtin.grep_string, { desc = '[S]earch current [W]ord' })
vim.keymap.set('n', '<leader>sg', builtin.live_grep, { desc = '[S]earch by [G]rep' })
vim.keymap.set('n', '<leader>sd', builtin.diagnostics, { desc = '[S]earch [D]iagnostics' })
vim.keymap.set('n', '<leader>sr', builtin.resume, { desc = '[S]earch [R]esume' })
vim.keymap.set('n', '<leader>s.', builtin.oldfiles, { desc = '[S]earch Recent Files ("." for repeat)' })
vim.keymap.set('n', '<leader><leader>', builtin.buffers, { desc = '[ ] Find existing buffers' })
-- Slightly advanced example of overriding default behavior and theme
vim.keymap.set('n', '<leader>/', function()
-- You can pass additional configuration to telescope to change theme, layout, etc.
builtin.current_buffer_fuzzy_find(require('telescope.themes').get_dropdown {
winblend = 10,
previewer = false,
})
end, { desc = '[/] Fuzzily search in current buffer' })
-- Also possible to pass additional configuration options.
-- See `:help telescope.builtin.live_grep()` for information about particular keys
vim.keymap.set('n', '<leader>s/', function()
builtin.live_grep {
grep_open_files = true,
prompt_title = 'Live Grep in Open Files',
}
end, { desc = '[S]earch [/] in Open Files' })
-- Shortcut for searching your neovim configuration files
vim.keymap.set('n', '<leader>sn', function()
builtin.find_files { cwd = vim.fn.stdpath 'config' }
end, { desc = '[S]earch [N]eovim files' })
end,
},