Skip to content

Commit 8ff9a0e

Browse files
author
Philipp Szechenyi
committed
move telescope related lsp functions inside the telscope plugin declaration block
1 parent 3338d39 commit 8ff9a0e

File tree

1 file changed

+38
-25
lines changed

1 file changed

+38
-25
lines changed

init.lua

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,44 @@ require('lazy').setup({
437437
vim.keymap.set('n', '<leader>s.', builtin.oldfiles, { desc = '[S]earch Recent Files ("." for repeat)' })
438438
vim.keymap.set('n', '<leader><leader>', builtin.buffers, { desc = '[ ] Find existing buffers' })
439439

440+
-- This runs on LSP attach per buffer (see main LSP attach function in 'neovim/nvim-lspconfig' config for more info,
441+
-- it is better explained there). This is a little bit redundant, but we can switch off telescope for an optional
442+
-- picker like snacks more easily when the keymaps are defined in the plugin itself.
443+
-- It sets up buffer-local keymaps, autocommands, and other LSP-related settings
444+
-- whenever an LSP client attaches to a buffer.
445+
446+
vim.api.nvim_create_autocmd('LspAttach', {
447+
group = vim.api.nvim_create_augroup('telescope-lsp-attach', { clear = true }),
448+
callback = function(event)
449+
local buf = event.buf
450+
451+
-- Find references for the word under your cursor.
452+
vim.keymap.set('n', 'grr', builtin.lsp_references, { buffer = buf, desc = '[G]oto [R]eferences' })
453+
454+
-- Jump to the implementation of the word under your cursor.
455+
-- Useful when your language has ways of declaring types without an actual implementation.
456+
vim.keymap.set('n', 'gri', builtin.lsp_implementations, { buffer = buf, desc = '[G]oto [I]mplementation' })
457+
458+
-- Jump to the definition of the word under your cursor.
459+
-- This is where a variable was first declared, or where a function is defined, etc.
460+
-- To jump back, press <C-t>.
461+
vim.keymap.set('n', 'grd', builtin.lsp_definitions, { buffer = buf, desc = '[G]oto [D]efinition' })
462+
463+
-- Fuzzy find all the symbols in your current document.
464+
-- Symbols are things like variables, functions, types, etc.
465+
vim.keymap.set('n', 'gO', builtin.lsp_document_symbols, { buffer = buf, desc = 'Open Document Symbols' })
466+
467+
-- Fuzzy find all the symbols in your current workspace.
468+
-- Similar to document symbols, except searches over your entire project.
469+
vim.keymap.set('n', 'gW', builtin.lsp_dynamic_workspace_symbols, { buffer = buf, desc = 'Open Workspace Symbols' })
470+
471+
-- Jump to the type of the word under your cursor.
472+
-- Useful when you're not sure what type a variable is and you want to see
473+
-- the definition of its *type*, not where it was *defined*.
474+
vim.keymap.set('n', 'grt', builtin.lsp_type_definitions, { buffer = buf, desc = '[G]oto [T]ype Definition' })
475+
end,
476+
})
477+
440478
-- Slightly advanced example of overriding default behavior and theme
441479
vim.keymap.set('n', '<leader>/', function()
442480
-- You can pass additional configuration to Telescope to change the theme, layout, etc.
@@ -543,35 +581,10 @@ require('lazy').setup({
543581
-- or a suggestion from your LSP for this to activate.
544582
map('gra', vim.lsp.buf.code_action, '[G]oto Code [A]ction', { 'n', 'x' })
545583

546-
-- Find references for the word under your cursor.
547-
map('grr', require('telescope.builtin').lsp_references, '[G]oto [R]eferences')
548-
549-
-- Jump to the implementation of the word under your cursor.
550-
-- Useful when your language has ways of declaring types without an actual implementation.
551-
map('gri', require('telescope.builtin').lsp_implementations, '[G]oto [I]mplementation')
552-
553-
-- Jump to the definition of the word under your cursor.
554-
-- This is where a variable was first declared, or where a function is defined, etc.
555-
-- To jump back, press <C-t>.
556-
map('grd', require('telescope.builtin').lsp_definitions, '[G]oto [D]efinition')
557-
558584
-- WARN: This is not Goto Definition, this is Goto Declaration.
559585
-- For example, in C this would take you to the header.
560586
map('grD', vim.lsp.buf.declaration, '[G]oto [D]eclaration')
561587

562-
-- Fuzzy find all the symbols in your current document.
563-
-- Symbols are things like variables, functions, types, etc.
564-
map('gO', require('telescope.builtin').lsp_document_symbols, 'Open Document Symbols')
565-
566-
-- Fuzzy find all the symbols in your current workspace.
567-
-- Similar to document symbols, except searches over your entire project.
568-
map('gW', require('telescope.builtin').lsp_dynamic_workspace_symbols, 'Open Workspace Symbols')
569-
570-
-- Jump to the type of the word under your cursor.
571-
-- Useful when you're not sure what type a variable is and you want to see
572-
-- the definition of its *type*, not where it was *defined*.
573-
map('grt', require('telescope.builtin').lsp_type_definitions, '[G]oto [T]ype Definition')
574-
575588
-- This function resolves a difference between neovim nightly (version 0.11) and stable (version 0.10)
576589
---@param client vim.lsp.Client
577590
---@param method vim.lsp.protocol.Method

0 commit comments

Comments
 (0)