diff --git a/README.md b/README.md index c1499bd..c343949 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,9 @@ vim.cmd [[cabbrev wq execute "lua vim.lsp.buf.formatting_seq_sync()" 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. diff --git a/doc/format.txt b/doc/format.txt index 6005636..d2288fd 100644 --- a/doc/format.txt +++ b/doc/format.txt @@ -2,7 +2,7 @@ Author: Lukas Reineke -Version: 2.2.2 +Version: 2.2.3 ============================================================================== CONTENTS *lsp-format* @@ -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 diff --git a/lua/lsp-format/init.lua b/lua/lsp-format/init.lua index a589f2e..004ca68 100644 --- a/lua/lsp-format/init.lua +++ b/lua/lsp-format/init.lua @@ -3,6 +3,7 @@ local M = { disabled = false, disabled_filetypes = {}, queue = {}, + buffers = {}, } M.setup = function(format_options) @@ -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 @@ -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! * - autocmd BufWritePost 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! * + autocmd BufWritePost lua require'lsp-format'.format() + augroup END + ]] end M._handler = function(err, result, ctx)