Skip to content

Commit cab7d98

Browse files
committed
feat!: allow lsp_handlers to be configured in setup with fine grained control
1 parent 7a172c5 commit cab7d98

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ local opts = {
5454
codelens = true, -- enable/disable codelens refresh on start
5555
diagnostics_mode = 3, -- diagnostic mode on start (0 = off, 1 = no signs/virtual text, 2 = no virtual text, 3 = off)
5656
inlay_hints = false, -- enable/disable inlay hints on start
57-
lsp_handlers = true, -- enable/disable setting of lsp_handlers
5857
semantic_tokens = true, -- enable/disable semantic token highlighting
5958
},
6059
-- Configure buffer local auto commands to add when attaching a language server
@@ -154,6 +153,11 @@ local opts = {
154153
-- set to false to disable the setup of a language server
155154
rust_analyzer = false,
156155
},
156+
-- Configure `vim.lsp.handlers`
157+
lsp_handlers = {
158+
["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, { border = "rounded", silent = true }),
159+
["textDocument/signatureHelp"] = false, -- set to false to disable any custom handlers
160+
},
157161
-- Configuration of mappings added when attaching a language server during the core `on_attach` function
158162
-- The first key into the table is the vim map mode (`:h map-modes`), and the value is a table of entries to be passed to `vim.keymap.set` (`:h vim.keymap.set`):
159163
-- - The key is the first parameter or the vim mode (only a single mode supported) and the value is a table of keymaps within that mode:

lua/astrolsp/config.lua

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
---@field codelens boolean? enable/disable codelens refresh on start (boolean; default = true)
3030
---@field diagnostics_mode integer? diagnostic mode on start (0 = off, 1 = no signs/virtual text, 2 = no virtual text, 3 = off; default = 3)
3131
---@field inlay_hints boolean? enable/disable inlay hints on start (boolean; default = false)
32-
---@field lsp_handlers boolean? enable/disable setting of lsp_handlers (boolean; default = true)
3332
---@field semantic_tokens boolean? enable/disable semantic token highlighting (boolean; default = true)
3433

3534
---@class AstroLSPFormatOnSaveOpts
@@ -102,7 +101,6 @@
102101
--- codelens = true,
103102
--- diagnostics_mode = 3,
104103
--- inlay_hints = false,
105-
--- lsp_handlers = true,
106104
--- semantic_tokens = true,
107105
---}
108106
---```
@@ -197,6 +195,21 @@
197195
---}
198196
---```
199197
---@field handlers table<string|integer,fun(server:string,opts:_.lspconfig.options)|boolean?>?
198+
---Configure global LSP handlers, set a method to `false` to use the Neovim default
199+
---Example:
200+
--
201+
---```lua
202+
---handlers = {
203+
--- -- custom function handler for pyright
204+
--- ["textDocument/hover"] = vim.lsp.with(
205+
--- vim.lsphandlers.hover, {
206+
--- border = "single",
207+
--- title = "hover",
208+
--- }
209+
--- )
210+
---}
211+
---```
212+
---@field lsp_handlers table<string,fun(err:lsp.ResponseError?,server:string,result:any,ctx:lsp.HandlerContext,config:table?)|false>|false?
200213
---Configuration of mappings added when attaching a language server during the core `on_attach` function
201214
---The first key into the table is the vim map mode (`:h map-modes`), and the value is a table of entries to be passed to `vim.keymap.set` (`:h vim.keymap.set`):
202215
--- - The key is the first parameter or the vim mode (only a single mode supported) and the value is a table of keymaps within that mode:
@@ -267,7 +280,6 @@ local M = {
267280
codelens = true,
268281
diagnostics_mode = 3,
269282
inlay_hints = false,
270-
lsp_handlers = true,
271283
semantic_tokens = true,
272284
},
273285
capabilities = {},
@@ -277,6 +289,7 @@ local M = {
277289
flags = {},
278290
formatting = { format_on_save = { enabled = true }, disabled = {} },
279291
handlers = {},
292+
lsp_handlers = {},
280293
mappings = {},
281294
servers = {},
282295
on_attach = nil,

lua/astrolsp/init.lua

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -266,17 +266,17 @@ function M.setup(opts)
266266
end
267267

268268
local progress_handler = vim.lsp.handlers["$/progress"]
269-
vim.lsp.handlers["$/progress"] = function(_, msg, info)
270-
local progress, id = M.lsp_progress, ("%s.%s"):format(info.client_id, msg.token)
271-
progress[id] = progress[id] and vim.tbl_deep_extend("force", progress[id], msg.value) or msg.value
269+
vim.lsp.handlers["$/progress"] = function(err, res, ctx)
270+
local progress, id = M.lsp_progress, ("%s.%s"):format(ctx.client_id, res.token)
271+
progress[id] = progress[id] and vim.tbl_deep_extend("force", progress[id], res.value) or res.value
272272
if progress[id].kind == "end" then
273273
vim.defer_fn(function()
274274
progress[id] = nil
275275
lsp_event "Progress"
276276
end, 100)
277277
end
278278
lsp_event "Progress"
279-
progress_handler(_, msg, info)
279+
progress_handler(err, res, ctx)
280280
end
281281

282282
local register_capability_handler = vim.lsp.handlers["client/registerCapability"]
@@ -286,10 +286,8 @@ function M.setup(opts)
286286
return ret
287287
end
288288

289-
if M.config.features.lsp_handlers then
290-
vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, { border = "rounded", silent = true })
291-
vim.lsp.handlers["textDocument/signatureHelp"] =
292-
vim.lsp.with(vim.lsp.handlers.signature_help, { border = "rounded", silent = true })
289+
for method, handler in pairs(M.config.lsp_handlers or {}) do
290+
if handler then vim.lsp.handlers[method] = handler end
293291
end
294292
end
295293

0 commit comments

Comments
 (0)