lukas-reineke / lsp-format.nvim

A wrapper around Neovims native LSP formatting.
559 stars 27 forks source link

Don't format if file path matches glob #61

Closed delfick closed 1 year ago

delfick commented 1 year ago

Hello,

Is it possible to make it so that it doesn't format certain files. For example anything that matches "/site-packages/" so that I don't end up running the formatter when I've gone into a file inside my python virtualenv?

Thanks

lukas-reineke commented 1 year ago

on_attach is called per buffer. If you don't want formatting for a buffer, you can just not call on_attach. Something like

lspconfig.pyright.setup {
    on_attach = function(client)
        if not string.match(vim.api.nvim_buf_get_name(0), "site-packages") then
            require("lsp-format").on_attach(client)
        end
    end,
}
delfick commented 1 year ago

Perfect!

Thank you so much.

For prosperity, I did this

local lsp = require('lsp-zero')

...

lsp.configure("pylsp", lspconfig.pylsp.setup {
    on_attach = function(client)
        if string.match(vim.api.nvim_buf_get_name(0), "/site-packages/") then
            return
        end
        if string.match(vim.api.nvim_buf_get_name(0), "/lib/python") then
            return
        end
        require("lsp-format").on_attach(client)
    end,
    settings = {
        pylsp = {
            plugins = {
                noy_pyls = { enabled = true },
                pydocstyle = { enabled = false },
                pycodestyle = { enabled = false },
                pyflakes = { enabled = false },
                mccabe = { enabled = false },
                pylint = { enabled = false },
                yapf = { enabled = false },
                pyls_isort = { enabled = false },
                black = { enabled = true, line_length = 100 },
                pylsp_mypy = { enabled = true }
            }
        }
    },
    flags = {
        debounce_text_changes = 200,
    }
})

❤️