1 -- Absorbed from LazyVim
2 -- TODO: review, understand and adapt
6 ---@alias lsp.Client.filter {id?: number, bufnr?: number, name?: string, method?: string, filter?:fun(client: lsp.Client):boolean}
26 -- you can specify a different filter for each filetype
37 -- "Package", -- remove package since luals uses it for control flow structures
44 ---@param opts? lsp.Client.filter
45 function M.get_clients(opts)
46 local ret = {} ---@type vim.lsp.Client[]
47 if vim.lsp.get_clients then
48 ret = vim.lsp.get_clients(opts)
50 ---@diagnostic disable-next-line: deprecated
51 ret = vim.lsp.get_active_clients(opts)
52 if opts and opts.method then
53 ---@param client vim.lsp.Client
54 ret = vim.tbl_filter(function(client)
55 return client.supports_method(opts.method, { bufnr = opts.bufnr })
59 return opts and opts.filter and vim.tbl_filter(opts.filter, ret) or ret
62 ---@param on_attach fun(client:vim.lsp.Client, buffer)
63 ---@param name? string
64 function M.on_attach(on_attach, name)
65 return vim.api.nvim_create_autocmd("LspAttach", {
66 callback = function(args)
67 local buffer = args.buf ---@type number
68 local client = vim.lsp.get_client_by_id(args.data.client_id)
69 if client and (not name or client.name == name) then
70 return on_attach(client, buffer)
77 local register_capability = vim.lsp.handlers["client/registerCapability"]
78 vim.lsp.handlers["client/registerCapability"] = function(err, res, ctx)
79 ---@diagnostic disable-next-line: no-unknown
80 local ret = register_capability(err, res, ctx)
81 local client = vim.lsp.get_client_by_id(ctx.client_id)
83 for buffer in pairs(client.attached_buffers) do
84 vim.api.nvim_exec_autocmds("User", {
85 pattern = "LspDynamicCapability",
86 data = { client_id = client.id, buffer = buffer },
94 ---@param fn fun(client:vim.lsp.Client, buffer):boolean?
95 ---@param opts? {group?: integer}
96 function M.on_dynamic_capability(fn, opts)
97 return vim.api.nvim_create_autocmd("User", {
98 pattern = "LspDynamicCapability",
99 group = opts and opts.group or nil,
100 callback = function(args)
101 local client = vim.lsp.get_client_by_id(args.data.client_id)
102 local buffer = args.data.buffer ---@type number
104 return fn(client, buffer)
110 M.action = setmetatable({}, {
111 __index = function(_, action)
113 vim.lsp.buf.code_action({