Skip to content

Commit

Permalink
fix: handle when format target is empty string (# 473)
Browse files Browse the repository at this point in the history
  • Loading branch information
boltlessengineer committed Sep 28, 2024
1 parent d8ffdea commit c703d54
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lua/rest-nvim/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ end
---@return boolean ok Whether formatting is done with `gq`
function utils.gq_lines(lines, filetype)
logger.debug("formatting with `gq`")
if #lines == 0 then
logger.debug("content is empty. Formatting is canceled")
return lines, true
end
local format_buf = vim.api.nvim_create_buf(false, true)
local ok, errmsg = pcall(vim.api.nvim_set_option_value, "filetype", filetype, { buf = format_buf })
if not ok then
Expand Down
4 changes: 4 additions & 0 deletions spec/utils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,8 @@ describe("gq_lines", function()
"</note>",
}, utils.gq_lines(lines, "xml"))
end)
it("handle empty content #473", function()
local lines = {}
assert.same({}, utils.gq_lines(lines, "json"))
end)
end)

0 comments on commit c703d54

Please sign in to comment.