Skip to content

Commit 7b0ec1e

Browse files
committed
CLIENT_CACHE
1 parent 453e9cf commit 7b0ec1e

2 files changed

Lines changed: 63 additions & 6 deletions

File tree

lua/kide/core/keybindings.lua

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,6 @@ M.maplsp = function(client, buffer, null_ls)
229229
end, opt)
230230

231231
vim.api.nvim_buf_set_keymap(buffer, "n", "<leader>fo", "<cmd>Telescope lsp_document_symbols<CR>", opt)
232-
-- >= 0.8.x
233-
if client.server_capabilities.documentHighlightProvider then
234-
vim.cmd(string.format("au CursorHold <buffer=%d> lua vim.lsp.buf.document_highlight()", buffer))
235-
vim.cmd(string.format("au CursorHoldI <buffer=%d> lua vim.lsp.buf.document_highlight()", buffer))
236-
vim.cmd(string.format("au CursorMoved <buffer=%d> lua vim.lsp.buf.clear_references()", buffer))
237-
end
238232
vim.api.nvim_buf_set_keymap(buffer, "n", "<leader>cr", "<Cmd>lua vim.lsp.codelens.refresh()<CR>", opt)
239233
vim.api.nvim_buf_set_keymap(buffer, "n", "<leader>ce", "<Cmd>lua vim.lsp.codelens.run()<CR>", opt)
240234
end

lua/kide/lsp/init.lua

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,66 @@ vim.api.nvim_create_autocmd("LspAttach", {
123123
end
124124
end,
125125
})
126+
127+
local CLIENT_CACHE = {}
128+
local function clientCache(client_id)
129+
if not CLIENT_CACHE[client_id] then
130+
CLIENT_CACHE[client_id] = {
131+
CursorHold = {},
132+
CursorHoldI = {},
133+
CursorMoved = {},
134+
}
135+
end
136+
return CLIENT_CACHE[client_id]
137+
end
138+
vim.api.nvim_create_autocmd("LspAttach", {
139+
callback = function(args)
140+
local bufnr = args.buf
141+
local client = vim.lsp.get_client_by_id(args.data.client_id)
142+
143+
if client.server_capabilities.documentHighlightProvider then
144+
if not clientCache(args.data.client_id).CursorHold[bufnr] then
145+
clientCache(args.data.client_id).CursorHold[bufnr] = vim.api.nvim_create_autocmd("CursorHold", {
146+
buffer = bufnr,
147+
callback = function()
148+
vim.lsp.buf.document_highlight()
149+
end,
150+
})
151+
end
152+
if not clientCache(args.data.client_id).CursorHoldI[bufnr] then
153+
clientCache(args.data.client_id).CursorHoldI[bufnr] = vim.api.nvim_create_autocmd("CursorHoldI", {
154+
buffer = bufnr,
155+
callback = function()
156+
vim.lsp.buf.document_highlight()
157+
end,
158+
})
159+
end
160+
if not clientCache(args.data.client_id).CursorMoved[bufnr] then
161+
clientCache(args.data.client_id).CursorMoved[bufnr] = vim.api.nvim_create_autocmd("CursorMoved", {
162+
buffer = bufnr,
163+
callback = function()
164+
vim.lsp.buf.clear_references()
165+
end,
166+
})
167+
end
168+
end
169+
end,
170+
})
171+
vim.api.nvim_create_autocmd("LspDetach", {
172+
callback = function(args)
173+
local bufnr = args.buf
174+
-- local client = vim.lsp.get_client_by_id(args.data.client_id)
175+
if clientCache(args.data.client_id).CursorHold[bufnr] then
176+
vim.api.nvim_del_autocmd(clientCache(args.data.client_id).CursorHold[bufnr])
177+
clientCache(args.data.client_id).CursorHold[bufnr] = nil
178+
end
179+
if clientCache(args.data.client_id).CursorHoldI[bufnr] then
180+
vim.api.nvim_del_autocmd(clientCache(args.data.client_id).CursorHoldI[bufnr])
181+
clientCache(args.data.client_id).CursorHoldI[bufnr] = nil
182+
end
183+
if clientCache(args.data.client_id).CursorMoved[bufnr] then
184+
vim.api.nvim_del_autocmd(clientCache(args.data.client_id).CursorMoved[bufnr])
185+
clientCache(args.data.client_id).CursorMoved[bufnr] = nil
186+
end
187+
end,
188+
})

0 commit comments

Comments
 (0)