@@ -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