Skip to content

Commit 0213ffb

Browse files
refactor: cleanup
1 parent b1ac96b commit 0213ffb

File tree

4 files changed

+80
-126
lines changed

4 files changed

+80
-126
lines changed

lua/rest-nvim/commands.lua

Lines changed: 10 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,14 @@ local rest_command_tbl = {
9696
if #args > 1 then
9797
vim.notify("Running multiple request isn't supported yet", vim.log.levels.WARN, { title = "rest.nvim" })
9898
return
99-
elseif #args == 1 then
100-
request().run_by_name(args[1])
101-
return
10299
end
103100
ui().clear()
104101
if not ui().is_open() then
105102
vim.cmd.wincmd("v")
106103
ui().enter(0)
107104
vim.cmd.wincmd("p")
108105
end
109-
request().run()
106+
request().run(args[1])
110107
end,
111108
---@return string[]
112109
complete = function(args)
@@ -200,30 +197,10 @@ local rest_command_tbl = {
200197
-- TODO(boltless): complete curl command
201198
curl = {
202199
impl = function(args, _)
203-
if args[1] == "yank" then
204-
local req_node
205-
if not args[2] then
206-
req_node = parser().get_cursor_request_node()
207-
if not req_node then
208-
logger().error("failed to find request at cursor position")
209-
vim.notify(
210-
"failed to find request at cursor position",
211-
vim.log.levels.ERROR,
212-
{ title = "rest.nvim" }
213-
)
214-
return
215-
end
216-
else
217-
req_node = parser().get_request_node_by_name(args[2])
218-
if not req_node then
219-
logger().error("failed to find request with name:" .. args[2])
220-
vim.notify(
221-
"failed to find request with name:" .. args[2],
222-
vim.log.levels.ERROR,
223-
{ title = "rest.nvim" }
224-
)
225-
return
226-
end
200+
if args[1] == "yank" or args[1] == "copy" then
201+
local req_node = parser().get_request_node(args[2])
202+
if not req_node then
203+
return
227204
end
228205
local req = parser().parse(req_node, 0)
229206
if not req then
@@ -236,32 +213,12 @@ local rest_command_tbl = {
236213
return
237214
end
238215
local curl_command = require("rest-nvim.client.curl.cli").builder.build_command(req)
239-
vim.fn.setreg("+", curl_command)
216+
vim.fn.setreg("+", curl_command .. "\n")
240217
vim.notify("Copied curl command to clipboard", vim.log.levels.INFO, { title = "rest.nvim" })
241218
elseif args[1] == "comment" then
242-
local req_node
243-
if not args[2] then
244-
req_node = parser().get_cursor_request_node()
245-
if not req_node then
246-
logger().error("failed to find request at cursor position")
247-
vim.notify(
248-
"failed to find request at cursor position",
249-
vim.log.levels.ERROR,
250-
{ title = "rest.nvim" }
251-
)
252-
return
253-
end
254-
else
255-
req_node = parser().get_request_node_by_name(args[2])
256-
if not req_node then
257-
logger().error("failed to find request with name:" .. args[2])
258-
vim.notify(
259-
"failed to find request with name:" .. args[2],
260-
vim.log.levels.ERROR,
261-
{ title = "rest.nvim" }
262-
)
263-
return
264-
end
219+
local req_node = parser().get_request_node(args[2])
220+
if not req_node then
221+
return
265222
end
266223
local req = parser().parse(req_node, 0)
267224
if not req then
@@ -275,11 +232,10 @@ local rest_command_tbl = {
275232
end
276233
local curl_command = require("rest-nvim.client.curl.cli").builder.build_command(req)
277234
local start = req_node:range()
278-
local end_ = start
279235
vim.api.nvim_buf_set_lines(
280236
0,
281237
start,
282-
end_,
238+
start,
283239
false,
284240
vim.tbl_map(function(line)
285241
return "# " .. line

lua/rest-nvim/parser/init.lua

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ function parser.eval_context(source, ctx, endline)
200200
end
201201

202202
---@return TSNode? node TSNode with type `section`
203-
function parser.get_cursor_request_node()
203+
function parser.get_request_node_by_cursor()
204204
local node = vim.treesitter.get_node()
205205
if node then
206206
node = utils.ts_find(node, "section")
@@ -320,6 +320,36 @@ function parser.get_request_names(source)
320320
return result
321321
end
322322

323+
---@param name string|nil
324+
---@return TSNode?
325+
function parser.get_request_node(name)
326+
local req_node
327+
if not name then
328+
req_node = parser.get_request_node_by_cursor()
329+
if not req_node then
330+
logger.error("Failed to find request at cursor position")
331+
vim.notify(
332+
"Failed to find request at cursor position. See `:Rest logs` for more info.",
333+
vim.log.levels.ERROR,
334+
{ title = "rest.nvim" }
335+
)
336+
return
337+
end
338+
else
339+
req_node = parser.get_request_node_by_name(name)
340+
if not req_node then
341+
logger.error("Failed to find request by name: " .. name)
342+
vim.notify(
343+
"Failed to find request by name: " .. name .. ". See `:Rest logs` for more info.",
344+
vim.log.levels.ERROR,
345+
{ title = "rest.nvim" }
346+
)
347+
return
348+
end
349+
end
350+
return req_node
351+
end
352+
323353
---Parse the request node and create Request object. Returns `nil` if parsing
324354
---failed.
325355
---@param node TSNode Tree-sitter request node
@@ -348,6 +378,7 @@ function parser.parse(node, source, ctx)
348378
-- first
349379
local url
350380

381+
---@type string|nil
351382
local name
352383
local handlers = {}
353384
for child, _ in node:iter_children() do

lua/rest-nvim/request.lua

Lines changed: 35 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ local Context = require("rest-nvim.context").Context
1717
---@field data any
1818

1919
---@class rest.Request
20-
---@field name? string The request identifier
20+
---The request identifier (`nil` on anonymous requests. e.g. parsed from raw string)
21+
---@field name? string
2122
---@field method string The request method
2223
---@field url string The request URL
2324
---@field http_version? string The request HTTP protocol
@@ -42,7 +43,7 @@ local rest_nvim_last_request = nil
4243

4344
---@param req rest.Request
4445
local function run_request(req)
45-
logger.debug("run_request")
46+
logger.debug("running request:" .. req.name)
4647
local client = clients.get_available_clients(req)[1]
4748
if not client then
4849
logger.error("can't find registered client available for request:\n" .. vim.inspect(req))
@@ -96,17 +97,12 @@ local function run_request(req)
9697
-- FIXME: return future to pass the command state
9798
end
9899

99-
---run request in current cursor position
100-
function M.run()
101-
logger.info("starting request")
102-
local req_node = parser.get_cursor_request_node()
100+
---Run request in current file.
101+
---When `name` is not provided, run request on cursor position
102+
---@param name string|nil name of the request
103+
function M.run(name)
104+
local req_node = parser.get_request_node(name)
103105
if not req_node then
104-
logger.error("Failed to find request at cursor position")
105-
vim.notify(
106-
"Failed to find request at cursor position. See `:Rest logs` for more info.",
107-
vim.log.levels.ERROR,
108-
{ title = "rest.nvim" }
109-
)
110106
return
111107
end
112108
local ctx = Context:new()
@@ -121,36 +117,7 @@ function M.run()
121117
end
122118
local highlight = config.highlight
123119
if highlight.enable then
124-
utils.ts_highlight_node(0, req_node, require("rest-nvim.api").namespace)
125-
end
126-
run_request(req)
127-
end
128-
129-
---@param name string
130-
function M.run_by_name(name)
131-
local req_node = parser.get_request_node_by_name(name)
132-
if not req_node then
133-
logger.error("Failed to find request by name: " .. name)
134-
vim.notify(
135-
"Failed to find request by name: " .. name .. ". See `:Rest logs` for more info.",
136-
vim.log.levels.ERROR,
137-
{ title = "rest.nvim" }
138-
)
139-
return
140-
end
141-
local ctx = Context:new()
142-
if config.env.enable and vim.b._rest_nvim_env_file then
143-
ctx:load_file(vim.b._rest_nvim_env_file)
144-
end
145-
local req = parser.parse(req_node, 0, ctx)
146-
if not req then
147-
logger.error("failed to parse request")
148-
vim.notify("failed to parse request", vim.log.levels.ERROR, { title = "rest.nvim" })
149-
return
150-
end
151-
local highlight = config.highlight
152-
if highlight.enable then
153-
utils.ts_highlight_node(0, req_node, require("rest-nvim.api").namespace)
120+
utils.ts_highlight_node(0, req_node, require("rest-nvim.api").namespace, highlight.timeout)
154121
end
155122
run_request(req)
156123
end
@@ -165,31 +132,31 @@ function M.run_last()
165132
run_request(req)
166133
end
167134

168-
---run all requests in current file with same context
169-
function M.run_all()
170-
local reqs = parser.get_all_request_nodes(0)
171-
local ctx = Context:new()
172-
for _, req_node in ipairs(reqs) do
173-
local req = parser.parse(req_node, 0, ctx)
174-
if not req then
175-
vim.notify(
176-
"Parsing request failed. See `:Rest logs` for more info",
177-
vim.log.levels.ERROR,
178-
{ title = "rest.nvim" }
179-
)
180-
return false
181-
end
182-
-- FIXME: wait for previous request ends
183-
local ok = run_request(req)
184-
if not ok then
185-
vim.notify(
186-
"Running request failed. See `:Rest logs` for more info",
187-
vim.log.levels.ERROR,
188-
{ title = "rest.nvim" }
189-
)
190-
return
191-
end
192-
end
193-
end
135+
-- ---run all requests in current file with same context
136+
-- function M.run_all()
137+
-- local reqs = parser.get_all_request_nodes(0)
138+
-- local ctx = Context:new()
139+
-- for _, req_node in ipairs(reqs) do
140+
-- local req = parser.parse(req_node, 0, ctx)
141+
-- if not req then
142+
-- vim.notify(
143+
-- "Parsing request failed. See `:Rest logs` for more info",
144+
-- vim.log.levels.ERROR,
145+
-- { title = "rest.nvim" }
146+
-- )
147+
-- return false
148+
-- end
149+
-- -- FIXME: wait for previous request ends
150+
-- local ok = run_request(req)
151+
-- if not ok then
152+
-- vim.notify(
153+
-- "Running request failed. See `:Rest logs` for more info",
154+
-- vim.log.levels.ERROR,
155+
-- { title = "rest.nvim" }
156+
-- )
157+
-- return
158+
-- end
159+
-- end
160+
-- end
194161

195162
return M

lua/rest-nvim/utils.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,11 @@ utils.transform_size = transform.size
162162
---@param bufnr number
163163
---@param node TSNode
164164
---@param ns number
165-
function utils.ts_highlight_node(bufnr, node, ns)
165+
---@param timeout number
166+
function utils.ts_highlight_node(bufnr, node, ns, timeout)
166167
if bufnr == 0 then
167168
bufnr = vim.api.nvim_get_current_buf()
168169
end
169-
local highlight = require("rest-nvim.config").highlight
170170
local higroup = "IncSearch"
171171
local s_row, s_col = node:start()
172172
local e_row, e_col = node:end_()
@@ -182,7 +182,7 @@ function utils.ts_highlight_node(bufnr, node, ns)
182182
if vim.api.nvim_buf_is_valid(bufnr) then
183183
vim.api.nvim_buf_clear_namespace(bufnr, ns, 0, -1)
184184
end
185-
end, highlight.timeout)
185+
end, timeout)
186186
end
187187

188188
---@param source string|integer

0 commit comments

Comments
 (0)