From 9c9193e2389488ba9a0b0ee86fede8aa3ef8f67f Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Fri, 15 Sep 2023 07:25:05 -0700 Subject: doc: recipe for automatically running slow formatters async --- doc/recipes.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'doc') 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, +}) +``` -- cgit v1.2.3-70-g09d2