refactor(util.lsp): easier lsp client filters

This commit is contained in:
Folke Lemaitre 2023-10-13 12:20:55 +02:00
parent 4584410e76
commit 3f1bf70b14
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040

View file

@ -1,10 +1,26 @@
local Util = require("lazyvim.util")
---@class lazyvim.util.lsp
local M = {}
function M.get_clients(...)
---@diagnostic disable-next-line: deprecated
local fn = vim.lsp.get_clients or vim.lsp.get_active_clients
return fn(...)
---@alias lsp.Client.filter {id?: number, bufnr?: number, name?: string, method?: string, filter?:fun(client: lsp.Client):boolean}
---@param opts? lsp.Client.filter
function M.get_clients(opts)
local ret = {} ---@type lsp.Client[]
if vim.lsp.get_clients then
ret = vim.lsp.get_clients(opts)
else
---@diagnostic disable-next-line: deprecated
ret = vim.lsp.get_active_clients(opts)
if opts and opts.method then
---@param client lsp.Client
ret = vim.tbl_filter(function(client)
return client.supports_method(opts.method, { bufnr = opts.bufnr })
end, ret)
end
end
return opts and opts.filter and vim.tbl_filter(opts.filter, ret) or ret
end
---@param on_attach fun(client, buffer)
@ -59,39 +75,26 @@ function M.disable(server, cond)
end)
end
---@alias lsp.Client.filter fun(client: lsp.Client): boolean
---@param name string
---@return lsp.Client.filter
function M.filter(name)
return function(client)
return client.name == name
end
end
---@param opts? LazyFormatter| {filter?: (string|lsp.Client.filter), bufnr?: number}
---@param opts? LazyFormatter| {filter?: (string|lsp.Client.filter)}
function M.formatter(opts)
opts = opts or {}
local filter = opts.filter
filter = type(filter) == "string" and M.filter(filter) or filter
---@cast filter lsp.Client.filter?
local filter = opts.filter or {}
filter = type(filter) == "string" and { name = filter } or filter
---@cast filter lsp.Client.filter
---@type LazyFormatter
local ret = {
name = "LSP",
primary = true,
priority = 1,
format = function(buf)
M.format({ bufnr = buf, filter = filter })
M.format(Util.merge(filter, { bufnr = buf }))
end,
sources = function(buf)
local clients = M.get_clients({ bufnr = buf })
local clients = M.get_clients(Util.merge(filter, { bufnr = buf }))
---@param client lsp.Client
local ret = vim.tbl_filter(function(client)
return (not filter or filter(client))
and (
client.supports_method("textDocument/formatting")
or client.supports_method("textDocument/rangeFormatting")
)
return client.supports_method("textDocument/formatting")
or client.supports_method("textDocument/rangeFormatting")
end, clients)
---@param client lsp.Client
return vim.tbl_map(function(client)
@ -99,10 +102,12 @@ function M.formatter(opts)
end, ret)
end,
}
return vim.tbl_deep_extend("force", ret, opts) --[[@as LazyFormatter]]
return Util.merge(ret, opts) --[[@as LazyFormatter]]
end
---@param opts? {filter?: lsp.Client.filter, bufnr?: number}
---@alias lsp.Client.format {timeout_ms?: number, format_options?: table} | lsp.Client.filter
---@param opts? lsp.Client.format
function M.format(opts)
opts = vim.tbl_deep_extend("force", {}, opts or {}, require("lazyvim.util").opts("nvim-lspconfig").format or {})
local ok, conform = pcall(require, "conform")