diff options
-rw-r--r-- | runtime/doc/lsp.txt | 14 | ||||
-rw-r--r-- | runtime/lua/vim/lsp/buf.lua | 29 |
2 files changed, 14 insertions, 29 deletions
diff --git a/runtime/doc/lsp.txt b/runtime/doc/lsp.txt index af3189a393..841ed36421 100644 --- a/runtime/doc/lsp.txt +++ b/runtime/doc/lsp.txt @@ -1120,19 +1120,13 @@ format({options}) *vim.lsp.buf.format()* • bufnr (number|nil): Restrict formatting to the clients attached to the given buffer, defaults to the current buffer (0). - • filter (function|nil): Predicate to filter clients used - for formatting. Receives the list of clients attached to - bufnr as the argument and must return the list of - clients on which to request formatting. Example: • > + • filter (function|nil): Predicate used to filter clients. + Receives a client as argument and must return a boolean. + Clients matching the predicate are included. Example: • > -- Never request typescript-language-server for formatting vim.lsp.buf.format { - filter = function(clients) - return vim.tbl_filter( - function(client) return client.name ~= "tsserver" end, - clients - ) - end + filter = function(client) return client.name ~= "tsserver" end } < • async boolean|nil If true the method won't block. diff --git a/runtime/lua/vim/lsp/buf.lua b/runtime/lua/vim/lsp/buf.lua index 1207da094a..0e86bff4f2 100644 --- a/runtime/lua/vim/lsp/buf.lua +++ b/runtime/lua/vim/lsp/buf.lua @@ -177,20 +177,15 @@ end --- - bufnr (number|nil): --- Restrict formatting to the clients attached to the given buffer, defaults to the current --- buffer (0). +--- --- - filter (function|nil): ---- Predicate to filter clients used for formatting. Receives the list of clients attached ---- to bufnr as the argument and must return the list of clients on which to request ---- formatting. Example: +--- Predicate used to filter clients. Receives a client as argument and must return a +--- boolean. Clients matching the predicate are included. Example: --- --- <pre> --- -- Never request typescript-language-server for formatting --- vim.lsp.buf.format { ---- filter = function(clients) ---- return vim.tbl_filter( ---- function(client) return client.name ~= "tsserver" end, ---- clients ---- ) ---- end +--- filter = function(client) return client.name ~= "tsserver" end --- } --- </pre> --- @@ -207,18 +202,14 @@ end function M.format(options) options = options or {} 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 - clients = 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) + clients = vim.tbl_filter(options.filter, clients) end clients = vim.tbl_filter(function(client) |