Skip to content

Commit a13b926

Browse files
committed
feat: add support for dynamic capabilities
1 parent 5c0e64f commit a13b926

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

lua/astrolsp/init.lua

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,11 @@ function M.lsp_setup(server)
7878
end
7979
end
8080

81-
--- The `on_attach` function used by AstroNvim
82-
---@param client table The LSP client details when attaching
83-
---@param bufnr integer The buffer that the LSP client is attaching to
84-
M.on_attach = function(client, bufnr)
81+
--- Helper function that does the actual configuring of the language server configure_environment
82+
--- Useful when dynamically refreshing the environment when capabilities are registered dynamically
83+
---@param client lsp.Client
84+
---@param bufnr integer
85+
local configure_environment = function(client, bufnr)
8586
if client.supports_method "textDocument/codeLens" and M.config.features.codelens then
8687
vim.lsp.codelens.refresh { bufnr = bufnr }
8788
end
@@ -108,7 +109,7 @@ M.on_attach = function(client, bufnr)
108109

109110
if client.supports_method "textDocument/semanticTokens/full" and vim.lsp.semantic_tokens then
110111
if M.config.features.semantic_tokens then
111-
vim.b[bufnr].semantic_tokens = true
112+
if vim.b[bufnr].semantic_tokens == nil then vim.b[bufnr].semantic_tokens = true end
112113
else
113114
client.server_capabilities.semanticTokensProvider = nil
114115
end
@@ -184,6 +185,13 @@ M.on_attach = function(client, bufnr)
184185
end
185186
end
186187
end
188+
end
189+
190+
--- The `on_attach` function used by AstroNvim
191+
---@param client lsp.Client The LSP client details when attaching
192+
---@param bufnr integer The buffer that the LSP client is attaching to
193+
M.on_attach = function(client, bufnr)
194+
configure_environment(client, bufnr)
187195

188196
for id, _ in pairs(M.lsp_progress) do -- clear lingering progress messages
189197
-- TODO: remove check after dropping support for Neovim v0.9
@@ -257,7 +265,7 @@ function M.setup(opts)
257265
and not (vim.tbl_contains(disabled, client.name) or (type(filter) == "function" and not filter(client)))
258266
end
259267

260-
local orig_handler = vim.lsp.handlers["$/progress"]
268+
local progress_handler = vim.lsp.handlers["$/progress"]
261269
vim.lsp.handlers["$/progress"] = function(_, msg, info)
262270
local progress, id = M.lsp_progress, ("%s.%s"):format(info.client_id, msg.token)
263271
progress[id] = progress[id] and vim.tbl_deep_extend("force", progress[id], msg.value) or msg.value
@@ -268,7 +276,14 @@ function M.setup(opts)
268276
end, 100)
269277
end
270278
lsp_event "Progress"
271-
orig_handler(_, msg, info)
279+
progress_handler(_, msg, info)
280+
end
281+
282+
local register_capability_handler = vim.lsp.handlers["client/registerCapability"]
283+
vim.lsp.handlers["client/registerCapability"] = function(err, res, ctx)
284+
local ret = register_capability_handler(err, res, ctx)
285+
configure_environment(assert(vim.lsp.get_client_by_id(ctx.client_id)), vim.api.nvim_get_current_buf())
286+
return ret
272287
end
273288

274289
if M.config.features.lsp_handlers then

0 commit comments

Comments
 (0)