Skip to content

Commit 16c1c8d

Browse files
authored
Merge pull request #223 from ddomurad/ddomurad/refactor_events
Fix RestStopRequest event
2 parents f13ae54 + 19cb82d commit 16c1c8d

File tree

3 files changed

+38
-21
lines changed

3 files changed

+38
-21
lines changed

doc/tags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ rest-nvim-license rest-nvim.txt /*rest-nvim-license*
88
rest-nvim-quick-start rest-nvim.txt /*rest-nvim-quick-start*
99
rest-nvim-response-script rest-nvim.txt /*rest-nvim-response-script*
1010
rest-nvim-usage rest-nvim.txt /*rest-nvim-usage*
11+
rest-nvim-usage-callbacks rest-nvim.txt /*rest-nvim-usage-callbacks*
1112
rest-nvim-usage-commands rest-nvim.txt /*rest-nvim-usage-commands*
1213
rest-nvim-usage-dynamic-variables rest-nvim.txt /*rest-nvim-usage-dynamic-variables*
1314
rest-nvim-usage-environment-variables rest-nvim.txt /*rest-nvim-usage-environment-variables*

lua/rest-nvim/curl/init.lua

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,29 @@ local function format_curl_cmd(res)
3131
return cmd
3232
end
3333

34+
local function send_curl_start_event(data)
35+
vim.api.nvim_exec_autocmds("User", {
36+
pattern = "RestStartRequest",
37+
modeline = false,
38+
data = data,
39+
})
40+
end
41+
42+
local function send_curl_stop_event(data)
43+
vim.api.nvim_exec_autocmds("User", {
44+
pattern = "RestStopRequest",
45+
modeline = false,
46+
data = data,
47+
})
48+
end
49+
50+
local function create_error_handler(opts)
51+
return function(err)
52+
send_curl_stop_event(vim.tbl_extend("keep", { err = err }, opts))
53+
error(err.message)
54+
end
55+
end
56+
3457
-- get_or_create_buf checks if there is already a buffer with the rest run results
3558
-- and if the buffer does not exists, then create a new one
3659
M.get_or_create_buf = function()
@@ -64,12 +87,17 @@ M.get_or_create_buf = function()
6487
vim.api.nvim_set_option_value("ft", "httpResult", { buf = new_bufnr })
6588
vim.api.nvim_set_option_value("buftype", "nofile", { buf = new_bufnr })
6689

67-
6890
return new_bufnr
6991
end
7092

71-
local function create_callback(curl_cmd, method, url, script_str)
93+
local function create_callback(curl_cmd, opts)
94+
local method = opts.method
95+
local url = opts.url
96+
local script_str = opts.script_str
97+
7298
return function(res)
99+
send_curl_stop_event(vim.tbl_extend("keep", { res = res }, opts))
100+
73101
if res.exit ~= 0 then
74102
log.error("[rest.nvim] " .. utils.curl_error(res.exit))
75103
return
@@ -234,16 +262,20 @@ M.curl_cmd = function(opts)
234262
local res = curl[opts.method](dry_run_opts)
235263
local curl_cmd = format_curl_cmd(res)
236264

265+
send_curl_start_event(opts)
266+
237267
if opts.dry_run then
238268
if config.get("yank_dry_run") then
239269
vim.cmd("let @+=" .. string.format("%q", curl_cmd))
240270
end
241271

242272
vim.api.nvim_echo({ { "[rest.nvim] Request preview:\n", "Comment" }, { curl_cmd } }, false, {})
273+
274+
send_curl_stop_event(opts)
243275
return
244276
else
245-
opts.callback =
246-
vim.schedule_wrap(create_callback(curl_cmd, opts.method, opts.url, opts.script_str))
277+
opts.callback = vim.schedule_wrap(create_callback(curl_cmd, opts))
278+
opts.on_error = vim.schedule_wrap(create_error_handler(opts))
247279
curl[opts.method](opts)
248280
end
249281
end

lua/rest-nvim/init.lua

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ rest.setup = function(user_configs)
1818
config.set(user_configs or {})
1919
end
2020

21-
2221
-- run will retrieve the required request information from the current buffer
2322
-- and then execute curl
2423
-- @param verbose toggles if only a dry run with preview should be executed (true = preview)
@@ -166,6 +165,7 @@ rest.run_request = function(req, opts)
166165
end
167166

168167
Opts = {
168+
request_id = vim.loop.now(), -- request id used to correlate RestStartRequest and RestStopRequest events
169169
method = result.method:lower(),
170170
url = result.url,
171171
-- plenary.curl can't set http protocol version
@@ -188,23 +188,7 @@ rest.run_request = function(req, opts)
188188
backend.highlight(result.bufnr, result.start_line, result.end_line)
189189
end
190190

191-
local request_id = vim.loop.now()
192-
local data = {
193-
requestId = request_id,
194-
request = req,
195-
}
196-
197-
vim.api.nvim_exec_autocmds("User", {
198-
pattern = "RestStartRequest",
199-
modeline = false,
200-
data = data,
201-
})
202191
local success_req, req_err = pcall(curl.curl_cmd, Opts)
203-
vim.api.nvim_exec_autocmds("User", {
204-
pattern = "RestStopRequest",
205-
modeline = false,
206-
data = vim.tbl_extend("keep", { status = success_req, message = req_err }, data),
207-
})
208192

209193
if not success_req then
210194
vim.api.nvim_err_writeln(

0 commit comments

Comments
 (0)