diff --git a/README.md b/README.md index 52c9bb8..7c73830 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,10 @@ Add this abbreviation into your dotfiles to do the right thing when doing `:wq` vim.cmd [[cabbrev wq execute "lua vim.lsp.buf.formatting_seq_sync()" wq]] ``` +#### `exclude` format option + +`exclude` is a special format option that lists LSP servers that should not format the buffer. + #### `order` format option `order` is a special format option that determines the order formatting is requested from the LSP server. @@ -128,15 +132,3 @@ require "lspconfig".efm.setup { Now Typescript gets formatted with 4 and YAML with 2 spaces by default. And you can run `:Format tab_width=8` to overwrite the setting and format with 8 spaces. -### How do I exclude an LSP server from formatting? - -To exclude a server, you have to set the clients `resolved_capabilities.document_formatting` to false. -Do this in the attach function, before you call `require "lsp-format".on_attach(client)` - -```lua -local on_attach = function(client) - client.resolved_capabilities.document_formatting = false - require "lsp-format".on_attach(client) -end -require "lspconfig".gopls.setup { on_attach = on_attach } -``` diff --git a/doc/format.txt b/doc/format.txt index e7466fa..2d209f6 100644 --- a/doc/format.txt +++ b/doc/format.txt @@ -2,7 +2,7 @@ Author: Lukas Reineke -Version: 2.1.2 +Version: 2.2.0 ============================================================================== CONTENTS *lsp-format* @@ -38,6 +38,16 @@ function to each LSP that should use it. The setup functions takes one optional argument that maps |filetypes| to format options. +`exclude` is a special format option, a list of client names to exclude from +formatting. + > + require "lsp-format".setup { + typescript = { + exclude = { "tsserver" } + } + } + + `order` is a special format option, a list of client names. Formatting is requested from clients in the following order: first all clients that are not in the `order` list, then the remaining clients in the order as they occur in @@ -86,6 +96,9 @@ the `order` list. (same logic as |vim.lsp.buf.formatting_seq_sync()|) ============================================================================== 4. CHANGELOG *lsp-format-changelog* +2.2.0 + Add `exclude` option + 2.1.2 * Fix error logging diff --git a/lua/lsp-format/init.lua b/lua/lsp-format/init.lua index 1d0d544..36e9860 100644 --- a/lua/lsp-format/init.lua +++ b/lua/lsp-format/init.lua @@ -44,13 +44,6 @@ end M.format = function(format_options_string) if not vim.b.format_saving and not M.disabled and not M.disabled_filetypes[vim.bo.filetype] then local bufnr = vim.api.nvim_get_current_buf() - local clients = vim.tbl_values(vim.lsp.buf_get_clients()) - for i, client in pairs(clients) do - if not client.resolved_capabilities.document_formatting then - table.remove(clients, i) - end - end - local format_options = M.format_options[vim.bo.filetype] or {} for _, option in ipairs(vim.split(format_options_string or "", " ")) do local key, value = unpack(vim.split(option, "=")) @@ -60,6 +53,13 @@ M.format = function(format_options_string) format_options[key] = value or true end + local clients = vim.tbl_values(vim.lsp.buf_get_clients()) + for i, client in pairs(clients) do + if vim.tbl_contains(format_options.exclude or {}, client.name) then + table.remove(clients, i) + end + end + for _, client_name in pairs(format_options.order or {}) do for i, client in pairs(clients) do if client.name == client_name then