Skip to content

Commit 19c7ba1

Browse files
ro0grRuslan Hrabovyi
andauthored
fix: improve error message for unknown formatters (#583)
* 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]>
1 parent 62d5acc commit 19c7ba1

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

lua/conform/health.lua

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,15 @@ M.show_window = function()
101101
---@param formatter conform.FormatterInfo
102102
local function append_formatter_info(formatter)
103103
if not formatter.available then
104-
local line = string.format("%s unavailable: %s", formatter.name, formatter.available_msg)
104+
local type_label = formatter.error and "error" or "unavailable"
105+
106+
local line = string.format("%s %s: %s", formatter.name, type_label, formatter.available_msg)
107+
105108
table.insert(lines, line)
106-
table.insert(
107-
highlights,
108-
{ "DiagnosticWarn", #lines, formatter.name:len(), formatter.name:len() + 12 }
109-
)
109+
110+
local hl = formatter.error and "DiagnosticError" or "DiagnosticWarn"
111+
local hl_start = formatter.name:len() + 1
112+
table.insert(highlights, { hl, #lines, hl_start, hl_start + type_label:len() })
110113
else
111114
local filetypes = get_formatter_filetypes(formatter.name)
112115
local filetypes_list = table.concat(filetypes, ", ")

lua/conform/init.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,8 @@ M.get_formatter_info = function(formatter, bufnr)
782782
name = formatter,
783783
command = formatter,
784784
available = false,
785-
available_msg = "Formatter config missing or incomplete",
785+
available_msg = "Unknown formatter. Formatter config missing or incomplete",
786+
error = true,
786787
}
787788
end
788789

lua/conform/types.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
---@field cwd? string
55
---@field available boolean
66
---@field available_msg? string
7+
---@field error? boolean
78

89
---@class (exact) conform.JobFormatterConfig
910
---@field command string|fun(self: conform.JobFormatterConfig, ctx: conform.Context): string

tests/api_spec.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ describe("api", function()
1212
assert.equal("stylua", info.name)
1313
assert.equal("stylua", info.command)
1414
assert.equal("boolean", type(info.available))
15+
assert.is_nil(info.error)
1516
end)
1617

1718
it("retrieves unavailable info if formatter does not exist", function()
1819
local info = conform.get_formatter_info("asdf")
1920
assert.equal("asdf", info.name)
2021
assert.equal("asdf", info.command)
2122
assert.falsy(info.available)
23+
assert.truthy(info.error)
2224
end)
2325

2426
describe("list_formatters", function()

0 commit comments

Comments
 (0)