Skip to content

Commit 1741ed8

Browse files
feat: remove external formatter, use gq instead
1 parent 9f339bc commit 1741ed8

File tree

8 files changed

+26
-152
lines changed

8 files changed

+26
-152
lines changed

README.md

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ CLI. For more information on this, please see this [blog post](https://amartin.c
3737
- Set custom pre-request and post-request hooks to dynamically interact with the data
3838
- Easily set environment variables based on the response to re-use the data later
3939
- Tree-sitter based parsing and syntax highlighting for speed and perfect accuracy
40+
- Format response body with native `gq` command
4041
- Possibility of using dynamic/environment variables and Lua scripting in HTTP files
4142
- Save received cookies and load them automatically
4243

@@ -48,11 +49,7 @@ CLI. For more information on this, please see this [blog post](https://amartin.c
4849
4950
### Dependencies
5051

51-
- System-wide
52-
- `curl`
53-
- Optional [can be changed, see config below](#default-configuration)
54-
- `jq` (to format JSON output)
55-
- `tidy` (to format HTML output)
52+
- `curl`
5653

5754
### [rocks.nvim](https://github.com/nvim-neorocks/rocks.nvim) (recommended)
5855

@@ -109,31 +106,9 @@ local default_config = {
109106
hooks = {
110107
---@type boolean Decode the request URL segments on response UI to improve readability
111108
decode_url = true,
112-
---@type boolean Format the response body
109+
---@type boolean Format the response body using `gq` command
113110
format = true,
114111
},
115-
---@type table<string,RestResultFormatter>
116-
formatters = {
117-
json = "jq",
118-
html = function(body)
119-
if vim.fn.executable("tidy") == 0 then
120-
return body, { found = false, name = "tidy" }
121-
end
122-
-- stylua: ignore
123-
local fmt_body = vim.fn.system({
124-
"tidy",
125-
"-i",
126-
"-q",
127-
"--tidy-mark", "no",
128-
"--show-body-only", "auto",
129-
"--show-errors", "0",
130-
"--show-warnings", "0",
131-
"-",
132-
}, body):gsub("\n$", "")
133-
134-
return fmt_body, { found = true, name = "tidy" }
135-
end,
136-
},
137112
},
138113
---@class rest.Config.Clients
139114
clients = {
@@ -160,7 +135,7 @@ local default_config = {
160135
---@type boolean
161136
enable = true,
162137
---@type string
163-
pattern = "%.env.*"
138+
pattern = ".*%.env.*"
164139
},
165140
---@class rest.Config.UI
166141
ui = {

lua/rest-nvim/config/check.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ function check.validate(cfg)
3030
["request.hooks.encode_url"] = { cfg.request.hooks.encode_url, "boolean" },
3131
response = { cfg.response, "table" },
3232
["response.hooks"] = { cfg.response.hooks, "table" },
33-
["response.formatters"] = { cfg.response.formatters, "table" },
3433
clients = { cfg.clients, "table" },
3534
["clients.curl"] = { cfg.clients.curl, "table" },
3635
["clients.curl.statistics"] = { cfg.clients.curl.statistics, "table" },

lua/rest-nvim/config/init.lua

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
---@type rest.Config
1717
local config
1818

19-
---@alias RestResultFormatter string|fun(body:string):string,table
20-
2119
---@class RestStatisticsStyle
2220
--- Title used on Statistics pane
2321
---@field title? string
@@ -51,8 +49,6 @@ local config
5149
---@class rest.Opts.Response
5250
--- Default response hooks (aka. request handlers) configuration
5351
---@field hooks? rest.Opts.Response.Hooks
54-
--- Formatters used for response format hook
55-
---@field formatters? table<string,RestResultFormatter>
5652

5753
---@class rest.Opts.Response.Hooks
5854
--- Decode url segments on response UI too improve readability (Default: `true`)
@@ -125,31 +121,9 @@ local default_config = {
125121
hooks = {
126122
---@type boolean Decode the request URL segments on response UI to improve readability
127123
decode_url = true,
128-
---@type boolean Format the response body
124+
---@type boolean Format the response body using `gq` command
129125
format = true,
130126
},
131-
---@type table<string,RestResultFormatter>
132-
formatters = {
133-
json = "jq",
134-
html = function(body)
135-
if vim.fn.executable("tidy") == 0 then
136-
return body, { found = false, name = "tidy" }
137-
end
138-
-- stylua: ignore
139-
local fmt_body = vim.fn.system({
140-
"tidy",
141-
"-i",
142-
"-q",
143-
"--tidy-mark", "no",
144-
"--show-body-only", "auto",
145-
"--show-errors", "0",
146-
"--show-warnings", "0",
147-
"-",
148-
}, body):gsub("\n$", "")
149-
150-
return fmt_body, { found = true, name = "tidy" }
151-
end,
152-
},
153127
},
154128
---@class rest.Config.Clients
155129
clients = {

lua/rest-nvim/health.lua

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -64,41 +64,6 @@ local function configuration_health()
6464
else
6565
vim.health.ok("No unrecognized configuration options were found")
6666
end
67-
68-
-- Formatters
69-
local formatters = config.response.formatters
70-
for ft, formatter in pairs(formatters) do
71-
if type(formatter) == "string" then
72-
if vim.fn.executable(formatter) ~= 1 then
73-
vim.health.warn(
74-
"Formatter for `"
75-
.. ft
76-
.. "` is set to `"
77-
.. formatter
78-
.. "`, however, rest.nvim could not find it in your system"
79-
)
80-
else
81-
vim.health.ok(
82-
"Formatter for `" .. ft .. "` is set to `" .. formatter .. "` and rest.nvim found it in your system"
83-
)
84-
end
85-
elseif type(formatter) == "function" then
86-
local _, fmt_meta = formatter("")
87-
if not fmt_meta.found then
88-
vim.health.warn(
89-
"Formatter for `"
90-
.. ft
91-
.. "` is set to `"
92-
.. fmt_meta.name
93-
.. "`, however, rest.nvim could not find it in your system"
94-
)
95-
else
96-
vim.health.ok(
97-
"Formatter for `" .. ft .. "` is set to `" .. fmt_meta.name .. "` and rest.nvim found it in your system"
98-
)
99-
end
100-
end
101-
end
10267
end
10368

10469
function health.check()

lua/rest-nvim/response.lua

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
local response = {}
22

3-
local config = require("rest-nvim.config")
4-
local logger = require("rest-nvim.logger")
5-
63
---@class rest.Response
74
---@field status rest.Response.status
85
---@field body string?
@@ -14,51 +11,4 @@ local logger = require("rest-nvim.logger")
1411
---@field version string
1512
---@field text string
1613

17-
---Try format the result body
18-
---@param content_type string?
19-
---@param body string
20-
---@return string[]
21-
function response.try_format_body(content_type, body)
22-
---@type string
23-
local res_type
24-
if content_type then
25-
res_type = vim.split(content_type, "/")[2]:gsub(";.*", "")
26-
end
27-
if res_type == "octet-stream" then
28-
return { "Binary answer" }
29-
else
30-
local formatters = config.response.formatters
31-
local fmt = formatters[res_type]
32-
if fmt then
33-
if type(fmt) == "function" then
34-
local ok, out = pcall(fmt, body)
35-
if ok and out then
36-
body = out
37-
else
38-
logger.error("Error calling formatter on response body:\n" .. out)
39-
end
40-
elseif vim.fn.executable(fmt[1] or fmt) then
41-
local cmd = type(fmt) == "string" and { fmt } or fmt
42-
---@cast cmd string[]
43-
local sc = vim.system(cmd, { stdin = body }):wait()
44-
if sc.code == 0 and sc.stdout then
45-
body = sc.stdout --[[@as string]]
46-
else
47-
logger.error("Error running formatter '" .. fmt .. "' on response body:\n" .. sc.stderr)
48-
vim.notify("[rest.nvim] Formatting response body failed. See `:Rest logs` for more info", vim.log.levels.ERROR)
49-
end
50-
end
51-
elseif res_type ~= nil then
52-
local msg = (
53-
"Could not find a formatter for the body type "
54-
.. res_type
55-
.. " returned in the request, the results will not be formatted"
56-
)
57-
logger.info(msg)
58-
vim.notify(msg, vim.log.levels.INFO)
59-
end
60-
return vim.split(body, "\n", {trimempty = res_type == "json"})
61-
end
62-
end
63-
6414
return response

lua/rest-nvim/ui/result.lua

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ local ui = {}
1111
local config = require("rest-nvim.config")
1212
local utils = require("rest-nvim.utils")
1313
local paneui = require("rest-nvim.ui.panes")
14-
local res = require("rest-nvim.response")
1514

1615
local function set_lines(buffer, lines)
1716
vim.api.nvim_buf_set_lines(buffer, 0, -1, false, lines)
@@ -70,12 +69,12 @@ local panes = {
7069
local content_type = data.response.headers["content-type"]
7170
table.insert(lines, "")
7271
table.insert(lines, "#+RES")
73-
---@type string[]
74-
local body
75-
if config.response.hooks.format then
76-
body = res.try_format_body(content_type and content_type[1], data.response.body)
77-
else
78-
body = vim.split(data.response.body, "\n")
72+
local body = vim.split(data.response.body, "\n")
73+
local res_type = content_type[1]:match(".*/([^;]+)")
74+
if res_type == "octet_stream" then
75+
body = { "Binary answer" }
76+
elseif config.response.hooks.format then
77+
body = utils.gq_lines(body, res_type)
7978
end
8079
vim.list_extend(lines, body)
8180
table.insert(lines, "#+END")

lua/rest-nvim/utils.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,19 @@ function utils.nvim_lazy_set_wo(bufnr, name, value)
276276
})
277277
end
278278

279+
---format lines using native vim `gq` command
280+
---@param lines string[]
281+
---@param filetype string
282+
---@return string[]
283+
function utils.gq_lines(lines, filetype)
284+
local format_buf = vim.api.nvim_create_buf(false, true)
285+
vim.bo[format_buf].filetype = filetype
286+
vim.api.nvim_buf_set_lines(format_buf, 0, -1, false, lines)
287+
vim.api.nvim_buf_call(format_buf, function ()
288+
vim.cmd("normal gg")
289+
vim.cmd("normal gqG")
290+
end)
291+
return vim.api.nvim_buf_get_lines(format_buf, 0, -1, false)
292+
end
293+
279294
return utils

spec/minimum_init.lua

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ vim.g.rest_nvim = {
1616
cookies = {
1717
path = "/tmp/rest-nvim.cookies"
1818
},
19-
response = {
20-
formatters = {}
21-
},
2219
}
2320
---@diagnostic disable-next-line: undefined-field
2421
vim.uv.fs_unlink(vim.g.rest_nvim.cookies.path)

0 commit comments

Comments
 (0)