aboutsummaryrefslogtreecommitdiffstats
path: root/lua/conform/init.lua
diff options
context:
space:
mode:
authorSteven Arcangeli <stevearc@stevearc.com>2023-08-28 08:41:02 -0700
committerSteven Arcangeli <stevearc@stevearc.com>2023-08-28 08:41:02 -0700
commita4d793e941e8e497ab9149ed09c946473d795c1b (patch)
tree909ac23146ce50b0a0cd5a67fc8f54eca4af723a /lua/conform/init.lua
parent27494d493bf8059e94b3074af6f6fa3747d72ca9 (diff)
feat: can silence notification when running formatters (#7)
Diffstat (limited to 'lua/conform/init.lua')
-rw-r--r--lua/conform/init.lua63
1 files changed, 37 insertions, 26 deletions
diff --git a/lua/conform/init.lua b/lua/conform/init.lua
index 7acd879..93d558a 100644
--- a/lua/conform/init.lua
+++ b/lua/conform/init.lua
@@ -87,6 +87,17 @@ M.setup = function(opts)
end
end
+---@param bufnr integer
+---@return boolean
+local function supports_lsp_format(bufnr)
+ for _, client in ipairs(vim.lsp.get_active_clients({ bufnr = bufnr })) do
+ if client.supports_method("textDocument/formatting", { bufnr = bufnr }) then
+ return true
+ end
+ end
+ return false
+end
+
---Format a buffer
---@param opts? table
--- timeout_ms nil|integer Time in milliseconds to block for formatting. Defaults to 1000. No effect if async = true.
@@ -94,14 +105,18 @@ end
--- async nil|boolean If true the method won't block. Defaults to false.
--- formatters nil|string[] List of formatters to run. Defaults to all formatters for the buffer filetype.
--- lsp_fallback nil|boolean Attempt LSP formatting if no formatters are available. Defaults to false.
+--- quiet nil|boolean Don't show any notifications for warnings or failures. Defaults to false.
---@return boolean True if any formatters were attempted
M.format = function(opts)
+ ---@type {timeout_ms: integer, bufnr: integer, async: boolean, lsp_fallback: boolean, quiet: boolean, formatters?: string[]}
opts = vim.tbl_extend("keep", opts or {}, {
timeout_ms = 1000,
bufnr = 0,
async = false,
lsp_fallback = false,
+ quiet = false,
})
+ local log = require("conform.log")
local formatters = {}
if opts.formatters then
@@ -110,44 +125,40 @@ M.format = function(opts)
if info.available then
table.insert(formatters, info)
else
- vim.notify(
- string.format("Formatter '%s' unavailable: %s", info.name, info.available_msg),
- vim.log.levels.WARN
- )
+ if opts.quiet then
+ log.warn("Formatter '%s' unavailable: %s", info.name, info.available_msg)
+ else
+ vim.notify(
+ string.format("Formatter '%s' unavailable: %s", info.name, info.available_msg),
+ vim.log.levels.WARN
+ )
+ end
end
end
else
formatters = M.list_formatters(opts.bufnr)
end
+ local formatter_names = vim.tbl_map(function(f)
+ return f.name
+ end, formatters)
+ log.debug("Running formatters on %s: %s", vim.api.nvim_buf_get_name(opts.bufnr), formatter_names)
+
local any_formatters = not vim.tbl_isempty(formatters)
if any_formatters then
if opts.async then
require("conform.runner").format_async(opts.bufnr, formatters)
else
- require("conform.runner").format_sync(opts.bufnr, formatters, opts.timeout_ms)
+ require("conform.runner").format_sync(opts.bufnr, formatters, opts.timeout_ms, opts.quiet)
end
- end
-
- if not any_formatters then
- if opts.lsp_fallback then
- local supports_lsp_formatting = false
- for _, client in ipairs(vim.lsp.get_active_clients({ bufnr = opts.bufnr })) do
- if client.server_capabilities.documentFormattingProvider then
- supports_lsp_formatting = true
- break
- end
- end
-
- if supports_lsp_formatting then
- local restore = require("conform.util").save_win_positions(opts.bufnr)
- vim.lsp.buf.format(opts)
- if not opts.async then
- restore()
- end
- end
- else
- vim.notify("No formatters found for buffer. See :checkhealth conform", vim.log.levels.WARN)
+ elseif opts.lsp_fallback and supports_lsp_format(opts.bufnr) then
+ log.debug("Running LSP formatter on %s", vim.api.nvim_buf_get_name(opts.bufnr))
+ local restore = require("conform.util").save_win_positions(opts.bufnr)
+ vim.lsp.buf.format(opts)
+ if not opts.async then
+ restore()
end
+ elseif not opts.quiet then
+ vim.notify("No formatters found for buffer. See :checkhealth conform", vim.log.levels.WARN)
end
return any_formatters