Skip to content

Commit

Permalink
feat: use pcall to invoke configured formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
linrongbin16 committed Oct 1, 2023
1 parent 664f888 commit 9d29bfc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
10 changes: 9 additions & 1 deletion lua/lsp-progress/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,19 @@ function ClientObject:format()
assert(Spinner ~= nil, "Spinner cannot be nil")
assert(#Spinner > 0, "Spinner length cannot be 0")
assert(ClientFormatter ~= nil, "ClientFormatter cannot be null")
self._format_cache = ClientFormatter(
local ok, result = pcall(ClientFormatter,
self.client_name,
Spinner[self.spin_index + 1],
series_messages
)

if not ok then
logger.err("failed to invoke 'client_format'! error: %s, params: %s, %s, %s", vim.inspect(result),
vim.inspect(self.client_name),
vim.inspect(Spinner[self.spin_index + 1]), vim.inspect(series_messages))
end
self._format_cache = result

logger.debug(
"|client.format| format client %s: %s",
self:tostring(),
Expand Down
5 changes: 3 additions & 2 deletions lua/lsp-progress/logger.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local PATH_SEPARATOR = (vim.fn.has("win32") > 0 or vim.fn.has("win64") > 0)
and "\\"
and "\\"
or "/"

local LogLevels = {
Expand Down Expand Up @@ -112,6 +112,7 @@ end
--- @param ... any
local function err(fmt, ...)
log(LogLevels.ERROR, string.format(fmt, ...))
error(string.format(fmt, ...))
end

local M = {
Expand All @@ -122,4 +123,4 @@ local M = {
err = err,
}

return M
return M
11 changes: 9 additions & 2 deletions lua/lsp-progress/series.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,15 @@ end
--- @return SeriesFormatResult
function SeriesObject:_format()
assert(SeriesFormatter ~= nil, "SeriesFormatter cannot be null")
self._format_cache =
SeriesFormatter(self.title, self.message, self.percentage, self.done)
local ok, result =
pcall(SeriesFormatter, self.title, self.message, self.percentage, self.done)

if not ok then
logger.err("failed to invoke 'series_format' function! error: %s, params: %s, %s, %s, %s", vim.inspect(result),
vim.inspect(self.title), vim.inspect(self.message), vim.inspect(self.percentage), vim.inspect(self.done))
end

self._format_cache = result
logger.debug("|series._format| Format series: %s", self:tostring())
return self._format_cache
end
Expand Down

0 comments on commit 9d29bfc

Please sign in to comment.