mirror of
https://github.com/neovim/neovim.git
synced 2026-08-29 02:21:51 +00:00
refactor(lsp): centralize provider capability resolution #37221
- Refactor LSP client to use unified provider-based capability lookup for diagnostics and other features. - Introduce `_provider_value_get` to abstract capability retrieval, supporting both static and dynamic registrations. - Update diagnostic handling and protocol mappings to leverage provider-centric logic.
This commit is contained in:
@@ -615,19 +615,17 @@ end
|
||||
function Client:_process_static_registrations()
|
||||
local static_registrations = {} ---@type lsp.Registration[]
|
||||
|
||||
for method, capability in pairs(lsp.protocol._request_name_to_server_capability) do
|
||||
for method in pairs(lsp.protocol._method_supports_static_registration) do
|
||||
local capability = lsp.protocol._request_name_to_server_capability[method]
|
||||
if
|
||||
vim.tbl_get(self.server_capabilities, unpack(capability), 'id')
|
||||
--- @cast method vim.lsp.protocol.Method
|
||||
vim.tbl_get(self.server_capabilities, capability[1], 'id')
|
||||
and self:_supports_registration(method)
|
||||
then
|
||||
local cap = vim.tbl_get(self.server_capabilities, unpack(capability))
|
||||
local cap = vim.tbl_get(self.server_capabilities, capability[1])
|
||||
static_registrations[#static_registrations + 1] = {
|
||||
id = cap.id,
|
||||
method = method,
|
||||
registerOptions = {
|
||||
documentSelector = cap.documentSelector, ---@type lsp.DocumentSelector|lsp.null
|
||||
},
|
||||
registerOptions = cap or {},
|
||||
}
|
||||
end
|
||||
end
|
||||
@@ -936,9 +934,12 @@ end
|
||||
--- Get options for a method that is registered dynamically.
|
||||
--- @param method vim.lsp.protocol.Method | vim.lsp.protocol.Method.Registration
|
||||
function Client:_supports_registration(method)
|
||||
local capability_path = lsp.protocol._request_name_to_client_capability[method] or {}
|
||||
-- dynamicRegistration is at the second level, even in deeply nested capabilities
|
||||
local capability = vim.tbl_get(self.capabilities, capability_path[1], capability_path[2])
|
||||
if lsp.protocol._methods_with_no_registration_options[method] then
|
||||
return true
|
||||
end
|
||||
local provider = self:_registration_provider(method)
|
||||
local capability_path = lsp.protocol._provider_to_client_registration[provider]
|
||||
local capability = vim.tbl_get(self.capabilities, unpack(capability_path))
|
||||
return type(capability) == 'table' and capability.dynamicRegistration
|
||||
end
|
||||
|
||||
@@ -946,7 +947,7 @@ end
|
||||
--- @param method vim.lsp.protocol.Method | vim.lsp.protocol.Method.Registration
|
||||
function Client:_registration_provider(method)
|
||||
local capability_path = lsp.protocol._request_name_to_server_capability[method]
|
||||
return capability_path and capability_path[1] or method
|
||||
return capability_path and capability_path[1]
|
||||
end
|
||||
|
||||
--- @private
|
||||
@@ -1205,7 +1206,7 @@ function Client:supports_method(method, bufnr)
|
||||
|
||||
local provider = self:_registration_provider(method)
|
||||
local regs = self:_get_registrations(provider, bufnr)
|
||||
if lsp.protocol._request_name_allows_registration[method] and not regs then
|
||||
if lsp.protocol._method_supports_dynamic_registration[method] and not regs then
|
||||
return false
|
||||
end
|
||||
if regs then
|
||||
@@ -1214,6 +1215,9 @@ function Client:supports_method(method, bufnr)
|
||||
if vim.tbl_get(reg, 'registerOptions', unpack(required_capability, 2)) then
|
||||
return self:_supports_registration(reg.method)
|
||||
end
|
||||
if lsp.protocol._methods_with_no_registration_options[method] then
|
||||
return true
|
||||
end
|
||||
else
|
||||
return self:_supports_registration(reg.method)
|
||||
end
|
||||
@@ -1226,6 +1230,40 @@ function Client:supports_method(method, bufnr)
|
||||
return required_capability == nil
|
||||
end
|
||||
|
||||
--- Retrieves all capability values for a given LSP method, handling both static and dynamic registrations.
|
||||
--- This function abstracts over differences between capabilities declared in `server_capabilities`
|
||||
--- and those registered dynamically at runtime, returning all matching capability values.
|
||||
--- It also handles cases where the registration method differs from the calling method by abstracting to the Provider.
|
||||
--- For example, `workspace/diagnostic` uses capabilities registered under `textDocument/diagnostic`.
|
||||
--- This is useful for features like diagnostics and formatting, where servers may register multiple providers
|
||||
--- with different options (such as specific filetypes or document selectors).
|
||||
--- @param method vim.lsp.protocol.Method.ClientToServer | vim.lsp.protocol.Method.Registration LSP method name
|
||||
--- @param ... any Additional keys to index into the capability
|
||||
--- @return lsp.LSPAny[] # The capability value if it exists, empty table if not found
|
||||
function Client:_provider_value_get(method, ...)
|
||||
local matched_regs = {} --- @type any[]
|
||||
local provider = self:_registration_provider(method)
|
||||
local dynamic_regs = self:_get_registrations(provider)
|
||||
if not provider then
|
||||
return matched_regs
|
||||
elseif not dynamic_regs then
|
||||
-- First check static capabilities
|
||||
local static_reg = vim.tbl_get(self.server_capabilities, provider)
|
||||
if static_reg then
|
||||
matched_regs[1] = vim.tbl_get(static_reg, ...) or vim.NIL
|
||||
end
|
||||
else
|
||||
local required_capability = lsp.protocol._request_name_to_server_capability[method]
|
||||
for _, reg in ipairs(dynamic_regs) do
|
||||
if vim.tbl_get(reg, 'registerOptions', unpack(required_capability, 2)) then
|
||||
matched_regs[#matched_regs + 1] = vim.tbl_get(reg, 'registerOptions', ...) or vim.NIL
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return matched_regs
|
||||
end
|
||||
|
||||
--- @private
|
||||
--- Handles a notification sent by an LSP server by invoking the
|
||||
--- corresponding handler.
|
||||
|
||||
@@ -193,14 +193,8 @@ function M.get_namespace(client_id, is_pull)
|
||||
|
||||
local client = lsp.get_client_by_id(client_id)
|
||||
if is_pull then
|
||||
local server_id =
|
||||
vim.tbl_get((client or {}).server_capabilities or {}, 'diagnosticProvider', 'identifier')
|
||||
local key = ('%d:%s'):format(client_id, server_id or 'nil')
|
||||
local name = ('nvim.lsp.%s.%d.%s'):format(
|
||||
client and client.name or 'unknown',
|
||||
client_id,
|
||||
server_id or 'nil'
|
||||
)
|
||||
local key = ('%d'):format(client_id)
|
||||
local name = ('nvim.lsp.%s.%d'):format(client and client.name or 'unknown', client_id)
|
||||
local ns = client_pull_namespaces[key]
|
||||
if not ns then
|
||||
ns = api.nvim_create_namespace(name)
|
||||
@@ -394,10 +388,7 @@ function M.on_refresh(err, _, ctx)
|
||||
if client == nil then
|
||||
return vim.NIL
|
||||
end
|
||||
if
|
||||
client.server_capabilities.diagnosticProvider
|
||||
and client.server_capabilities.diagnosticProvider.workspaceDiagnostics
|
||||
then
|
||||
if client:supports_method('workspace/diagnostic') then
|
||||
M._workspace_diagnostics({ client_id = ctx.client_id })
|
||||
else
|
||||
for bufnr in pairs(client.attached_buffers or {}) do
|
||||
@@ -532,13 +523,16 @@ function M._workspace_diagnostics(opts)
|
||||
end
|
||||
|
||||
for _, client in ipairs(clients) do
|
||||
--- @type lsp.WorkspaceDiagnosticParams
|
||||
local params = {
|
||||
identifier = vim.tbl_get(client, 'server_capabilities', 'diagnosticProvider', 'identifier'),
|
||||
previousResultIds = previous_result_ids(client.id),
|
||||
}
|
||||
local identifiers = client:_provider_value_get('workspace/diagnostic', 'identifier')
|
||||
for _, id in ipairs(identifiers) do
|
||||
--- @type lsp.WorkspaceDiagnosticParams
|
||||
local params = {
|
||||
identifier = type(id) == 'string' and id or nil,
|
||||
previousResultIds = previous_result_ids(client.id),
|
||||
}
|
||||
|
||||
client:request('workspace/diagnostic', params, handler)
|
||||
client:request('workspace/diagnostic', params, handler)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -613,6 +613,15 @@ function protocol.make_client_capabilities()
|
||||
diagnostics = {
|
||||
refreshSupport = true,
|
||||
},
|
||||
fileOperations = {
|
||||
dynamicRegistration = false,
|
||||
didCreate = false,
|
||||
willCreate = false,
|
||||
didRename = false,
|
||||
willRename = false,
|
||||
didDelete = false,
|
||||
willDelete = false,
|
||||
},
|
||||
},
|
||||
experimental = nil,
|
||||
window = {
|
||||
@@ -1160,79 +1169,49 @@ protocol.Methods = {
|
||||
-- stylua: ignore start
|
||||
-- Generated by gen_lsp.lua, keep at end of file.
|
||||
--- Maps method names to the required client capability
|
||||
protocol._request_name_to_client_capability = {
|
||||
['codeAction/resolve'] = { 'textDocument', 'codeAction', 'resolveSupport' },
|
||||
['codeLens/resolve'] = { 'textDocument', 'codeLens', 'resolveSupport' },
|
||||
['completionItem/resolve'] = { 'textDocument', 'completion', 'completionItem', 'resolveSupport' },
|
||||
['documentLink/resolve'] = { 'textDocument', 'documentLink' },
|
||||
['inlayHint/resolve'] = { 'textDocument', 'inlayHint', 'resolveSupport' },
|
||||
['textDocument/codeAction'] = { 'textDocument', 'codeAction' },
|
||||
['textDocument/codeLens'] = { 'textDocument', 'codeLens' },
|
||||
['textDocument/colorPresentation'] = { 'textDocument', 'colorProvider' },
|
||||
['textDocument/completion'] = { 'textDocument', 'completion' },
|
||||
['textDocument/declaration'] = { 'textDocument', 'declaration' },
|
||||
['textDocument/definition'] = { 'textDocument', 'definition' },
|
||||
['textDocument/diagnostic'] = { 'textDocument', 'diagnostic' },
|
||||
['textDocument/didChange'] = { 'textDocument', 'synchronization' },
|
||||
['textDocument/didClose'] = { 'textDocument', 'synchronization' },
|
||||
['textDocument/didOpen'] = { 'textDocument', 'synchronization' },
|
||||
['textDocument/didSave'] = { 'textDocument', 'synchronization', 'didSave' },
|
||||
['textDocument/documentColor'] = { 'textDocument', 'colorProvider' },
|
||||
['textDocument/documentHighlight'] = { 'textDocument', 'documentHighlight' },
|
||||
['textDocument/documentLink'] = { 'textDocument', 'documentLink' },
|
||||
['textDocument/documentSymbol'] = { 'textDocument', 'documentSymbol' },
|
||||
['textDocument/foldingRange'] = { 'textDocument', 'foldingRange' },
|
||||
['textDocument/formatting'] = { 'textDocument', 'formatting' },
|
||||
['textDocument/hover'] = { 'textDocument', 'hover' },
|
||||
['textDocument/implementation'] = { 'textDocument', 'implementation' },
|
||||
['textDocument/inlayHint'] = { 'textDocument', 'inlayHint' },
|
||||
['textDocument/inlineCompletion'] = { 'textDocument', 'inlineCompletion' },
|
||||
['textDocument/inlineValue'] = { 'textDocument', 'inlineValue' },
|
||||
['textDocument/linkedEditingRange'] = { 'textDocument', 'linkedEditingRange' },
|
||||
['textDocument/moniker'] = { 'textDocument', 'moniker' },
|
||||
['textDocument/onTypeFormatting'] = { 'textDocument', 'onTypeFormatting' },
|
||||
['textDocument/prepareCallHierarchy'] = { 'textDocument', 'callHierarchy' },
|
||||
['textDocument/prepareRename'] = { 'textDocument', 'rename', 'prepareSupport' },
|
||||
['textDocument/prepareTypeHierarchy'] = { 'textDocument', 'typeHierarchy' },
|
||||
['textDocument/publishDiagnostics'] = { 'textDocument', 'publishDiagnostics' },
|
||||
['textDocument/rangeFormatting'] = { 'textDocument', 'rangeFormatting' },
|
||||
['textDocument/rangesFormatting'] = { 'textDocument', 'rangeFormatting', 'rangesSupport' },
|
||||
['textDocument/references'] = { 'textDocument', 'references' },
|
||||
['textDocument/rename'] = { 'textDocument', 'rename' },
|
||||
['textDocument/selectionRange'] = { 'textDocument', 'selectionRange' },
|
||||
['textDocument/semanticTokens/full'] = { 'textDocument', 'semanticTokens' },
|
||||
['textDocument/semanticTokens/full/delta'] = { 'textDocument', 'semanticTokens', 'requests', 'full', 'delta' },
|
||||
['textDocument/semanticTokens/range'] = { 'textDocument', 'semanticTokens', 'requests', 'range' },
|
||||
['textDocument/signatureHelp'] = { 'textDocument', 'signatureHelp' },
|
||||
['textDocument/typeDefinition'] = { 'textDocument', 'typeDefinition' },
|
||||
['textDocument/willSave'] = { 'textDocument', 'synchronization', 'willSave' },
|
||||
['textDocument/willSaveWaitUntil'] = { 'textDocument', 'synchronization', 'willSaveWaitUntil' },
|
||||
['window/showDocument'] = { 'window', 'showDocument', 'support' },
|
||||
['window/showMessage'] = { 'window', 'showMessage' },
|
||||
['window/showMessageRequest'] = { 'window', 'showMessage' },
|
||||
['window/workDoneProgress/create'] = { 'window', 'workDoneProgress' },
|
||||
['workspaceSymbol/resolve'] = { 'workspace', 'symbol', 'resolveSupport' },
|
||||
['workspace/applyEdit'] = { 'workspace', 'applyEdit' },
|
||||
['workspace/codeLens/refresh'] = { 'workspace', 'codeLens' },
|
||||
['workspace/configuration'] = { 'workspace', 'configuration' },
|
||||
['workspace/diagnostic'] = { 'workspace', 'diagnostics' },
|
||||
['workspace/diagnostic/refresh'] = { 'workspace', 'diagnostics', 'refreshSupport' },
|
||||
---TODO: also has workspace/* items because spec lacks a top-level "workspaceProvider"
|
||||
protocol._provider_to_client_registration = {
|
||||
['callHierarchyProvider'] = { 'textDocument', 'callHierarchy' },
|
||||
['codeActionProvider'] = { 'textDocument', 'codeAction' },
|
||||
['codeLensProvider'] = { 'textDocument', 'codeLens' },
|
||||
['colorProvider'] = { 'textDocument', 'colorProvider' },
|
||||
['completionProvider'] = { 'textDocument', 'completion' },
|
||||
['declarationProvider'] = { 'textDocument', 'declaration' },
|
||||
['definitionProvider'] = { 'textDocument', 'definition' },
|
||||
['diagnosticProvider'] = { 'textDocument', 'diagnostic' },
|
||||
['documentFormattingProvider'] = { 'textDocument', 'formatting' },
|
||||
['documentHighlightProvider'] = { 'textDocument', 'documentHighlight' },
|
||||
['documentLinkProvider'] = { 'textDocument', 'documentLink' },
|
||||
['documentOnTypeFormattingProvider'] = { 'textDocument', 'onTypeFormatting' },
|
||||
['documentRangeFormattingProvider'] = { 'textDocument', 'rangeFormatting' },
|
||||
['documentSymbolProvider'] = { 'textDocument', 'documentSymbol' },
|
||||
['executeCommandProvider'] = { 'workspace', 'executeCommand' },
|
||||
['foldingRangeProvider'] = { 'textDocument', 'foldingRange' },
|
||||
['hoverProvider'] = { 'textDocument', 'hover' },
|
||||
['implementationProvider'] = { 'textDocument', 'implementation' },
|
||||
['inlayHintProvider'] = { 'textDocument', 'inlayHint' },
|
||||
['inlineCompletionProvider'] = { 'textDocument', 'inlineCompletion' },
|
||||
['inlineValueProvider'] = { 'textDocument', 'inlineValue' },
|
||||
['linkedEditingRangeProvider'] = { 'textDocument', 'linkedEditingRange' },
|
||||
['monikerProvider'] = { 'textDocument', 'moniker' },
|
||||
['referencesProvider'] = { 'textDocument', 'references' },
|
||||
['renameProvider'] = { 'textDocument', 'rename' },
|
||||
['selectionRangeProvider'] = { 'textDocument', 'selectionRange' },
|
||||
['semanticTokensProvider'] = { 'textDocument', 'semanticTokens' },
|
||||
['signatureHelpProvider'] = { 'textDocument', 'signatureHelp' },
|
||||
['textDocumentSync'] = { 'textDocument', 'synchronization' },
|
||||
['typeDefinitionProvider'] = { 'textDocument', 'typeDefinition' },
|
||||
['typeHierarchyProvider'] = { 'textDocument', 'typeHierarchy' },
|
||||
['workspace/didChangeConfiguration'] = { 'workspace', 'didChangeConfiguration' },
|
||||
['workspace/didChangeWatchedFiles'] = { 'workspace', 'didChangeWatchedFiles' },
|
||||
['workspace/didCreateFiles'] = { 'workspace', 'fileOperations', 'didCreate' },
|
||||
['workspace/didDeleteFiles'] = { 'workspace', 'fileOperations', 'didDelete' },
|
||||
['workspace/didRenameFiles'] = { 'workspace', 'fileOperations', 'didRename' },
|
||||
['workspace/executeCommand'] = { 'workspace', 'executeCommand' },
|
||||
['workspace/foldingRange/refresh'] = { 'workspace', 'foldingRange', 'refreshSupport' },
|
||||
['workspace/inlayHint/refresh'] = { 'workspace', 'inlayHint', 'refreshSupport' },
|
||||
['workspace/inlineValue/refresh'] = { 'workspace', 'inlineValue', 'refreshSupport' },
|
||||
['workspace/semanticTokens/refresh'] = { 'workspace', 'semanticTokens', 'refreshSupport' },
|
||||
['workspace/symbol'] = { 'workspace', 'symbol' },
|
||||
['workspace/textDocumentContent'] = { 'workspace', 'textDocumentContent' },
|
||||
['workspace/willCreateFiles'] = { 'workspace', 'fileOperations', 'willCreate' },
|
||||
['workspace/willDeleteFiles'] = { 'workspace', 'fileOperations', 'willDelete' },
|
||||
['workspace/willRenameFiles'] = { 'workspace', 'fileOperations', 'willRename' },
|
||||
['workspace/workspaceFolders'] = { 'workspace', 'workspaceFolders' },
|
||||
['workspaceSymbolProvider'] = { 'workspace', 'symbol' },
|
||||
}
|
||||
-- stylua: ignore end
|
||||
|
||||
@@ -1299,13 +1278,13 @@ protocol._request_name_to_server_capability = {
|
||||
['workspace/willRenameFiles'] = { 'workspace', 'fileOperations', 'willRename' },
|
||||
['workspace/workspaceFolders'] = { 'workspace', 'workspaceFolders' },
|
||||
['textDocument/semanticTokens'] = { 'semanticTokensProvider' },
|
||||
['workspace/didChangeWatchedFiles'] = { 'workspace/didChangeWatchedFiles' },
|
||||
}
|
||||
-- stylua: ignore end
|
||||
|
||||
-- stylua: ignore start
|
||||
-- Generated by gen_lsp.lua, keep at end of file.
|
||||
--- Maps method names to the required client capability
|
||||
protocol._request_name_allows_registration = {
|
||||
protocol._method_supports_dynamic_registration = {
|
||||
['notebookDocument/didChange'] = true,
|
||||
['notebookDocument/didClose'] = true,
|
||||
['notebookDocument/didOpen'] = true,
|
||||
@@ -1351,6 +1330,7 @@ protocol._request_name_allows_registration = {
|
||||
['textDocument/willSaveWaitUntil'] = true,
|
||||
['workspace/didChangeConfiguration'] = true,
|
||||
['workspace/didChangeWatchedFiles'] = true,
|
||||
['workspace/didChangeWorkspaceFolders'] = true,
|
||||
['workspace/didCreateFiles'] = true,
|
||||
['workspace/didDeleteFiles'] = true,
|
||||
['workspace/didRenameFiles'] = true,
|
||||
@@ -1363,4 +1343,51 @@ protocol._request_name_allows_registration = {
|
||||
}
|
||||
-- stylua: ignore end
|
||||
|
||||
-- stylua: ignore start
|
||||
-- Generated by gen_lsp.lua, keep at end of file.
|
||||
protocol._method_supports_static_registration = {
|
||||
['textDocument/codeAction'] = true,
|
||||
['textDocument/codeLens'] = true,
|
||||
['textDocument/colorPresentation'] = true,
|
||||
['textDocument/completion'] = true,
|
||||
['textDocument/declaration'] = true,
|
||||
['textDocument/definition'] = true,
|
||||
['textDocument/diagnostic'] = true,
|
||||
['textDocument/didChange'] = true,
|
||||
['textDocument/documentColor'] = true,
|
||||
['textDocument/documentHighlight'] = true,
|
||||
['textDocument/documentLink'] = true,
|
||||
['textDocument/documentSymbol'] = true,
|
||||
['textDocument/foldingRange'] = true,
|
||||
['textDocument/formatting'] = true,
|
||||
['textDocument/hover'] = true,
|
||||
['textDocument/implementation'] = true,
|
||||
['textDocument/inlayHint'] = true,
|
||||
['textDocument/inlineCompletion'] = true,
|
||||
['textDocument/inlineValue'] = true,
|
||||
['textDocument/linkedEditingRange'] = true,
|
||||
['textDocument/moniker'] = true,
|
||||
['textDocument/onTypeFormatting'] = true,
|
||||
['textDocument/prepareCallHierarchy'] = true,
|
||||
['textDocument/prepareTypeHierarchy'] = true,
|
||||
['textDocument/rangeFormatting'] = true,
|
||||
['textDocument/references'] = true,
|
||||
['textDocument/rename'] = true,
|
||||
['textDocument/selectionRange'] = true,
|
||||
['textDocument/semanticTokens/full'] = true,
|
||||
['textDocument/signatureHelp'] = true,
|
||||
['textDocument/typeDefinition'] = true,
|
||||
['workspace/executeCommand'] = true,
|
||||
['workspace/symbol'] = true,
|
||||
}
|
||||
-- stylua: ignore end
|
||||
|
||||
-- stylua: ignore start
|
||||
-- Generated by gen_lsp.lua, keep at end of file.
|
||||
-- These methods have no registration options but can still be registered dynamically.
|
||||
protocol._methods_with_no_registration_options = {
|
||||
['workspace/didChangeWorkspaceFolders'] = true ,
|
||||
}
|
||||
-- stylua: ignore end
|
||||
|
||||
return protocol
|
||||
|
||||
Reference in New Issue
Block a user