Check if current servers have document_formatting enabled

New vim.lsp.buf.format prints the following message when fails:

[LSP] Format request failed, no matching language servers.

I always have format on save enabled, so this message oftentimes annoys me.

Can I somehow check if any of the currently attached servers have document_formatting enabled. And only if yes, vim.lsp.buf.format will be run,

You could define that autocmd inside the on_attach function you passed to lsp client, the function receives a client object which can be used to check it’s capabilities, so you can check if the client supports formatting and then define the mapping and stuff if it does, like:

function on_attach(client, bufnr) {
    if client.supports_method("textDocument/formatting") then
            // Define formatting autocmd or mapping

Thanks for your suggestion, but I have a shortcut that toggles format on save on the fly by adding/removing the autocommand.

Thus, this won’t work for me.