mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
feat(lsp)!: turn format filter into predicate (#18458)
This makes the common use case easier. If one really needs access to all clients, they can create a filter function which manually calls `get_active_clients`.
This commit is contained in:
parent
c6d6b8c7eb
commit
fa3492c5f7
@ -1120,19 +1120,13 @@ format({options}) *vim.lsp.buf.format()*
|
|||||||
• bufnr (number|nil): Restrict formatting to the clients
|
• bufnr (number|nil): Restrict formatting to the clients
|
||||||
attached to the given buffer, defaults to the current
|
attached to the given buffer, defaults to the current
|
||||||
buffer (0).
|
buffer (0).
|
||||||
• filter (function|nil): Predicate to filter clients used
|
• filter (function|nil): Predicate used to filter clients.
|
||||||
for formatting. Receives the list of clients attached to
|
Receives a client as argument and must return a boolean.
|
||||||
bufnr as the argument and must return the list of
|
Clients matching the predicate are included. Example: • >
|
||||||
clients on which to request formatting. Example: • >
|
|
||||||
|
|
||||||
-- Never request typescript-language-server for formatting
|
-- Never request typescript-language-server for formatting
|
||||||
vim.lsp.buf.format {
|
vim.lsp.buf.format {
|
||||||
filter = function(clients)
|
filter = function(client) return client.name ~= "tsserver" end
|
||||||
return vim.tbl_filter(
|
|
||||||
function(client) return client.name ~= "tsserver" end,
|
|
||||||
clients
|
|
||||||
)
|
|
||||||
end
|
|
||||||
}
|
}
|
||||||
<
|
<
|
||||||
• async boolean|nil If true the method won't block.
|
• async boolean|nil If true the method won't block.
|
||||||
|
@ -177,20 +177,15 @@ end
|
|||||||
--- - bufnr (number|nil):
|
--- - bufnr (number|nil):
|
||||||
--- Restrict formatting to the clients attached to the given buffer, defaults to the current
|
--- Restrict formatting to the clients attached to the given buffer, defaults to the current
|
||||||
--- buffer (0).
|
--- buffer (0).
|
||||||
|
---
|
||||||
--- - filter (function|nil):
|
--- - filter (function|nil):
|
||||||
--- Predicate to filter clients used for formatting. Receives the list of clients attached
|
--- Predicate used to filter clients. Receives a client as argument and must return a
|
||||||
--- to bufnr as the argument and must return the list of clients on which to request
|
--- boolean. Clients matching the predicate are included. Example:
|
||||||
--- formatting. Example:
|
|
||||||
---
|
---
|
||||||
--- <pre>
|
--- <pre>
|
||||||
--- -- Never request typescript-language-server for formatting
|
--- -- Never request typescript-language-server for formatting
|
||||||
--- vim.lsp.buf.format {
|
--- vim.lsp.buf.format {
|
||||||
--- filter = function(clients)
|
--- filter = function(client) return client.name ~= "tsserver" end
|
||||||
--- return vim.tbl_filter(
|
|
||||||
--- function(client) return client.name ~= "tsserver" end,
|
|
||||||
--- clients
|
|
||||||
--- )
|
|
||||||
--- end
|
|
||||||
--- }
|
--- }
|
||||||
--- </pre>
|
--- </pre>
|
||||||
---
|
---
|
||||||
@ -207,18 +202,14 @@ end
|
|||||||
function M.format(options)
|
function M.format(options)
|
||||||
options = options or {}
|
options = options or {}
|
||||||
local bufnr = options.bufnr or vim.api.nvim_get_current_buf()
|
local bufnr = options.bufnr or vim.api.nvim_get_current_buf()
|
||||||
local clients = vim.lsp.buf_get_clients(bufnr)
|
local clients = vim.lsp.get_active_clients({
|
||||||
|
id = options.id,
|
||||||
|
bufnr = bufnr,
|
||||||
|
name = options.name,
|
||||||
|
})
|
||||||
|
|
||||||
if options.filter then
|
if options.filter then
|
||||||
clients = options.filter(clients)
|
clients = vim.tbl_filter(options.filter, clients)
|
||||||
elseif options.id then
|
|
||||||
clients = vim.tbl_filter(function(client)
|
|
||||||
return client.id == options.id
|
|
||||||
end, clients)
|
|
||||||
elseif options.name then
|
|
||||||
clients = vim.tbl_filter(function(client)
|
|
||||||
return client.name == options.name
|
|
||||||
end, clients)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
clients = vim.tbl_filter(function(client)
|
clients = vim.tbl_filter(function(client)
|
||||||
|
Loading…
Reference in New Issue
Block a user