Skip to content

Commit

Permalink
feat: use pcall to invoke configured formatters (#65)
Browse files Browse the repository at this point in the history
* feat: use pcall to invoke configured formatters

* test: neodev

* test
  • Loading branch information
linrongbin16 authored Oct 1, 2023
1 parent 664f888 commit 0655d95
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 9 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
12 changes: 8 additions & 4 deletions tests/logger_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ describe("logger", function()
assert_true(true)
end)
it("err", function()
logger.err("err without parameters")
logger.err("err with 1 parameters: %s", "a")
logger.err("err with 2 parameters: %s, %d", "a", 1)
logger.err("err with 3 parameters: %s, %d, %f", "a", 1, 3.12)
local ok1, msg1 = pcall(logger.err, "err without parameters")
local ok2, msg2 = pcall(logger.err, "err with 1 parameters: %s", "a")
assert_false(ok1)
assert_eq(type(msg1), "string")
assert_true(string.len(msg1) > 0)
assert_false(ok2)
assert_eq(type(msg2), "string")
assert_true(string.len(msg2) > 0)
assert_true(true)
end)
end)
Expand Down
122 changes: 122 additions & 0 deletions tests/minimal_neodev_init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
local root = vim.fn.stdpath("data")

-- bootstrap lazy
local lazypath = root .. "/lazy/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
vim.fn.system({
"git",
"clone",
"--filter=blob:none",
"https://github.com/folke/lazy.nvim.git",
lazypath,
})
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
{ -- Initialize language server configuration
"neovim/nvim-lspconfig",
cmd = { "LspInfo", "LspInstall", "LspUninstall" },
event = { "BufReadPost", "BufNewFile" },
dependencies = {
{ "williamboman/mason.nvim", config = true },
{ "folke/neodev.nvim", config = true },
},
config = function()
require("lspconfig")["lua_ls"].setup({
settings = {
Lua = {
diagnostics = {
enable = true,
globals = { "vim" },
},
workspace = {
checkThirdParty = false,
},
},
},
})
end,
},
{
"jose-elias-alvarez/null-ls.nvim",
event = { "BufReadPost", "BufNewFile" },
dependencies = { "nvim-lua/plenary.nvim" },
config = function()
local null_ls = require("null-ls")
null_ls.setup({
sources = { null_ls.builtins.formatting.stylua },
})
end,
},
{
"nvim-lualine/lualine.nvim",
event = "UIEnter",
dependencies = {
-- Lua fork of vim-web-devicons for neovim
{ "nvim-tree/nvim-web-devicons" },
-- A performant lsp progress status for Neovim.
{
"linrongbin16/lsp-progress.nvim",
config = true,
-- dev = true,
-- dir = "~/github/linrongbin16/lsp-progress.nvim",
},
},
config = function(_, opts)
require("lualine").setup(opts)

vim.api.nvim_create_augroup("lualine_augroup", { clear = true })
vim.api.nvim_create_autocmd("User LspProgressStatusUpdated", {
group = "lualine_augroup",
callback = require("lualine").refresh,
})
end,
opts = {
sections = {
lualine_a = { "mode" },
lualine_b = {},
lualine_c = { "filename" },
lualine_x = {
{ -- Setup lsp-progress component
function()
return require("lsp-progress").progress({
max_size = 80,
})
end,
icon = { "", align = "right" },
},
"diagnostics",
},
lualine_y = { "filetype", "encoding", "fileformat" },
lualine_z = { "location" },
},
},
},
}

-- Attach autocmd to enable auto-formatting on save
vim.api.nvim_create_autocmd({ "LspAttach" }, {
callback = function(ev)
-- Apply autocmd if client supports formatting
vim.api.nvim_create_autocmd("BufWritePre", {
buffer = ev.buf,
desc = "Apply Auto-formatting for to document on save",
group = vim.api.nvim_create_augroup("LspFormat." .. ev.buf, {}),
callback = function()
vim.lsp.buf.format({
bufnr = ev.buf,
filter = function(client)
return client.name == "null-ls"
end,
})
end,
})
end,
})

-- Setup lazy.nvim
require("lazy").setup(plugins, {
root = root .. "/lazy/plugins",
})

0 comments on commit 0655d95

Please sign in to comment.