Skip to content

Commit

Permalink
feat(ui): show if response body is formatted
Browse files Browse the repository at this point in the history
  • Loading branch information
boltlessengineer committed Sep 3, 2024
1 parent 474f283 commit 3030841
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions doc/rest-nvim-api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ utils.gq_lines({lines}, {filetype}) *utils.gq_lines*

Returns: ~
(string[])
(boolean) Whether formatting is done with `gq`


==============================================================================
Expand Down
15 changes: 12 additions & 3 deletions lua/rest-nvim/ui/result.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ local panes = {
)
)
local content_type = data.response.headers["content-type"]
table.insert(lines, "")
table.insert(lines, "# @_RES")
local body = vim.split(data.response.body, "\n")
local body_meta = {}
if content_type then
local base_type, res_type = content_type[1]:match("(.*)/([^;]+)")
if base_type == "image" then
Expand All @@ -87,9 +86,19 @@ local panes = {
body = { "Binary answer" }
elseif config.response.hooks.format then
-- NOTE: format hook runs here because it should be done last.
body = utils.gq_lines(body, res_type)
local ok
body, ok = utils.gq_lines(body, res_type)
if ok then
table.insert(body_meta, "formatted")
end
end
end
local meta_str = ""
if #body_meta > 0 then
meta_str = " (" .. table.concat(body_meta, ",") .. ")"
end
table.insert(lines, "")
table.insert(lines, "# @_RES" .. meta_str)
vim.list_extend(lines, body)
table.insert(lines, "# @_END")
else
Expand Down
5 changes: 3 additions & 2 deletions lua/rest-nvim/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ end
---@param lines string[]
---@param filetype string
---@return string[]
---@return boolean ok Whether formatting is done with `gq`
function utils.gq_lines(lines, filetype)
logger.debug("formatting with `gq`")
local format_buf = vim.api.nvim_create_buf(false, true)
Expand All @@ -313,12 +314,12 @@ function utils.gq_lines(lines, filetype)
logger.debug(("formatting %s filetype with formatprg=%s"):format(filetype, formatprg))
else
logger.debug(("can't find formatexpr or formatprg for %s filetype. Formatting is canceled"):format(filetype))
return lines
return lines, false
end
vim.api.nvim_buf_call(format_buf, function()
vim.cmd("silent normal gggqG")
end)
return vim.api.nvim_buf_get_lines(format_buf, 0, -1, false)
return vim.api.nvim_buf_get_lines(format_buf, 0, -1, false), true
end

return utils

0 comments on commit 3030841

Please sign in to comment.