]> git.rmz.io Git - dotfiles.git/blob - nvim/lua/rmz/util/lsp.lua
weechat: disable partial auto-complete
[dotfiles.git] / nvim / lua / rmz / util / lsp.lua
1 -- Absorbed from LazyVim
2 -- TODO: review, understand and adapt
3 ---@class rmz.util.lsp
4 local M = {}
5
6 ---@alias lsp.Client.filter {id?: number, bufnr?: number, name?: string, method?: string, filter?:fun(client: lsp.Client):boolean}
7
8 M.kind_filter = {
9 default = {
10 "Class",
11 "Constructor",
12 "Enum",
13 "Field",
14 "Function",
15 "Interface",
16 "Method",
17 "Module",
18 "Namespace",
19 "Package",
20 "Property",
21 "Struct",
22 "Trait",
23 },
24 markdown = false,
25 help = false,
26 -- you can specify a different filter for each filetype
27 lua = {
28 "Class",
29 "Constructor",
30 "Enum",
31 "Field",
32 "Function",
33 "Interface",
34 "Method",
35 "Module",
36 "Namespace",
37 -- "Package", -- remove package since luals uses it for control flow structures
38 "Property",
39 "Struct",
40 "Trait",
41 },
42 }
43
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)
49 else
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 })
56 end, ret)
57 end
58 end
59 return opts and opts.filter and vim.tbl_filter(opts.filter, ret) or ret
60 end
61
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)
71 end
72 end,
73 })
74 end
75
76 function M.setup()
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)
82 if client then
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 },
87 })
88 end
89 end
90 return ret
91 end
92 end
93
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
103 if client then
104 return fn(client, buffer)
105 end
106 end,
107 })
108 end
109
110 M.action = setmetatable({}, {
111 __index = function(_, action)
112 return function()
113 vim.lsp.buf.code_action({
114 apply = true,
115 context = {
116 only = { action },
117 diagnostics = {},
118 },
119 })
120 end
121 end,
122 })
123
124 return M