diff options
-rw-r--r-- | doc/recipes.md | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/doc/recipes.md b/doc/recipes.md index 9fed51d..467029b 100644 --- a/doc/recipes.md +++ b/doc/recipes.md @@ -134,3 +134,32 @@ end, { desc = "Re-enable autoformat-on-save", }) ``` + +## Automatically run slow formatters async + +This snippet will automatically detect which formatters take too long to run synchronously and will run them async on save instead. + +```lua +local slow_format_filetypes = {} +require("conform").setup({ + format_on_save = function(bufnr) + if slow_format_filetypes[vim.bo[bufnr].filetype] then + return + end + local function on_format(err) + if err and err:match("timed out$") 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 not slow_format_filetypes[vim.bo[bufnr].filetype] then + return + end + return { lsp_fallback = true } + end, +}) +``` |