Skip to content

Commit

Permalink
feat(lsp): cleanup autocmds on lsp detach
Browse files Browse the repository at this point in the history
  • Loading branch information
csvenke committed Nov 10, 2024
1 parent 5ecde54 commit 4603aaf
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/config/plugins/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,28 @@ local function default_on_attach(client, buffer)
map("<leader>cr", vim.lsp.buf.rename, "[c]ode [r]ename")
map("<leader>cd", vim.diagnostic.open_float, "[c]ode [d]iagnostic")

if client and client.server_capabilities.documentHighlightProvider then
if client and client.supports_method(vim.lsp.protocol.Methods.textDocument_documentHighlight) then
local highlight_augroup = vim.api.nvim_create_augroup("lsp-highlight", { clear = false })

vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, {
buffer = buffer,
group = highlight_augroup,
callback = vim.lsp.buf.document_highlight,
})

vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
buffer = buffer,
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(event)
vim.lsp.buf.clear_references()
vim.api.nvim_clear_autocmds({ group = "lsp-highlight", buffer = event.buf })
end,
})
end
end

Expand Down

0 comments on commit 4603aaf

Please sign in to comment.