diff --git a/README.md b/README.md index 45ed7c9e..934e7ce2 100644 --- a/README.md +++ b/README.md @@ -429,6 +429,7 @@ require("conform").formatters.shfmt = { - [Command to toggle format-on-save](doc/recipes.md#command-to-toggle-format-on-save) - [Automatically run slow formatters async](doc/recipes.md#automatically-run-slow-formatters-async) - [Lazy loading with lazy.nvim](doc/recipes.md#lazy-loading-with-lazynvim) +- [Display error message](doc/recipes.md#display-error-message) diff --git a/doc/recipes.md b/doc/recipes.md index f5d6f99a..103b7ef7 100644 --- a/doc/recipes.md +++ b/doc/recipes.md @@ -7,6 +7,7 @@ - [Command to toggle format-on-save](#command-to-toggle-format-on-save) - [Automatically run slow formatters async](#automatically-run-slow-formatters-async) - [Lazy loading with lazy.nvim](#lazy-loading-with-lazynvim) +- [Display error message](#display-error-message) @@ -178,3 +179,51 @@ return { end, } ``` + +## Display error message + +Display formatter output if it fails or explanation if it is unable to run. A message similar to one below will show up on the bottom of the screen (using default `vim.notify`). + +``` +Formatter 'rustfmt' error: error: expected `;`, found `bar` + --> :2:10 + | +2 | foo() + | ^ help: add `;` here +3 | bar() + | --- unexpected token + +Press ENTER or type command to continue +``` + +```lua +local function notify_on_error(err) + if err then + vim.notify(err, vim.log.levels.WARN) + end +end + +-- Show error message in manual invocation +vim.keymap.set("f", function() + local format_opts = { + -- ... + } + require("conform").format(format_opts, notify_on_error) +end, { desc = "Format buffer" }) + +-- Show error message on/after save +require("conform").setup({ + format_on_save = function(bufnr) + local format_opts = { + -- ... + } + return format_opts, notify_on_error + end, + format_after_save = function(bufnr) + local format_opts = { + -- ... + } + return format_opts, notify_on_error + end, +}) +```