Skip to content

Commit be234c8

Browse files
fix(utils): safely call :normal gggqG (fix #478)
1 parent e9f9237 commit be234c8

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lua/rest-nvim/utils.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,13 @@ function utils.gq_lines(lines, filetype)
327327
return lines, false
328328
end
329329
vim.api.nvim_buf_call(format_buf, function()
330-
vim.cmd("silent normal gggqG")
330+
-- HACK: dirty fix for neovim/neovim#30593
331+
local gq_ok, res = pcall(vim.api.nvim_command, "silent normal gggqG")
332+
if not gq_ok then
333+
local msg = ("formatting %s filetype failed"):format(filetype)
334+
logger.warn(msg, res)
335+
vim.notify(msg, vim.log.levels.WARN, { title = "rest.nvim" })
336+
end
331337
end)
332338
local buf_lines = vim.api.nvim_buf_get_lines(format_buf, 0, -1, false)
333339
vim.api.nvim_buf_delete(format_buf, { force = true })

spec/utils_spec.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,14 @@ describe("gq_lines", function()
8989
local lines = {}
9090
assert.same({}, utils.gq_lines(lines, "json"))
9191
end)
92+
it("handle format error #478 (neovim/neovim#30593)", function()
93+
vim.api.nvim_create_autocmd("FileType", {
94+
pattern = "json",
95+
callback = function(ev)
96+
vim.bo[ev.buf].formatprg = "jq --indent 4"
97+
end,
98+
})
99+
local lines = { "" }
100+
assert.same({ "" }, utils.gq_lines(lines, "json"))
101+
end)
92102
end)

0 commit comments

Comments
 (0)