Skip to content

Commit

Permalink
fix: crash in error handling for function formatters (nvim-lua#554)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc authored and F1lipB committed Dec 17, 2024
1 parent c0c1a60 commit a4d9ca3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
5 changes: 5 additions & 0 deletions lua/conform/formatters/injected.lua
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ return {
-- (defaults to the value from formatters_by_ft)
lang_to_formatters = {},
},
condition = function(self, ctx)
local buf_lang = vim.treesitter.language.get_lang(vim.bo[ctx.buf].filetype)
local ok = pcall(vim.treesitter.get_string_parser, "", buf_lang)
return ok
end,
format = function(self, ctx, lines, callback)
local conform = require("conform")
local errors = require("conform.errors")
Expand Down
17 changes: 12 additions & 5 deletions lua/conform/runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,20 @@ local function run_formatter(bufnr, formatter, config, ctx, input_lines, opts, c
end
end)
if config.format then
local err_string_cb = function(err, ...)
if err then
callback({
code = errors.ERROR_CODE.RUNTIME,
message = err,
}, ...)
else
callback(nil, ...)
end
end
---@cast config conform.LuaFormatterConfig
local ok, err = pcall(config.format, config, ctx, input_lines, callback)
local ok, err = pcall(config.format, config, ctx, input_lines, err_string_cb)
if not ok then
callback({
code = errors.ERROR_CODE.RUNTIME,
message = string.format("Formatter '%s' error: %s", formatter.name, err),
})
err_string_cb(string.format("Formatter '%s' error: %s", formatter.name, err))
end
return
end
Expand Down

0 comments on commit a4d9ca3

Please sign in to comment.