Enable formatting on save

Requires `ENABLE_FORMATTING` to be set
This commit is contained in:
Sonny Bakker 2025-02-11 09:10:52 +01:00
parent eb14a601fb
commit 5aa08aef39

View file

@ -156,3 +156,28 @@ vim.diagnostic.config({
border = border border = border
}, },
}) })
local enable_formatting = os.getenv("ENABLE_FORMATTING", False)
-- format buffers before saving for specific LSPs
if (enable_formatting) then
local excluded_clients = { 'pyright' }
vim.api.nvim_create_autocmd('LspAttach', {
group = vim.api.nvim_create_augroup('lsp', { clear = true }),
callback = function(args)
vim.api.nvim_create_autocmd('BufWritePre', {
buffer = args.buf,
callback = function()
vim.lsp.buf.format {
filter = function(client)
return not vim.list_contains(excluded_clients, client.name)
end,
async = false,
id = args.data.client_id
}
end,
})
end
})
end