From cbcd91ee9ec646ced5f8ea506e98e7ab6b4df7a8 Mon Sep 17 00:00:00 2001 From: srdusr Date: Tue, 1 Nov 2022 22:59:40 +0200 Subject: changed document_highlight autocommand --- lua/plugins/lsp.lua | 74 +++++++++++++++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 36 deletions(-) (limited to 'lua/plugins') diff --git a/lua/plugins/lsp.lua b/lua/plugins/lsp.lua index 097699e..155143d 100644 --- a/lua/plugins/lsp.lua +++ b/lua/plugins/lsp.lua @@ -70,7 +70,9 @@ function _G.toggle_diagnostics() vim.diagnostic.enable() end end -map('n', 'm', ':call v:lua.toggle_diagnostics()') + +--map('n', 'm', ':call v:lua.toggle_diagnostics()') + --vim.g.diagnostics_active = true --function _G.toggle_diagnostics() -- if vim.g.diagnostics_active then @@ -105,6 +107,35 @@ map('n', 'm', ':call v:lua.toggle_diagnostics()') map("n", "ra", "RustHoverAction") end +-- Highlight symbol under cursor + +-- Add the following to your on_attach (this allows checking server capabilities to avoid calling invalid commands. + +if client.server_capabilities.document_highlight then + vim.cmd [[ + hi! LspReferenceRead cterm=bold ctermbg=red guibg=LightYellow + hi! LspReferenceText cterm=bold ctermbg=red guibg=LightYellow + hi! LspReferenceWrite cterm=bold ctermbg=red guibg=LightYellow + ]] + vim.api.nvim_create_augroup('lsp_document_highlight', { + clear = false + }) + vim.api.nvim_clear_autocmds({ + buffer = bufnr, + group = 'lsp_document_highlight', + }) + vim.api.nvim_create_autocmd({ 'CursorHold', 'CursorHoldI' }, { + group = 'lsp_document_highlight', + buffer = bufnr, + callback = vim.lsp.buf.document_highlight, + }) + vim.api.nvim_create_autocmd('CursorMoved', { + group = 'lsp_document_highlight', + buffer = bufnr, + callback = vim.lsp.buf.clear_references, + }) +end + --For diagnostics for specific cursor position --vim.api.nvim_create_autocmd("CursorHold", { -- buffer = bufnr, @@ -162,7 +193,7 @@ map('n', 'm', ':call v:lua.toggle_diagnostics()') -- augroup END -- ]]) -- end - +-- -- Only highlight if compatible with the language -- if client.resolved_capabilities.document_highlight then -- vim.cmd('augroup LspHighlight') @@ -172,10 +203,10 @@ map('n', 'm', ':call v:lua.toggle_diagnostics()') -- vim.cmd('augroup END') -- end --- if vim.g.logging_level == "debug" then --- local msg = string.format("Language server %s started!", client.name) --- vim.notify(msg, vim.log.levels.DEBUG, { title = "Server?" }) --- end + if vim.g.logging_level == "debug" then + local msg = string.format("Language server %s started!", client.name) + vim.notify(msg, vim.log.levels.DEBUG, { title = "Server?" }) + end -- suppress error messages from lang servers end vim.lsp.set_log_level("debug") @@ -395,7 +426,7 @@ function! ToggleDiagnosticsOpenFloat() augroup END endif endfunction -nnoremap a :call ToggleDiagnosticsOpenFloat()\|:echom "vim.diagnostic.open_float disabled . . ." +nnoremap m :call ToggleDiagnosticsOpenFloat()\|:echom "vim.diagnostic.open_float disabled . . ." ]]) --vim.lsp.handlers["textDocument/hover"] = vim.lsp.with( @@ -578,32 +609,3 @@ end -- sign define DiagnosticSignHint text= texthl=DiagnosticSignHint linehl= numhl=DiagnosticLineNrHint --]] --- Highlight symbol under cursor - --- Add the following to your on_attach (this allows checking server capabilities to avoid calling invalid commands. - ---if client.resolved_capabilities.document_highlight then --- vim.cmd [[ --- hi! LspReferenceRead cterm=bold ctermbg=red guibg=LightYellow --- hi! LspReferenceText cterm=bold ctermbg=red guibg=LightYellow --- hi! LspReferenceWrite cterm=bold ctermbg=red guibg=LightYellow --- ]] --- vim.api.nvim_create_augroup('lsp_document_highlight', { --- clear = false --- }) --- vim.api.nvim_clear_autocmds({ --- buffer = bufnr, --- group = 'lsp_document_highlight', --- }) --- vim.api.nvim_create_autocmd({ 'CursorHold', 'CursorHoldI' }, { --- group = 'lsp_document_highlight', --- buffer = bufnr, --- callback = vim.lsp.buf.document_highlight, --- }) --- vim.api.nvim_create_autocmd('CursorMoved', { --- group = 'lsp_document_highlight', --- buffer = bufnr, --- callback = vim.lsp.buf.clear_references, --- }) ---end - -- cgit v1.2.3