Skip to content

Commit c703d54

Browse files
fix: handle when format target is empty string (# 473)
1 parent d8ffdea commit c703d54

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

lua/rest-nvim/utils.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,10 @@ end
291291
---@return boolean ok Whether formatting is done with `gq`
292292
function utils.gq_lines(lines, filetype)
293293
logger.debug("formatting with `gq`")
294+
if #lines == 0 then
295+
logger.debug("content is empty. Formatting is canceled")
296+
return lines, true
297+
end
294298
local format_buf = vim.api.nvim_create_buf(false, true)
295299
local ok, errmsg = pcall(vim.api.nvim_set_option_value, "filetype", filetype, { buf = format_buf })
296300
if not ok then

spec/utils_spec.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,8 @@ describe("gq_lines", function()
8585
"</note>",
8686
}, utils.gq_lines(lines, "xml"))
8787
end)
88+
it("handle empty content #473", function()
89+
local lines = {}
90+
assert.same({}, utils.gq_lines(lines, "json"))
91+
end)
8892
end)

0 commit comments

Comments
 (0)