summaryrefslogtreecommitdiffstats
path: root/lua
diff options
context:
space:
mode:
authorSteven Arcangeli <stevearc@stevearc.com>2024-07-14 10:45:06 -0700
committerSteven Arcangeli <stevearc@stevearc.com>2024-07-19 08:41:31 -0700
commit8c226d917918ffe92e0f30f4e13acfc088a5faa7 (patch)
tree55ccc9be2be96f9fc21ce63a2731718dbfdbaa1e /lua
parentc16c749612fb34a9c1dcc6e4a0f40e24e37d5cfb (diff)
feat: notify when no formatters available for a buffer
Diffstat (limited to 'lua')
-rw-r--r--lua/conform/init.lua23
-rw-r--r--lua/conform/types.lua1
2 files changed, 22 insertions, 2 deletions
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.