From 953fc404fc4e75dfbec40f7b8d60fc4a0bba8e82 Mon Sep 17 00:00:00 2001 From: sommerfeld Date: Wed, 12 Jun 2024 18:19:27 +0100 Subject: [nvim] Add LSP current word highlighting --- home/.config/nvim/lua/custom/plugins/lsp.lua | 39 ++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'home/.config/nvim') diff --git a/home/.config/nvim/lua/custom/plugins/lsp.lua b/home/.config/nvim/lua/custom/plugins/lsp.lua index 6bd73d7..ad7d43d 100644 --- a/home/.config/nvim/lua/custom/plugins/lsp.lua +++ b/home/.config/nvim/lua/custom/plugins/lsp.lua @@ -119,6 +119,45 @@ return { map.n("gR", vim.lsp.buf.rename, { buffer = bufnr }) map.n("ga", vim.lsp.buf.code_action, { buffer = bufnr }) map.v("ga", vim.lsp.buf.code_action, { buffer = bufnr }) + + -- The following two autocommands are used to highlight references of the + -- word under your cursor when your cursor rests there for a little while. + -- See `:help CursorHold` for information about when this is executed + -- + -- When you move your cursor, the highlights will be cleared (the second autocommand). + if client.server_capabilities.documentHighlightProvider then + local highlight_augroup = vim.api.nvim_create_augroup('lsp-highlight', { clear = false }) + vim.api.nvim_create_autocmd({ 'CursorHold', 'CursorHoldI' }, { + buffer = bufnr, + group = highlight_augroup, + callback = vim.lsp.buf.document_highlight, + }) + + vim.api.nvim_create_autocmd({ 'CursorMoved', 'CursorMovedI' }, { + buffer = bufnr, + group = highlight_augroup, + callback = vim.lsp.buf.clear_references, + }) + + vim.api.nvim_create_autocmd('LspDetach', { + group = vim.api.nvim_create_augroup('lsp-detach', { clear = true }), + callback = function(event2) + vim.lsp.buf.clear_references() + vim.api.nvim_clear_autocmds { group = 'lsp-highlight', buffer = event2.buf } + end, + }) + end + + -- The following autocommand is used to enable inlay hints in your + -- code, if the language server you are using supports them + -- + -- This may be unwanted, since they displace some of your code + if client.server_capabilities.inlayHintProvider and vim.lsp.inlay_hint then + map.n('th', function() + vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled({})) + vim.lsp.inlay_hint.enable() + end, { buffer = bufnr }) + end end, }) end, -- cgit v1.2.3-70-g09d2