Skip to content

Commit

Permalink
feat: ConformInfo command for debugging formatter status
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Aug 29, 2023
1 parent f133da2 commit 1fd547f
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ body:
required: true
- type: textarea
attributes:
label: "Output of :checkhealth conform"
label: "Output of :ConformInfo"
validations:
required: true
- type: textarea
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ require("conform").setup({

See [conform.format()](#formatopts) for more details about the parameters.

To view configured and available formatters, as well as to see the path to the log file, run `:checkhealth conform`
To view configured and available formatters, as well as to see the path to the log file, run `:ConformInfo`

## Formatters

Expand Down Expand Up @@ -222,7 +222,7 @@ require("conform").setup({
lsp_fallback = true,
timeout_ms = 500,
},
-- Set the log level. Use `:checkhealth conform` to see the location of the log file.
-- Set the log level. Use `:ConformInfo` to see the location of the log file.
log_level = vim.log.levels.ERROR,
-- Define custom formatters here
formatters = {
Expand Down
2 changes: 1 addition & 1 deletion doc/conform.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ OPTIONS *conform-option
lsp_fallback = true,
timeout_ms = 500,
},
-- Set the log level. Use `:checkhealth conform` to see the location of the log file.
-- Set the log level. Use `:ConformInfo` to see the location of the log file.
log_level = vim.log.levels.ERROR,
-- Define custom formatters here
formatters = {
Expand Down
105 changes: 95 additions & 10 deletions lua/conform/health.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
local M = {}

---@param name string
---@return string[]
local function get_formatter_filetypes(name)
local conform = require("conform")
local filetypes = {}
for filetype, formatters in pairs(conform.formatters_by_ft) do
if not vim.tbl_islist(formatters) then
formatters = formatters.formatters
end
if vim.tbl_contains(formatters, name) then
table.insert(filetypes, filetype)
end
end
return filetypes
end

M.check = function()
local conform = require("conform")
vim.health.report_start("conform.nvim report")
Expand All @@ -14,21 +30,90 @@ M.check = function()
string.format("%s unavailable: %s", formatter.name, formatter.available_msg)
)
else
local filetypes = {}
for filetype, formatters in pairs(conform.formatters_by_ft) do
if not vim.tbl_islist(formatters) then
formatters = formatters.formatters
end
if vim.tbl_contains(formatters, formatter.name) then
table.insert(filetypes, filetype)
end
end

local filetypes = get_formatter_filetypes(formatter.name)
vim.health.report_ok(
string.format("%s ready (%s)", formatter.name, table.concat(filetypes, ", "))
)
end
end
end

M.show_window = function()
local conform = require("conform")
local lines = {}
local highlights = {}
local log = require("conform.log")
table.insert(lines, string.format("Log file: %s", log.get_logfile()))
table.insert(lines, "")

---@param formatters conform.FormatterInfo[]
local function append_formatters(formatters)
for _, formatter in ipairs(formatters) do
if not formatter.available then
local line = string.format("%s unavailable: %s", formatter.name, formatter.available_msg)
table.insert(lines, line)
table.insert(
highlights,
{ "DiagnosticWarn", #lines, formatter.name:len(), formatter.name:len() + 12 }
)
else
local filetypes = get_formatter_filetypes(formatter.name)
local line = string.format("%s ready (%s)", formatter.name, table.concat(filetypes, ", "))
table.insert(lines, line)
table.insert(
highlights,
{ "DiagnosticInfo", #lines, formatter.name:len(), formatter.name:len() + 6 }
)
end
end
end

table.insert(lines, "Formatters for this buffer:")
local seen = {}
local buf_formatters = conform.list_formatters_for_buffer()
for _, formatter in ipairs(buf_formatters) do
seen[formatter.name] = true
end
append_formatters(buf_formatters)

table.insert(lines, "")
table.insert(lines, "Other formatters:")
local all_formatters = vim.tbl_filter(function(f)
return not seen[f.name]
end, conform.list_all_formatters())
append_formatters(all_formatters)

local bufnr = vim.api.nvim_create_buf(false, true)
local winid = vim.api.nvim_open_win(bufnr, true, {
relative = "editor",
border = "rounded",
width = vim.o.columns - 6,
height = vim.o.lines - 6,
col = 2,
row = 2,
style = "minimal",
})
vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, lines)
vim.bo[bufnr].modifiable = false
vim.bo[bufnr].modified = false
vim.bo[bufnr].bufhidden = "wipe"
vim.keymap.set("n", "q", "<cmd>close<cr>", { buffer = bufnr })
vim.keymap.set("n", "<C-c>", "<cmd>close<cr>", { buffer = bufnr })
vim.api.nvim_create_autocmd("BufLeave", {
desc = "Close info window when leaving buffer",
buffer = bufnr,
once = true,
nested = true,
callback = function()
if vim.api.nvim_win_is_valid(winid) then
vim.api.nvim_win_close(winid, true)
end
end,
})
local ns = vim.api.nvim_create_namespace("conform")
for _, hl in ipairs(highlights) do
vim.api.nvim_buf_add_highlight(bufnr, ns, hl[1], hl[2] - 1, hl[3], hl[4])
end
end

return M
13 changes: 9 additions & 4 deletions lua/conform/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ M.setup = function(opts)
})
end

vim.api.nvim_create_user_command("ConformInfo", function()
require("conform.health").show_window()
end, { desc = "Show information about Conform formatters" })

---@diagnostic disable-next-line: duplicate-set-field
vim.lsp.handlers["textDocument/formatting"] = function(_, result, ctx, _)
if not result then
Expand All @@ -115,10 +119,11 @@ local function supports_lsp_format(bufnr)
return false
end

---@private
---@param bufnr? integer
---@return conform.FormatterInfo[]
---@return conform.RunOptions
local function list_formatters_for_buffer(bufnr)
M.list_formatters_for_buffer = function(bufnr)
if not bufnr or bufnr == 0 then
bufnr = vim.api.nvim_get_current_buf()
end
Expand Down Expand Up @@ -249,7 +254,7 @@ M.format = function(opts)
end
else
local run_info
formatters, run_info = list_formatters_for_buffer(opts.bufnr)
formatters, run_info = M.list_formatters_for_buffer(opts.bufnr)
any_formatters_configured = not vim.tbl_isempty(formatters)
formatters = filter_formatters(formatters, run_info)
end
Expand Down Expand Up @@ -284,7 +289,7 @@ M.format = function(opts)
restore()
end
elseif any_formatters_configured and not opts.quiet then
vim.notify("No formatters found for buffer. See :checkhealth conform", vim.log.levels.WARN)
vim.notify("No formatters found for buffer. See :ConformInfo", vim.log.levels.WARN)
else
log.debug("No formatters found for %s", vim.api.nvim_buf_get_name(opts.bufnr))
end
Expand All @@ -296,7 +301,7 @@ end
---@param bufnr? integer
---@return conform.FormatterInfo[]
M.list_formatters = function(bufnr)
local formatters, run_options = list_formatters_for_buffer(bufnr)
local formatters, run_options = M.list_formatters_for_buffer(bufnr)
return filter_formatters(formatters, run_options)
end

Expand Down
2 changes: 1 addition & 1 deletion tests/options_doc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require("conform").setup({
lsp_fallback = true,
timeout_ms = 500,
},
-- Set the log level. Use `:checkhealth conform` to see the location of the log file.
-- Set the log level. Use `:ConformInfo` to see the location of the log file.
log_level = vim.log.levels.ERROR,
-- Define custom formatters here
formatters = {
Expand Down

0 comments on commit 1fd547f

Please sign in to comment.