diff options
-rw-r--r-- | README.md | 3 | ||||
-rw-r--r-- | doc/conform.txt | 5 | ||||
-rw-r--r-- | lua/conform/init.lua | 23 | ||||
-rw-r--r-- | lua/conform/types.lua | 1 | ||||
-rw-r--r-- | scripts/options_doc.lua | 2 |
5 files changed, 32 insertions, 2 deletions
@@ -498,6 +498,8 @@ require("conform").setup({ log_level = vim.log.levels.ERROR, -- Conform will notify you when a formatter errors notify_on_error = true, + -- Conform will notify you when no formatters are available for the buffer + notify_no_formatters = true, -- Custom formatters and overrides for built-in formatters formatters = { my_formatter = { @@ -587,6 +589,7 @@ require("conform").formatters.my_formatter = { | | format_after_save | `nil\|conform.FormatOpts\|fun(bufnr: integer): nil\|conform.FormatOpts` | If this is set, Conform will run the formatter asynchronously after save. It will pass the table to conform.format(). This can also be a function that returns the table. | | | log_level | `nil\|integer` | Set the log level (e.g. `vim.log.levels.DEBUG`). Use `:ConformInfo` to see the location of the log file. | | | notify_on_error | `nil\|boolean` | Conform will notify you when a formatter errors (default true). | +| | notify_no_formatters | `nil\|boolean` | Conform will notify you when no formatters are available for the buffer (default true). | | | formatters | `nil\|table<string, conform.FormatterConfigOverride\|fun(bufnr: integer): nil\|conform.FormatterConfigOverride>` | Custom formatters and overrides for built-in formatters. | ### format(opts, callback) diff --git a/doc/conform.txt b/doc/conform.txt index 89b5468..8d2fa3e 100644 --- a/doc/conform.txt +++ b/doc/conform.txt @@ -56,6 +56,8 @@ OPTIONS *conform-option log_level = vim.log.levels.ERROR, -- Conform will notify you when a formatter errors notify_on_error = true, + -- Conform will notify you when no formatters are available for the buffer + notify_no_formatters = true, -- Custom formatters and overrides for built-in formatters formatters = { my_formatter = { @@ -158,6 +160,9 @@ setup({opts}) *conform.setu the location of the log file. {notify_on_error} `nil|boolean` Conform will notify you when a formatter errors (default true). + {notify_no_formatters} `nil|boolean` Conform will notify you when no + formatters are available for the buffer (default + true). {formatters} `nil|table<string, conform.FormatterConfigOverride|fun(bufnr: integer): nil|conform.FormatterConfigOverride>` Custom formatters and overrides for built-in formatters. diff --git a/lua/conform/init.lua b/lua/conform/init.lua index da55067..1a648f0 100644 --- a/lua/conform/init.lua +++ b/lua/conform/init.lua @@ -9,6 +9,7 @@ M.formatters_by_ft = {} M.formatters = {} M.notify_on_error = true +M.notify_no_formatters = true ---@type conform.DefaultFormatOpts M.default_format_opts = {} @@ -38,6 +39,10 @@ M.setup = function(opts) if notify_on_error ~= nil then M.notify_on_error = notify_on_error end + local notify_no_formatters = opts.notify_no_formatters + if notify_no_formatters ~= nil then + M.notify_no_formatters = notify_no_formatters + end local aug = vim.api.nvim_create_augroup("Conform", { clear = true }) if opts.format_on_save then @@ -370,6 +375,8 @@ local function has_lsp_formatter(opts) return not vim.tbl_isempty(lsp_format.get_format_clients(opts)) end +local has_notified_ft_no_formatters = {} + ---Format a buffer ---@param opts? conform.FormatOpts ---@param callback? fun(err: nil|string, did_edit: nil|boolean) Called once formatting has completed @@ -504,8 +511,20 @@ M.format = function(opts, callback) return true else local level = has_explicit_formatters and "warn" or "debug" - log[level]("No formatters found for %s", vim.api.nvim_buf_get_name(opts.bufnr)) - callback("No formatters found for buffer") + log[level]("Formatters unavailable for %s", vim.api.nvim_buf_get_name(opts.bufnr)) + + local ft = vim.bo[opts.bufnr].filetype + if + not vim.tbl_isempty(formatter_names) + and not has_notified_ft_no_formatters[ft] + and not opts.quiet + and M.notify_no_formatters + then + notify(string.format("Formatters unavailable for %s file", ft), vim.log.levels.WARN) + has_notified_ft_no_formatters[ft] = true + end + + callback("No formatters available for buffer") return false end end diff --git a/lua/conform/types.lua b/lua/conform/types.lua index 007e84a..a4b8cc0 100644 --- a/lua/conform/types.lua +++ b/lua/conform/types.lua @@ -106,4 +106,5 @@ ---@field format_after_save? conform.FormatOpts|fun(bufnr: integer): nil|conform.FormatOpts If this is set, Conform will run the formatter asynchronously after save. It will pass the table to conform.format(). This can also be a function that returns the table. ---@field log_level? integer Set the log level (e.g. `vim.log.levels.DEBUG`). Use `:ConformInfo` to see the location of the log file. ---@field notify_on_error? boolean Conform will notify you when a formatter errors (default true). +---@field notify_no_formatters? boolean Conform will notify you when no formatters are available for the buffer (default true). ---@field formatters? table<string, conform.FormatterConfigOverride|fun(bufnr: integer): nil|conform.FormatterConfigOverride> Custom formatters and overrides for built-in formatters. diff --git a/scripts/options_doc.lua b/scripts/options_doc.lua index 17734d3..62ba104 100644 --- a/scripts/options_doc.lua +++ b/scripts/options_doc.lua @@ -43,6 +43,8 @@ require("conform").setup({ log_level = vim.log.levels.ERROR, -- Conform will notify you when a formatter errors notify_on_error = true, + -- Conform will notify you when no formatters are available for the buffer + notify_no_formatters = true, -- Custom formatters and overrides for built-in formatters formatters = { my_formatter = { |