From c703d54440c90e4fbecda3748c6286551d57eab0 Mon Sep 17 00:00:00 2001 From: Seongmin Lee Date: Sat, 28 Sep 2024 17:49:13 +0000 Subject: [PATCH] fix: handle when format target is empty string (# 473) --- lua/rest-nvim/utils.lua | 4 ++++ spec/utils_spec.lua | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/lua/rest-nvim/utils.lua b/lua/rest-nvim/utils.lua index be7de15..d97a860 100644 --- a/lua/rest-nvim/utils.lua +++ b/lua/rest-nvim/utils.lua @@ -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 diff --git a/spec/utils_spec.lua b/spec/utils_spec.lua index beb71b6..92380b5 100644 --- a/spec/utils_spec.lua +++ b/spec/utils_spec.lua @@ -85,4 +85,8 @@ describe("gq_lines", function() "", }, utils.gq_lines(lines, "xml")) end) + it("handle empty content #473", function() + local lines = {} + assert.same({}, utils.gq_lines(lines, "json")) + end) end) -- 2.51.2