mirror of
https://github.com/neovim/neovim.git
synced 2025-09-06 19:38:20 +00:00
feat(lsp): add method filter to get_active_clients (#24319)
This commit is contained in:

committed by
GitHub

parent
ef94fb69c6
commit
317c80f460
@@ -780,6 +780,8 @@ get_active_clients({filter}) *vim.lsp.get_active_clients()*
|
|||||||
• bufnr (number): Only return clients attached to this
|
• bufnr (number): Only return clients attached to this
|
||||||
buffer
|
buffer
|
||||||
• name (string): Only return clients with the given name
|
• name (string): Only return clients with the given name
|
||||||
|
• method (string): Only return clients supporting the given
|
||||||
|
method
|
||||||
|
|
||||||
Return: ~
|
Return: ~
|
||||||
lsp.Client []: List of |vim.lsp.client| objects
|
lsp.Client []: List of |vim.lsp.client| objects
|
||||||
|
@@ -1995,6 +1995,7 @@ end
|
|||||||
---@field id integer|nil Match clients by id
|
---@field id integer|nil Match clients by id
|
||||||
---@field bufnr integer|nil match clients attached to the given buffer
|
---@field bufnr integer|nil match clients attached to the given buffer
|
||||||
---@field name string|nil match clients by name
|
---@field name string|nil match clients by name
|
||||||
|
---@field method string|nil match client by supported method name
|
||||||
|
|
||||||
--- Get active clients.
|
--- Get active clients.
|
||||||
---
|
---
|
||||||
@@ -2004,6 +2005,7 @@ end
|
|||||||
--- - id (number): Only return clients with the given id
|
--- - id (number): Only return clients with the given id
|
||||||
--- - bufnr (number): Only return clients attached to this buffer
|
--- - bufnr (number): Only return clients attached to this buffer
|
||||||
--- - name (string): Only return clients with the given name
|
--- - name (string): Only return clients with the given name
|
||||||
|
--- - method (string): Only return clients supporting the given method
|
||||||
---@return lsp.Client[]: List of |vim.lsp.client| objects
|
---@return lsp.Client[]: List of |vim.lsp.client| objects
|
||||||
function lsp.get_active_clients(filter)
|
function lsp.get_active_clients(filter)
|
||||||
validate({ filter = { filter, 't', true } })
|
validate({ filter = { filter, 't', true } })
|
||||||
@@ -2020,6 +2022,7 @@ function lsp.get_active_clients(filter)
|
|||||||
client
|
client
|
||||||
and (filter.id == nil or client.id == filter.id)
|
and (filter.id == nil or client.id == filter.id)
|
||||||
and (filter.name == nil or client.name == filter.name)
|
and (filter.name == nil or client.name == filter.name)
|
||||||
|
and (filter.method == nil or client.supports_method(filter.method, { bufnr = filter.bufnr }))
|
||||||
then
|
then
|
||||||
clients[#clients + 1] = client
|
clients[#clients + 1] = client
|
||||||
end
|
end
|
||||||
|
@@ -197,15 +197,6 @@ end
|
|||||||
function M.format(options)
|
function M.format(options)
|
||||||
options = options or {}
|
options = options or {}
|
||||||
local bufnr = options.bufnr or api.nvim_get_current_buf()
|
local bufnr = options.bufnr or api.nvim_get_current_buf()
|
||||||
local clients = vim.lsp.get_active_clients({
|
|
||||||
id = options.id,
|
|
||||||
bufnr = bufnr,
|
|
||||||
name = options.name,
|
|
||||||
})
|
|
||||||
|
|
||||||
if options.filter then
|
|
||||||
clients = vim.tbl_filter(options.filter, clients)
|
|
||||||
end
|
|
||||||
|
|
||||||
local mode = api.nvim_get_mode().mode
|
local mode = api.nvim_get_mode().mode
|
||||||
local range = options.range
|
local range = options.range
|
||||||
@@ -214,9 +205,15 @@ function M.format(options)
|
|||||||
end
|
end
|
||||||
local method = range and 'textDocument/rangeFormatting' or 'textDocument/formatting'
|
local method = range and 'textDocument/rangeFormatting' or 'textDocument/formatting'
|
||||||
|
|
||||||
clients = vim.tbl_filter(function(client)
|
local clients = vim.lsp.get_active_clients({
|
||||||
return client.supports_method(method)
|
id = options.id,
|
||||||
end, clients)
|
bufnr = bufnr,
|
||||||
|
name = options.name,
|
||||||
|
method = method,
|
||||||
|
})
|
||||||
|
if options.filter then
|
||||||
|
clients = vim.tbl_filter(options.filter, clients)
|
||||||
|
end
|
||||||
|
|
||||||
if #clients == 0 then
|
if #clients == 0 then
|
||||||
vim.notify('[LSP] Format request failed, no matching language servers.')
|
vim.notify('[LSP] Format request failed, no matching language servers.')
|
||||||
@@ -277,16 +274,13 @@ function M.rename(new_name, options)
|
|||||||
local clients = vim.lsp.get_active_clients({
|
local clients = vim.lsp.get_active_clients({
|
||||||
bufnr = bufnr,
|
bufnr = bufnr,
|
||||||
name = options.name,
|
name = options.name,
|
||||||
|
-- Clients must at least support rename, prepareRename is optional
|
||||||
|
method = 'textDocument/rename',
|
||||||
})
|
})
|
||||||
if options.filter then
|
if options.filter then
|
||||||
clients = vim.tbl_filter(options.filter, clients)
|
clients = vim.tbl_filter(options.filter, clients)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Clients must at least support rename, prepareRename is optional
|
|
||||||
clients = vim.tbl_filter(function(client)
|
|
||||||
return client.supports_method('textDocument/rename')
|
|
||||||
end, clients)
|
|
||||||
|
|
||||||
if #clients == 0 then
|
if #clients == 0 then
|
||||||
vim.notify('[LSP] Rename, no matching language servers with rename capability.')
|
vim.notify('[LSP] Rename, no matching language servers with rename capability.')
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user