39 lines
1.2 KiB
Django/Jinja
39 lines
1.2 KiB
Django/Jinja
-- {{ ansible_managed }}
|
|
|
|
local enable_formatting = vim.env.enable_formatting == 'true';
|
|
local legacy_formatting = nil;
|
|
|
|
-- TODO: move to utils file
|
|
-- TODO: add efm-languageserver to lsp.lua
|
|
if vim.env.VIRTUAL_ENV ~= nil and enable_formatting == true then
|
|
local formatters = vim.fs.find(
|
|
{ 'isort', 'black' },
|
|
{ limit = 1, type = 'file', path = vim.fs.joinpath(vim.env.VIRTUAL_ENV, 'bin') }
|
|
)
|
|
|
|
legacy_formatting = #formatters > 0;
|
|
end
|
|
|
|
if (enable_formatting) then
|
|
vim.api.nvim_create_autocmd('LspAttach', {
|
|
group = vim.api.nvim_create_augroup('lsp', { clear = true }),
|
|
callback = function(args)
|
|
local client = vim.lsp.get_client_by_id(args.data.client_id)
|
|
|
|
if client.server_capabilities.documentFormattingProvider then
|
|
vim.api.nvim_create_autocmd('BufWritePre', {
|
|
buffer = args.buf,
|
|
callback = function()
|
|
vim.lsp.buf.format {
|
|
async = false,
|
|
id = args.data.client_id,
|
|
filter = function(format_client)
|
|
return format_client.name ~= 'ruff' or not legacy_formatting
|
|
end
|
|
}
|
|
end,
|
|
})
|
|
end
|
|
end
|
|
})
|
|
end
|