@@ -31,6 +31,29 @@ local function format_curl_cmd(res)
31
31
return cmd
32
32
end
33
33
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
+
34
57
-- get_or_create_buf checks if there is already a buffer with the rest run results
35
58
-- and if the buffer does not exists, then create a new one
36
59
M .get_or_create_buf = function ()
@@ -64,12 +87,17 @@ M.get_or_create_buf = function()
64
87
vim .api .nvim_set_option_value (" ft" , " httpResult" , { buf = new_bufnr })
65
88
vim .api .nvim_set_option_value (" buftype" , " nofile" , { buf = new_bufnr })
66
89
67
-
68
90
return new_bufnr
69
91
end
70
92
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
+
72
98
return function (res )
99
+ send_curl_stop_event (vim .tbl_extend (" keep" , { res = res }, opts ))
100
+
73
101
if res .exit ~= 0 then
74
102
log .error (" [rest.nvim] " .. utils .curl_error (res .exit ))
75
103
return
@@ -234,16 +262,20 @@ M.curl_cmd = function(opts)
234
262
local res = curl [opts .method ](dry_run_opts )
235
263
local curl_cmd = format_curl_cmd (res )
236
264
265
+ send_curl_start_event (opts )
266
+
237
267
if opts .dry_run then
238
268
if config .get (" yank_dry_run" ) then
239
269
vim .cmd (" let @+=" .. string.format (" %q" , curl_cmd ))
240
270
end
241
271
242
272
vim .api .nvim_echo ({ { " [rest.nvim] Request preview:\n " , " Comment" }, { curl_cmd } }, false , {})
273
+
274
+ send_curl_stop_event (opts )
243
275
return
244
276
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 ))
247
279
curl [opts .method ](opts )
248
280
end
249
281
end
0 commit comments