fix(lsp): do not assume client capability exists in watchfiles check (#24558)

Backports #24550.

Adjusts test to expect that didChangeWatchedFiles is *not* registered if
client `capabilities` is nil (as it's not enabled by default for v0.9).
This commit is contained in:
Sean Dewar
2023-08-04 10:16:54 +01:00
committed by GitHub
parent 5e6c8b3385
commit df63474930
4 changed files with 71 additions and 49 deletions

View File

@@ -933,8 +933,7 @@ end
--- |vim.lsp.protocol.make_client_capabilities()|, passed to the language
--- server on initialization. Hint: use make_client_capabilities() and modify
--- its result.
--- - Note: To send an empty dictionary use
--- `{[vim.type_idx]=vim.types.dictionary}`, else it will be encoded as an
--- - Note: To send an empty dictionary use |vim.empty_dict()|, else it will be encoded as an
--- array.
---
--- - handlers: Map of language server method names to |lsp-handler|

View File

@@ -193,12 +193,15 @@ local to_lsp_change_type = {
function M.register(reg, ctx)
local client_id = ctx.client_id
local client = vim.lsp.get_client_by_id(client_id)
if
-- Ill-behaved servers may not honor the client capability and try to register
-- anyway, so ignore requests when the user has opted out of the feature.
not client.config.capabilities.workspace.didChangeWatchedFiles.dynamicRegistration
or not client.workspace_folders
then
-- Ill-behaved servers may not honor the client capability and try to register
-- anyway, so ignore requests when the user has opted out of the feature.
local has_capability = vim.tbl_get(
client.config.capabilities or {},
'workspace',
'didChangeWatchedFiles',
'dynamicRegistration'
)
if not has_capability or not client.workspace_folders then
return
end
local watch_regs = {}