Switch to async formatting automatically if the formatter is slow

This commit is contained in:
Dreaded_X 2024-04-13 18:13:36 +02:00
parent 228cd34087
commit cf6ffdb83a
Signed by: Dreaded_X
GPG Key ID: FA5F485356B0D2D4

View File

@ -1,4 +1,5 @@
-- https://github.com/stevearc/conform.nvim
local slow_format_filetypes = {}
return {
"stevearc/conform.nvim",
event = { "BufWritePre" },
@ -35,10 +36,27 @@ return {
-- ["*"] = { "codespell" },
["_"] = { "trim_whitespace", "trim_newlines" },
},
format_on_save = function(bufnr)
if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
return
end
if slow_format_filetypes[vim.bo[bufnr].filetype] then
return
end
local function on_format(err)
if err and err:match("timeout$") then
slow_format_filetypes[vim.bo[bufnr].filetype] = true
end
end
return { timeout_ms = 200, lsp_fallback = true }, on_format
end,
format_after_save = function(bufnr)
if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
return
end
if not slow_format_filetypes[vim.bo[bufnr].filetype] then
return
end
return { lsp_fallback = true }
end,
notify_on_error = true,