fix(lsp): use supports_method method instead of field on Neovim >= 0.11

This commit is contained in:
Iordanis Petkakis 2025-04-17 14:09:47 +03:00
parent cb223553ff
commit 6e21ac4962
3 changed files with 19 additions and 6 deletions

View file

@ -8,7 +8,10 @@ return {
init = function() init = function()
vim.g.navic_silence = true vim.g.navic_silence = true
LazyVim.lsp.on_attach(function(client, buffer) LazyVim.lsp.on_attach(function(client, buffer)
if client.supports_method("textDocument/documentSymbol") then if
vim.fn.has("nvim-0.11") == 0 and client.supports_method("textDocument/documentSymbol")
or client:supports_method("textDocument/documentSymbol")
then
require("nvim-navic").attach(client, buffer) require("nvim-navic").attach(client, buffer)
end end
end) end)

View file

@ -54,7 +54,7 @@ function M.has(buffer, method)
method = method:find("/") and method or "textDocument/" .. method method = method:find("/") and method or "textDocument/" .. method
local clients = LazyVim.lsp.get_clients({ bufnr = buffer }) local clients = LazyVim.lsp.get_clients({ bufnr = buffer })
for _, client in ipairs(clients) do for _, client in ipairs(clients) do
if client.supports_method(method) then if vim.fn.has("nvim-0.11") == 0 and client.supports_method(method) or client:supports_method(method) then
return true return true
end end
end end

View file

@ -1,6 +1,16 @@
---@class lazyvim.util.lsp ---@class lazyvim.util.lsp
local M = {} local M = {}
---@param client vim.lsp.Client
---@param method string
---@param bufnr? integer
local function supports_method(client, method, bufnr)
if vim.fn.has("nvim-0.11") == 0 then
return client.supports_method and client.supports_method(method, { bufnr = bufnr })
end
return client:supports_method(method, bufnr) and client:supports_method(method, bufnr)
end
---@alias lsp.Client.filter {id?: number, bufnr?: number, name?: string, method?: string, filter?:fun(client: lsp.Client):boolean} ---@alias lsp.Client.filter {id?: number, bufnr?: number, name?: string, method?: string, filter?:fun(client: lsp.Client):boolean}
---@param opts? lsp.Client.filter ---@param opts? lsp.Client.filter
@ -14,7 +24,7 @@ function M.get_clients(opts)
if opts and opts.method then if opts and opts.method then
---@param client vim.lsp.Client ---@param client vim.lsp.Client
ret = vim.tbl_filter(function(client) ret = vim.tbl_filter(function(client)
return client.supports_method(opts.method, { bufnr = opts.bufnr }) return supports_method(client, opts.method, opts.bufnr)
end, ret) end, ret)
end end
end end
@ -75,7 +85,7 @@ function M._check_methods(client, buffer)
for method, clients in pairs(M._supports_method) do for method, clients in pairs(M._supports_method) do
clients[client] = clients[client] or {} clients[client] = clients[client] or {}
if not clients[client][buffer] then if not clients[client][buffer] then
if client.supports_method and client.supports_method(method, { bufnr = buffer }) then if supports_method(client, method, buffer) then
clients[client][buffer] = true clients[client][buffer] = true
vim.api.nvim_exec_autocmds("User", { vim.api.nvim_exec_autocmds("User", {
pattern = "LspSupportsMethod", pattern = "LspSupportsMethod",
@ -169,8 +179,8 @@ function M.formatter(opts)
local clients = M.get_clients(LazyVim.merge({}, filter, { bufnr = buf })) local clients = M.get_clients(LazyVim.merge({}, filter, { bufnr = buf }))
---@param client vim.lsp.Client ---@param client vim.lsp.Client
local ret = vim.tbl_filter(function(client) local ret = vim.tbl_filter(function(client)
return client.supports_method("textDocument/formatting") return supports_method(client, "textDocument/formatting")
or client.supports_method("textDocument/rangeFormatting") or supports_method(client, "textDocument/rangeFormatting")
end, clients) end, clients)
---@param client vim.lsp.Client ---@param client vim.lsp.Client
return vim.tbl_map(function(client) return vim.tbl_map(function(client)