Skip to content

Commit 27d0faf

Browse files
feat: better progress handlers
1 parent 434927d commit 27d0faf

File tree

4 files changed

+29
-24
lines changed

4 files changed

+29
-24
lines changed

lua/rest-nvim/client/curl/cli.lua

+10-8
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ end
313313
---@return nio.control.Future future Future containing rest.Response
314314
function curl.request(request)
315315
local progress_handle = progress.handle.create({
316-
title = "Fetching",
316+
title = "Executing",
317+
message = "Executing request...",
317318
lsp_client = { name = "rest.nvim" },
318319
})
319320
local future = nio.control.future()
@@ -328,14 +329,15 @@ function curl.request(request)
328329
end
329330
vim.schedule(function ()
330331
progress_handle:report({
331-
message = "parsing response",
332+
message = "Parsing response...",
332333
})
333-
end)
334-
local response = parser.parse_verbose(vim.split(sc.stderr, "\n"))
335-
response.body = sc.stdout
336-
future.set(response)
337-
vim.schedule(function ()
338-
progress_handle:finish()
334+
local response = parser.parse_verbose(vim.split(sc.stderr, "\n"))
335+
response.body = sc.stdout
336+
future.set(response)
337+
progress_handle:report({
338+
message = "Success",
339+
})
340+
progress_handle:finish()
339341
end)
340342
end, {
341343
-- TODO(boltless): parse by chunk from here

lua/rest-nvim/parser/init.lua

+13-11
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ function parser.create_context(source)
179179
local query = vim.treesitter.query.parse("http", IN_PLACE_VARIABLE_QUERY)
180180
local ctx = Context:new()
181181
local _, tree = utils.ts_parse_source(source)
182+
-- TODO: capture variable_decalarations in section without request
182183
for _, node in query:iter_captures(tree:root(), source) do
183184
if node:type() == "variable_declaration" then
184185
parser.parse_variable_declaration(node, source, ctx)
@@ -192,7 +193,14 @@ function parser.get_cursor_request_node()
192193
local node = vim.treesitter.get_node()
193194
if node then
194195
node = utils.ts_find(node, "section")
195-
if not node or #node:field("request") < 1 then
196+
if not node then
197+
logger.error("can't find request section node")
198+
return
199+
elseif node:has_error() then
200+
logger.error(utils.ts_node_error_log(node))
201+
return
202+
elseif #node:field("request") < 1 then
203+
logger.error("request section doesn't have request node")
196204
return
197205
end
198206
end
@@ -309,17 +317,11 @@ end
309317
---@return rest.Request|nil
310318
function parser.parse(node, source, ctx)
311319
assert(node:type() == "section")
312-
ctx = ctx or Context:new()
313-
-- request should not include error
314-
if node:has_error() then
315-
logger.error(utils.ts_node_error_log(node))
316-
return nil
317-
end
320+
assert(not node:has_error())
318321
local req_node = node:field("request")[1]
319-
if not req_node then
320-
logger.error("request section doesn't have request node")
321-
return nil
322-
end
322+
assert(req_node)
323+
324+
ctx = ctx or Context:new()
323325
local method = get_node_field_text(req_node, "method", source)
324326
if not method then
325327
logger.info("no method provided, falling back to 'GET'")

lua/rest-nvim/request.lua

+5-5
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ function M.run()
8686
logger.info("starting request")
8787
local req_node = parser.get_cursor_request_node()
8888
if not req_node then
89-
logger.error("failed to find request at cursor position")
90-
vim.notify("failed to find request at cursor position", vim.log.levels.ERROR, { title = "rest.nvim" })
89+
logger.error("Failed to find request at cursor position")
90+
vim.notify("Failed to find request at cursor position. See `:Rest logs` for more info.", vim.log.levels.ERROR, { title = "rest.nvim" })
9191
return
9292
end
9393
local ctx = parser.create_context(0)
@@ -111,8 +111,8 @@ end
111111
function M.run_by_name(name)
112112
local req_node = parser.get_request_node_by_name(name)
113113
if not req_node then
114-
logger.error("failed to find request by name: " .. name)
115-
vim.notify("failed to find request by name: " .. name, vim.log.levels.ERROR, { title = "rest.nvim" })
114+
logger.error("Failed to find request by name: " .. name)
115+
vim.notify("Failed to find request by name: " .. name .. ". See `:Rest logs` for more info.", vim.log.levels.ERROR, { title = "rest.nvim" })
116116
return
117117
end
118118
local ctx = parser.create_context(0)
@@ -144,7 +144,7 @@ end
144144

145145
---run all requests in current file with same context
146146
function M.run_all()
147-
local reqs = parser.get_all_request_nodes()
147+
local reqs = parser.get_all_request_nodes(0)
148148
local ctx = parser.create_context(0)
149149
for _, req_node in ipairs(reqs) do
150150
local req = parser.parse(req_node, 0, ctx)

spec/api_spec.lua

+1
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ X-DATE: {{$date}}
202202
request.variables.set("baz", "new")
203203
]]
204204
local h = require("rest-nvim.script.lua").load_post_req_hook(script, ctx)
205+
---@diagnostic disable-next-line: missing-parameter
205206
h()
206207
assert.same("new", vim.env["foo"])
207208
assert.same("new", ctx:resolve("bar"))

0 commit comments

Comments
 (0)