Skip to content

Commit

Permalink
Version 2.2.3
Browse files Browse the repository at this point in the history
Don't format with client if `on_attach` was not called for that client
  • Loading branch information
lukas-reineke committed Apr 18, 2022
1 parent f967bfb commit 4ba2f95
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ vim.cmd [[cabbrev wq execute "lua vim.lsp.buf.formatting_seq_sync()" <bar> wq]]

`exclude` is a special format option that lists LSP servers that should not format the buffer.

Alternatively, you can also just not call `on_attach` for the clients you don't want to use for
formatting.

#### `order` format option

`order` is a special format option that determines the order formatting is requested from the LSP server.
Expand Down
5 changes: 4 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.2.2
Version: 2.2.3

==============================================================================
CONTENTS *lsp-format*
Expand Down Expand Up @@ -96,6 +96,9 @@ the `order` list. (same logic as |vim.lsp.buf.formatting_seq_sync()|)
==============================================================================
4. CHANGELOG *lsp-format-changelog*

2.2.3
Don't format with client if `on_attach` was not called for that client

2.2.2
Don't overwrite the global format handler

Expand Down
23 changes: 15 additions & 8 deletions lua/lsp-format/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local M = {
disabled = false,
disabled_filetypes = {},
queue = {},
buffers = {},
}

M.setup = function(format_options)
Expand Down Expand Up @@ -31,7 +32,10 @@ M.format = function(format_options_string)

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
if
vim.tbl_contains(format_options.exclude or {}, client.name)
or not vim.tbl_contains(M.buffers[bufnr] or {}, client.id)
then
table.remove(clients, i)
end
end
Expand Down Expand Up @@ -78,14 +82,17 @@ M.toggle = function(filetype)
end

M.on_attach = function(client)
if client.resolved_capabilities.document_formatting then
vim.cmd [[
augroup Format
autocmd! * <buffer>
autocmd BufWritePost <buffer> lua require'lsp-format'.format()
augroup END
]]
local bufnr = vim.api.nvim_get_current_buf()
if M.buffers[bufnr] == nil then
M.buffers[bufnr] = {}
end
table.insert(M.buffers[bufnr], client.id)
vim.cmd [[
augroup Format
autocmd! * <buffer>
autocmd BufWritePost <buffer> lua require'lsp-format'.format()
augroup END
]]
end

M._handler = function(err, result, ctx)
Expand Down

0 comments on commit 4ba2f95

Please sign in to comment.