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