nvim: lsp: Update capabilities check

Dynamic capabilities were introduced in neovim with commit ddd92a7.

With dynamic registration of LSP capabilities, a client's `server_capabilities`
is no longer a sufficient indicator to see if a server supports a feature. We
instead need to use `client.supports_method(<method>)` which  considers both
the dynamic capabilities and static `server_capabilities`.
This commit is contained in:
Sanchayan Maity 2023-07-10 14:24:50 +05:30
parent 5b108ac9b4
commit 549bc29b6f
Signed by: sanchayanmaity
GPG key ID: 6F6A0609C12038F3

View file

@ -48,29 +48,29 @@ protocol.CompletionItemKind = {
local lsp_augroup_id = vim.api.nvim_create_augroup("LSP", { clear = true })
local lsp_key_mappings = {
{ "definitionProvider" , 'n', 'pd' , '<cmd>lua PeekDefinition()<CR>' },
{ "definitionProvider" , 'n', 'gd' , '<cmd>lua vim.lsp.buf.definition()<CR>' },
--{ "declarationProvider" , 'n', 'gD' , '<cmd>lua vim.lsp.buf.declaration()<CR>' },
{ "typeDefinitionProvider" , 'n', '<C-k>' , '<cmd>lua vim.lsp.buf.type_definition()<CR>' },
{ "referencesProvider" , 'n', 'gr' , '<cmd>lua vim.lsp.buf.references()<CR>' },
{ "implementationProvider" , 'n', 'gD' , '<cmd>lua vim.lsp.buf.implementation()<CR>' },
{ "documentSymbolProvider" , 'n', '1gd' , '<cmd>lua vim.lsp.buf.document_symbol()<CR>' },
{ "workspaceSymbolProvider", 'n', '1gD' , '<cmd>lua vim.lsp.buf.workspace_symbol()<CR>' },
{ "hoverProvider" , 'n', '<Leader>k', '<cmd>lua vim.lsp.buf.hover()<CR>' },
{ "signatureHelpProvider" , 'n', '<Leader>S', '<cmd>lua vim.lsp.buf.signature_help()<CR>' },
{ "renameProvider" , 'n', 'gR' , '<cmd>lua vim.lsp.buf.rename()<CR>' },
{ "inlayHintProvider" , 'n', 'gi' , '<cmd>lua vim.lsp.inlay_hint(0, nil)<CR>' },
{ "textDocument/definition" , 'n', 'pd' , '<cmd>lua PeekDefinition()<CR>' },
{ "textDocument/definition" , 'n', 'gd' , '<cmd>lua vim.lsp.buf.definition()<CR>' },
--{ "textDocument/declaration" , 'n', 'gD' , '<cmd>lua vim.lsp.buf.declaration()<CR>' },
{ "textDocument/typeDefinition" , 'n', '<C-k>' , '<cmd>lua vim.lsp.buf.type_definition()<CR>' },
{ "textDocument/references" , 'n', 'gr' , '<cmd>lua vim.lsp.buf.references()<CR>' },
{ "textDocument/implementation" , 'n', 'gD' , '<cmd>lua vim.lsp.buf.implementation()<CR>' },
{ "textDocument/documentSymbol" , 'n', '1gd' , '<cmd>lua vim.lsp.buf.document_symbol()<CR>' },
{ "workspace/symbol" , 'n', '1gD' , '<cmd>lua vim.lsp.buf.workspace_symbol()<CR>' },
{ "textDocument/hover" , 'n', '<Leader>k', '<cmd>lua vim.lsp.buf.hover()<CR>' },
{ "textDocument/signatureHelp" , 'n', '<Leader>S', '<cmd>lua vim.lsp.buf.signature_help()<CR>' },
{ "textDocument/rename" , 'n', 'gR' , '<cmd>lua vim.lsp.buf.rename()<CR>' },
{ "textDocument/inlayHint" , 'n', 'gi' , '<cmd>lua vim.lsp.inlay_hint(0, nil)<CR>' },
{ "documentRangeFormattingProvider", 'x', 'gq', '<cmd>lua vim.lsp.buf.format({async=true})<CR>' },
{ "documentFormattingProvider" , 'n', 'gq', '<cmd>lua vim.lsp.buf.format({async=true})<CR>' },
{ "textDocument/rangeFormatting", 'x', 'gq', '<cmd>lua vim.lsp.buf.format({async=true})<CR>' },
{ "textDocument/formatting" , 'n', 'gq', '<cmd>lua vim.lsp.buf.format({async=true})<CR>' },
{ "codeActionProvider", 'n', 'ga' , '<cmd>lua vim.lsp.buf.code_action()<CR>' },
{ "codeActionProvider", 'v', 'ga' , '<cmd>lua vim.lsp.buf.code_action()<CR>' },
{ "codeActionProvider", 'n', '<Leader>r', '<cmd>lua vim.lsp.buf.code_action{only = \'refactor\' }<CR>' },
{ "codeActionProvider", 'v', '<Leader>r', '<cmd>lua vim.lsp.buf.code_action{only = \'refactor\' }<CR>' },
{ "textDocument/codeAction", 'n', 'ga' , '<cmd>lua vim.lsp.buf.code_action()<CR>' },
{ "textDocument/codeAction", 'v', 'ga' , '<cmd>lua vim.lsp.buf.code_action()<CR>' },
{ "textDocument/codeAction", 'n', '<Leader>r', '<cmd>lua vim.lsp.buf.code_action{only = \'refactor\' }<CR>' },
{ "textDocument/codeAction", 'v', '<Leader>r', '<cmd>lua vim.lsp.buf.code_action{only = \'refactor\' }<CR>' },
{ "codeLensProvider", 'n', '<LocalLeader>l', '<cmd>lua vim.lsp.codelens.run()<CR>' },
{ "codeLensProvider", 'n', '<LocalLeader>L', '<cmd>lua vim.lsp.codelens.clear()<CR>' },
{ "textDocument/codeLens", 'n', '<LocalLeader>l', '<cmd>lua vim.lsp.codelens.run()<CR>' },
{ "textDocument/codeLens", 'n', '<LocalLeader>L', '<cmd>lua vim.lsp.codelens.clear()<CR>' },
}
local CODE_ACTION_AVAILABLE = "CodeActionAvailable"
@ -189,12 +189,12 @@ local on_attach = function(client, bufnr)
for _, mappings in pairs(lsp_key_mappings) do
local capability, mode, lhs, rhs = unpack(mappings)
if client.server_capabilities[capability] then
if client.supports_method(capability) then
vim.api.nvim_buf_set_keymap(bufnr, mode, lhs, rhs, opts)
end
end
if client.server_capabilities.codeActionProvider then
if client.supports_method('textDocument/codeAction') then
vim.api.nvim_create_autocmd({"CursorHold", "CursorHoldI"}, {
group = lsp_augroup_id,
buffer = bufnr,
@ -215,7 +215,7 @@ local on_attach = function(client, bufnr)
})
end
if client.server_capabilities.codeLensProvider then
if client.supports_method('textDocument/codeLens') then
vim.api.nvim_create_autocmd({"CursorHold", "CursorHoldI", "InsertLeave"}, {
group = lsp_augroup_id,
buffer = bufnr,
@ -223,7 +223,7 @@ local on_attach = function(client, bufnr)
})
end
if client.server_capabilities.documentHighlightProvider then
if client.supports_method('textDocument/documentHighlight') then
vim.api.nvim_create_autocmd("CursorHold", {
group = lsp_augroup_id,
buffer = bufnr,
@ -236,7 +236,7 @@ local on_attach = function(client, bufnr)
})
end
if client.server_capabilities.inlayHintProvider then
if client.supports_method('textDocument/inlayHint') then
vim.lsp.inlay_hint(bufnr, false)
end
end
@ -250,7 +250,7 @@ vim.api.nvim_create_autocmd("LspAttach", {
for _, mappings in pairs(lsp_key_mappings) do
local capability, mode, lhs, rhs = unpack(mappings)
if client.server_capabilities[capability] then
if client.supports_method(capability) then
vim.api.nvim_buf_set_keymap(bufnr, mode, lhs, rhs, opts)
end
end
@ -267,7 +267,7 @@ vim.api.nvim_create_autocmd("LspDetach", {
for _, mappings in pairs(lsp_key_mappings) do
local capability, mode, lhs, _ = unpack(mappings)
if client.server_capabilities and client.server_capabilities[capability] then
if client.supports_method(capability) then
vim.api.nvim_buf_del_keymap(bufnr, mode, lhs)
end
end