Skip to content

Commit

Permalink
Merge pull request #31 from lukas-reineke/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-reineke authored Apr 18, 2022
2 parents 84e117b + 73f7889 commit c40edda
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
16 changes: 4 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()" <bar> 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.
Expand Down Expand Up @@ -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 }
```
15 changes: 14 additions & 1 deletion doc/format.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


Author: Lukas Reineke <lukas@reineke.jp>
Version: 2.1.2
Version: 2.2.0

==============================================================================
CONTENTS *lsp-format*
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
14 changes: 7 additions & 7 deletions lua/lsp-format/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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, "="))
Expand All @@ -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
Expand Down

0 comments on commit c40edda

Please sign in to comment.