Disable formatting for some LSP
In the lspconfig section it is mentioned that we can disable certain LSP features by setting capabilities. Is there any example for turning off formatting for e.g. tsserver?
This is from LazyVim: https://github.com/LazyVim/LazyVim/discussions/242#discussioncomment-4947842
But basically you can add
if client.name == "tsserver" then
client.server_capabilities.documentFormattingProvider = false
end
to the LspAttach autocomd, inside the nvim-lspconfig config and after defining the client (so after line 503)
@qianxyz do you want to disable lsp formatting completely, or just the autoformat on save? If it's the autoformat on save, there's a recipe from conform.nvim that I use, perhaps I should open a PR for that...
@dam9000 Thanks! I'm aware of the conform.nvim recipe. Basically I'm missing the old behavior with autoformat.lua where there is a :Format command, and you can toggle format on save globally and filter on client.name. All of these are configurable through conform.nvim now so functionally I'm all set.
I'm just curious since this is explicitly mentioned in the comments https://github.com/nvim-lua/kickstart.nvim/blob/b83b2b061c1fab0e1a3a28c185345be7957e74cd/init.lua#L593-L600 and I'm wondering how to do that here since people may want to disable lsp formatting completely. But as you said, just disabling format on save makes more sense to me now. Thanks!
I opened a PR for the "autoformat on save only for specified filetypes" https://github.com/nvim-lua/kickstart.nvim/pull/694
I think the comments are misleading: the capabilities field in the servers variable are for defining the client capabilities, but documentFormattingProvider is a server capability. It could instead be modified through a on_attach callback by modifying client.server_capabilities.
But for this particular case, I instead added a filter field to the vim.lsp.buf.format to exclude tsserver:
nmap('<leader>f', function()
vim.lsp.buf.format {
async = true,
filter = function(client) return client.name ~= "tsserver" end,
}
end, '[F]ormat')