Skip to content

Commit 3030841

Browse files
feat(ui): show if response body is formatted
1 parent 474f283 commit 3030841

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

doc/rest-nvim-api.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ utils.gq_lines({lines}, {filetype}) *utils.gq_lines*
372372

373373
Returns: ~
374374
(string[])
375+
(boolean) Whether formatting is done with `gq`
375376

376377

377378
==============================================================================

lua/rest-nvim/ui/result.lua

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,8 @@ local panes = {
7676
)
7777
)
7878
local content_type = data.response.headers["content-type"]
79-
table.insert(lines, "")
80-
table.insert(lines, "# @_RES")
8179
local body = vim.split(data.response.body, "\n")
80+
local body_meta = {}
8281
if content_type then
8382
local base_type, res_type = content_type[1]:match("(.*)/([^;]+)")
8483
if base_type == "image" then
@@ -87,9 +86,19 @@ local panes = {
8786
body = { "Binary answer" }
8887
elseif config.response.hooks.format then
8988
-- NOTE: format hook runs here because it should be done last.
90-
body = utils.gq_lines(body, res_type)
89+
local ok
90+
body, ok = utils.gq_lines(body, res_type)
91+
if ok then
92+
table.insert(body_meta, "formatted")
93+
end
9194
end
9295
end
96+
local meta_str = ""
97+
if #body_meta > 0 then
98+
meta_str = " (" .. table.concat(body_meta, ",") .. ")"
99+
end
100+
table.insert(lines, "")
101+
table.insert(lines, "# @_RES" .. meta_str)
93102
vim.list_extend(lines, body)
94103
table.insert(lines, "# @_END")
95104
else

lua/rest-nvim/utils.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ end
288288
---@param lines string[]
289289
---@param filetype string
290290
---@return string[]
291+
---@return boolean ok Whether formatting is done with `gq`
291292
function utils.gq_lines(lines, filetype)
292293
logger.debug("formatting with `gq`")
293294
local format_buf = vim.api.nvim_create_buf(false, true)
@@ -313,12 +314,12 @@ function utils.gq_lines(lines, filetype)
313314
logger.debug(("formatting %s filetype with formatprg=%s"):format(filetype, formatprg))
314315
else
315316
logger.debug(("can't find formatexpr or formatprg for %s filetype. Formatting is canceled"):format(filetype))
316-
return lines
317+
return lines, false
317318
end
318319
vim.api.nvim_buf_call(format_buf, function()
319320
vim.cmd("silent normal gggqG")
320321
end)
321-
return vim.api.nvim_buf_get_lines(format_buf, 0, -1, false)
322+
return vim.api.nvim_buf_get_lines(format_buf, 0, -1, false), true
322323
end
323324

324325
return utils

0 commit comments

Comments
 (0)