Skip to content

Commit

Permalink
feat: implementing backend-agnostic run_request
Browse files Browse the repository at this point in the history
  • Loading branch information
teto committed Jul 13, 2023
1 parent f1597ab commit 87941ab
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 24 deletions.
54 changes: 30 additions & 24 deletions lua/rest-nvim/init.lua
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
local request = require("rest-nvim.request")
local backend = require("rest-nvim.request")
local config = require("rest-nvim.config")
local curl = require("rest-nvim.curl")
local log = require("plenary.log").new({ plugin = "rest.nvim" })

local rest = {}
local Opts = {}
local LastOpts = {}
local defaultRequestOpts = {
verbose = false,
highlight = false,
engine = 'classic'
}

local LastOpts = {}
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)
rest.run = function(verbose)
local ok, result = request.get_current_request()
local ok, result = backend.get_current_request()
if not ok then
log.error("Failed to run the http request:")
log.error(result)
Expand All @@ -31,27 +37,28 @@ end
-- @param string filename to load
-- @param opts table
-- 1. keep_going boolean keep running even when last request failed
-- 2. verbose boolean
rest.run_file = function(filename, opts)
log.info("Running file :" .. filename)
local new_buf = vim.api.nvim_create_buf(false, false)
opts = vim.tbl_deep_extend(
"force", -- use value from rightmost map
defaultRequestOpts,
opts or {}
)

-- 0 on error or buffer handle
local new_buf = vim.api.nvim_create_buf(true, false)

vim.api.nvim_win_set_buf(0, new_buf)
vim.cmd.edit(filename)
local last_line = vim.fn.line("$")

-- reset cursor position
vim.fn.cursor(1, 1)
local curpos = vim.fn.getcurpos()
while curpos[2] <= last_line do
local ok, req = request.buf_get_request(new_buf, curpos)
if ok then
-- request.print_request(req)
curpos[2] = req.end_line + 1
rest.run_request(req, opts)
else
return false, req
end

local requests = backend.buf_list_requests(new_buf)
for _, req in pairs(requests) do
vim.pretty_print("Request:")
vim.pretty_print(req)
rest.run_request(req, opts)
end

return true
end

Expand All @@ -62,12 +69,11 @@ end
-- @param opts table
-- 1. keep_going boolean keep running even when last request failed
rest.run_request = function(req, opts)
-- TODO rename result to req
local result = req
opts = vim.tbl_deep_extend(
"force", -- use value from rightmost map
{ verbose = false,
highlight = false
}, -- defaults
defaultRequestOpts,
opts or {}
)

Expand All @@ -92,7 +98,7 @@ rest.run_request = function(req, opts)
end

if opts.highlight then
request.highlight(result.bufnr, result.start_line, result.end_line)
backend.highlight(result.bufnr, result.start_line, result.end_line)
end

local request_id = vim.loop.now()
Expand Down Expand Up @@ -127,7 +133,7 @@ rest.last = function()
end

if config.get("highlight").enabled then
request.highlight(LastOpts.bufnr, LastOpts.start_line, LastOpts.end_line)
backend.highlight(LastOpts.bufnr, LastOpts.start_line, LastOpts.end_line)
end

local success_req, req_err = pcall(curl.curl_cmd, LastOpts)
Expand All @@ -140,7 +146,7 @@ rest.last = function()
end
end

rest.request = request
rest.request = backend

rest.select_env = function(path)
if path ~= nil then
Expand Down
22 changes: 22 additions & 0 deletions lua/rest-nvim/request/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ local function get_curl_args(bufnr, headers_end, end_line)
local curl_args = {}
local body_start = end_line

log.debug("Getting curl args between lines", headers_end, " and ", end_line)
for line_number = headers_end, end_line do
local line_content = vim.fn.getbufline(bufnr, line_number)[1]

Expand Down Expand Up @@ -399,6 +400,27 @@ M.stringify_request = function(req, opts)
return (str)
end

M.buf_list_requests = function(buf, _opts)
local last_line = vim.fn.line("$")
local requests = {}

-- reset cursor position
vim.fn.cursor({1, 1})
local curpos = vim.fn.getcurpos()
log.debug("Listing requests for buf ", buf)
while curpos[2] <= last_line do
local ok, req = M.buf_get_request(buf, curpos)
if ok then
curpos[2] = req.end_line + 1
requests[#requests + 1] = req
else
break
end
end
-- log.debug("found " , #requests , "requests")
return requests
end

local select_ns = vim.api.nvim_create_namespace("rest-nvim")
M.highlight = function(bufnr, start_line, end_line)
local opts = config.get("highlight") or {}
Expand Down

0 comments on commit 87941ab

Please sign in to comment.