mirror of
https://github.com/LazyVim/LazyVim.git
synced 2025-06-28 19:50:09 +02:00
refactor(lsp): prevent autocmd leaks
This commit is contained in:
parent
69751cf417
commit
ac092289f5
1 changed files with 22 additions and 9 deletions
|
@ -51,21 +51,24 @@ function M.setup_dynamic_capability()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param fn fun(client:vim.lsp.Client, buffer)
|
---@param fn fun(client:vim.lsp.Client, buffer):boolean?
|
||||||
---@param buf? number
|
---@param opts? {group?: integer}
|
||||||
function M.on_dynamic_capability(fn, buf)
|
function M.on_dynamic_capability(fn, opts)
|
||||||
vim.api.nvim_create_autocmd("User", {
|
return vim.api.nvim_create_autocmd("User", {
|
||||||
pattern = "LspDynamicCapability",
|
pattern = "LspDynamicCapability",
|
||||||
|
group = opts and opts.group or nil,
|
||||||
callback = function(args)
|
callback = function(args)
|
||||||
local client = vim.lsp.get_client_by_id(args.data.client_id)
|
local client = vim.lsp.get_client_by_id(args.data.client_id)
|
||||||
local buffer = args.data.buffer ---@type number
|
local buffer = args.data.buffer ---@type number
|
||||||
if client and (buf == nil or buf == buffer) then
|
if client then
|
||||||
return fn(client, buffer)
|
return fn(client, buffer)
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
M._on_supports_method_id = 0
|
||||||
|
|
||||||
---@param method string
|
---@param method string
|
||||||
---@param fn fun(client:vim.lsp.Client, buffer)
|
---@param fn fun(client:vim.lsp.Client, buffer)
|
||||||
function M.on_supports_method(method, fn)
|
function M.on_supports_method(method, fn)
|
||||||
|
@ -73,12 +76,22 @@ function M.on_supports_method(method, fn)
|
||||||
if client.supports_method(method, { bufnr = buffer }) then
|
if client.supports_method(method, { bufnr = buffer }) then
|
||||||
fn(client, buffer)
|
fn(client, buffer)
|
||||||
else
|
else
|
||||||
M.on_dynamic_capability(function()
|
M._on_supports_method_id = M._on_supports_method_id + 1
|
||||||
if client.supports_method(method, { bufnr = buffer }) then
|
local id = M._on_supports_method_id
|
||||||
|
local group = vim.api.nvim_create_augroup("on_supports_method_" .. id, { clear = true })
|
||||||
|
M.on_dynamic_capability(function(c, b)
|
||||||
|
if c == client and b == buffer and client.supports_method(method, { bufnr = buffer }) then
|
||||||
fn(client, buffer)
|
fn(client, buffer)
|
||||||
return false
|
pcall(vim.api.nvim_del_augroup_by_id, group)
|
||||||
end
|
end
|
||||||
end, buffer)
|
end, { group = group })
|
||||||
|
vim.api.nvim_create_autocmd({ "LspDetach", "BufDelete" }, {
|
||||||
|
group = group,
|
||||||
|
buffer = buffer,
|
||||||
|
callback = function()
|
||||||
|
pcall(vim.api.nvim_del_augroup_by_id, group)
|
||||||
|
end,
|
||||||
|
})
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue