Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix RestStopRequest event #223

Merged
merged 2 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/tags
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ rest-nvim-license rest-nvim.txt /*rest-nvim-license*
rest-nvim-quick-start rest-nvim.txt /*rest-nvim-quick-start*
rest-nvim-response-script rest-nvim.txt /*rest-nvim-response-script*
rest-nvim-usage rest-nvim.txt /*rest-nvim-usage*
rest-nvim-usage-callbacks rest-nvim.txt /*rest-nvim-usage-callbacks*
rest-nvim-usage-commands rest-nvim.txt /*rest-nvim-usage-commands*
rest-nvim-usage-dynamic-variables rest-nvim.txt /*rest-nvim-usage-dynamic-variables*
rest-nvim-usage-environment-variables rest-nvim.txt /*rest-nvim-usage-environment-variables*
Expand Down
40 changes: 36 additions & 4 deletions lua/rest-nvim/curl/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,29 @@ local function format_curl_cmd(res)
return cmd
end

local function send_curl_start_event(data)
vim.api.nvim_exec_autocmds("User", {
pattern = "RestStartRequest",
modeline = false,
data = data,
})
end

local function send_curl_stop_event(data)
vim.api.nvim_exec_autocmds("User", {
pattern = "RestStopRequest",
modeline = false,
data = data,
})
end

local function create_error_handler(opts)
return function(err)
send_curl_stop_event(vim.tbl_extend("keep", { err = err }, opts))
error(err.message)
end
end

-- get_or_create_buf checks if there is already a buffer with the rest run results
-- and if the buffer does not exists, then create a new one
M.get_or_create_buf = function()
Expand Down Expand Up @@ -64,12 +87,17 @@ M.get_or_create_buf = function()
vim.api.nvim_set_option_value("ft", "httpResult", { buf = new_bufnr })
vim.api.nvim_set_option_value("buftype", "nofile", { buf = new_bufnr })


return new_bufnr
end

local function create_callback(curl_cmd, method, url, script_str)
local function create_callback(curl_cmd, opts)
local method = opts.method
local url = opts.url
local script_str = opts.script_str

return function(res)
send_curl_stop_event(vim.tbl_extend("keep", { res = res }, opts))

if res.exit ~= 0 then
log.error("[rest.nvim] " .. utils.curl_error(res.exit))
return
Expand Down Expand Up @@ -234,16 +262,20 @@ M.curl_cmd = function(opts)
local res = curl[opts.method](dry_run_opts)
local curl_cmd = format_curl_cmd(res)

send_curl_start_event(opts)

if opts.dry_run then
if config.get("yank_dry_run") then
vim.cmd("let @+=" .. string.format("%q", curl_cmd))
end

vim.api.nvim_echo({ { "[rest.nvim] Request preview:\n", "Comment" }, { curl_cmd } }, false, {})

send_curl_stop_event(opts)
return
else
opts.callback =
vim.schedule_wrap(create_callback(curl_cmd, opts.method, opts.url, opts.script_str))
opts.callback = vim.schedule_wrap(create_callback(curl_cmd, opts))
opts.on_error = vim.schedule_wrap(create_error_handler(opts))
curl[opts.method](opts)
end
end
Expand Down
18 changes: 1 addition & 17 deletions lua/rest-nvim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ rest.setup = function(user_configs)
config.set(user_configs or {})
end


-- run will retrieve the required request information from the current buffer
-- and then execute curl
-- @param verbose toggles if only a dry run with preview should be executed (true = preview)
Expand Down Expand Up @@ -164,6 +163,7 @@ rest.run_request = function(req, opts)
end

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

local request_id = vim.loop.now()
local data = {
requestId = request_id,
request = req,
}

vim.api.nvim_exec_autocmds("User", {
pattern = "RestStartRequest",
modeline = false,
data = data,
})
local success_req, req_err = pcall(curl.curl_cmd, Opts)
vim.api.nvim_exec_autocmds("User", {
pattern = "RestStopRequest",
modeline = false,
data = vim.tbl_extend("keep", { status = success_req, message = req_err }, data),
})

if not success_req then
vim.api.nvim_err_writeln(
Expand Down