Skip to content

Commit

Permalink
fix: improve error message for unknown formatters (nvim-lua#583)
Browse files Browse the repository at this point in the history
* improve error message for unknown formatters

* distinguish between availability and initialization error

- use `error` instead of `unavailable` message type in case of
  initialization error

- highlight with error color

---------

Co-authored-by: Ruslan Hrabovyi <[email protected]>
  • Loading branch information
2 people authored and F1lipB committed Dec 17, 2024
1 parent 57e7b24 commit 5165845
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
13 changes: 8 additions & 5 deletions lua/conform/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,15 @@ M.show_window = function()
---@param formatter conform.FormatterInfo
local function append_formatter_info(formatter)
if not formatter.available then
local line = string.format("%s unavailable: %s", formatter.name, formatter.available_msg)
local type_label = formatter.error and "error" or "unavailable"

local line = string.format("%s %s: %s", formatter.name, type_label, formatter.available_msg)

table.insert(lines, line)
table.insert(
highlights,
{ "DiagnosticWarn", #lines, formatter.name:len(), formatter.name:len() + 12 }
)

local hl = formatter.error and "DiagnosticError" or "DiagnosticWarn"
local hl_start = formatter.name:len() + 1
table.insert(highlights, { hl, #lines, hl_start, hl_start + type_label:len() })
else
local filetypes = get_formatter_filetypes(formatter.name)
local filetypes_list = table.concat(filetypes, ", ")
Expand Down
3 changes: 2 additions & 1 deletion lua/conform/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,8 @@ M.get_formatter_info = function(formatter, bufnr)
name = formatter,
command = formatter,
available = false,
available_msg = "Formatter config missing or incomplete",
available_msg = "Unknown formatter. Formatter config missing or incomplete",
error = true,
}
end

Expand Down
1 change: 1 addition & 0 deletions lua/conform/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
---@field cwd? string
---@field available boolean
---@field available_msg? string
---@field error? boolean

---@class (exact) conform.JobFormatterConfig
---@field command string|fun(self: conform.JobFormatterConfig, ctx: conform.Context): string
Expand Down
2 changes: 2 additions & 0 deletions tests/api_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ describe("api", function()
assert.equal("stylua", info.name)
assert.equal("stylua", info.command)
assert.equal("boolean", type(info.available))
assert.is_nil(info.error)
end)

it("retrieves unavailable info if formatter does not exist", function()
local info = conform.get_formatter_info("asdf")
assert.equal("asdf", info.name)
assert.equal("asdf", info.command)
assert.falsy(info.available)
assert.truthy(info.error)
end)

describe("list_formatters", function()
Expand Down

0 comments on commit 5165845

Please sign in to comment.