Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lua/rest-nvim/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -345,18 +345,22 @@ function utils.gq_lines(lines, filetype)
logger.debug(("can't find formatexpr or formatprg for %s filetype. Formatting is canceled"):format(filetype))
return lines, false
end
local result = true
vim.api.nvim_buf_call(format_buf, function()
-- HACK: dirty fix for neovim/neovim#30593
local gq_ok, res = pcall(vim.api.nvim_command, "silent normal gggqG")
if not gq_ok then
local msg = ("formatting %s filetype failed"):format(filetype)
local shell_error = vim.v.shell_error
if not gq_ok or shell_error ~= 0 then
local msg = ("formatting %s filetype failed (shell_error=%d)"):format(filetype, shell_error)
logger.warn(msg, res)
vim.notify(msg, vim.log.levels.WARN, { title = "rest.nvim" })
result = false
end
end)
local buf_lines = vim.api.nvim_buf_get_lines(format_buf, 0, -1, false)
vim.api.nvim_buf_delete(format_buf, { force = true })
return buf_lines, true
-- Return the unmodified lines if result is failed
return result and buf_lines or lines, result
end

return utils
12 changes: 12 additions & 0 deletions spec/utils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,16 @@ describe("gq_lines", function()
local lines = { "" }
assert.same({ "" }, utils.gq_lines(lines, "json"))
end)
it("returns original lines when json parsing fails", function()
vim.api.nvim_create_autocmd("FileType", {
pattern = "json",
callback = function(ev)
vim.bo[ev.buf].formatprg = "jq --indent 4"
end,
})
local lines = { "this is not valid json {{{" }
local result, ok = utils.gq_lines(lines, "json")
assert.is_false(ok)
assert.same(lines, result)
end)
end)