fix(lsp): guard dynamic capability attachment #38382

Problem:
The capability attach path for client/registerCapability can initialize a capability even when the capability was only registered in specific buffers

Solution:
Check supports_method() before attaching capabilities from the dynamic registration handler so unsupported registrations are ignored.

AI-assisted: OpenCode
This commit is contained in:
Tristan Knight
2026-03-20 08:48:19 +00:00
committed by GitHub
parent 24714c0d43
commit a8ffa6e323

View File

@@ -157,7 +157,10 @@ RSC['client/registerCapability'] = function(_, params, ctx)
for _, Cap in pairs(vim.lsp._capability.all) do
if reg.method == Cap.method then
for bufnr in pairs(client.attached_buffers) do
if vim.lsp._capability.is_enabled(Cap.name, { bufnr = bufnr, client_id = client.id }) then
if
client:supports_method(Cap.method, bufnr)
and vim.lsp._capability.is_enabled(Cap.name, { bufnr = bufnr, client_id = client.id })
then
local capability = Cap.active[bufnr] or Cap:new(bufnr)
if not capability.client_state[client.id] then
capability:on_attach(client.id)