Skip to content

Commit ca14c8c

Browse files
authored
Merge pull request #217 from teto/treesitter-engine
2 parents f1597ab + f811cfe commit ca14c8c

File tree

3 files changed

+178
-115
lines changed

3 files changed

+178
-115
lines changed

lua/rest-nvim/curl/init.lua

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

34-
3534
-- get_or_create_buf checks if there is already a buffer with the rest run results
3635
-- and if the buffer does not exists, then create a new one
3736
M.get_or_create_buf = function()
@@ -230,7 +229,7 @@ end
230229
M.curl_cmd = function(opts)
231230
-- plenary's curl module is strange in the sense that with "dry_run" it returns the command
232231
-- otherwise it starts the request :/
233-
local dry_run_opts = vim.tbl_extend("force", opts, { dry_run = true } )
232+
local dry_run_opts = vim.tbl_extend("force", opts, { dry_run = true })
234233
local res = curl[opts.method](dry_run_opts)
235234
local curl_cmd = format_curl_cmd(res)
236235

@@ -242,7 +241,8 @@ M.curl_cmd = function(opts)
242241
vim.api.nvim_echo({ { "[rest.nvim] Request preview:\n", "Comment" }, { curl_cmd } }, false, {})
243242
return
244243
else
245-
opts.callback = vim.schedule_wrap(create_callback(curl_cmd, opts.method, opts.url, opts.script_str))
244+
opts.callback =
245+
vim.schedule_wrap(create_callback(curl_cmd, opts.method, opts.url, opts.script_str))
246246
curl[opts.method](opts)
247247
end
248248
end

lua/rest-nvim/init.lua

Lines changed: 122 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
local request = require("rest-nvim.request")
1+
local backend = require("rest-nvim.request")
22
local config = require("rest-nvim.config")
33
local curl = require("rest-nvim.curl")
44
local log = require("plenary.log").new({ plugin = "rest.nvim" })
5+
local utils = require("rest-nvim.utils")
6+
local path = require("plenary.path")
57

68
local rest = {}
79
local Opts = {}
10+
local defaultRequestOpts = {
11+
verbose = false,
12+
highlight = false,
13+
}
14+
815
local LastOpts = {}
916

1017
rest.setup = function(user_configs)
@@ -15,7 +22,7 @@ end
1522
-- and then execute curl
1623
-- @param verbose toggles if only a dry run with preview should be executed (true = preview)
1724
rest.run = function(verbose)
18-
local ok, result = request.get_current_request()
25+
local ok, result = backend.get_current_request()
1926
if not ok then
2027
log.error("Failed to run the http request:")
2128
log.error(result)
@@ -31,55 +38,130 @@ end
3138
-- @param string filename to load
3239
-- @param opts table
3340
-- 1. keep_going boolean keep running even when last request failed
41+
-- 2. verbose boolean
3442
rest.run_file = function(filename, opts)
3543
log.info("Running file :" .. filename)
36-
local new_buf = vim.api.nvim_create_buf(false, false)
44+
opts = vim.tbl_deep_extend(
45+
"force", -- use value from rightmost map
46+
defaultRequestOpts,
47+
opts or {}
48+
)
49+
50+
-- 0 on error or buffer handle
51+
local new_buf = vim.api.nvim_create_buf(true, false)
3752

3853
vim.api.nvim_win_set_buf(0, new_buf)
3954
vim.cmd.edit(filename)
40-
local last_line = vim.fn.line("$")
41-
42-
-- reset cursor position
43-
vim.fn.cursor(1, 1)
44-
local curpos = vim.fn.getcurpos()
45-
while curpos[2] <= last_line do
46-
local ok, req = request.buf_get_request(new_buf, curpos)
47-
if ok then
48-
-- request.print_request(req)
49-
curpos[2] = req.end_line + 1
50-
rest.run_request(req, opts)
51-
else
52-
return false, req
53-
end
55+
56+
local requests = backend.buf_list_requests(new_buf)
57+
for _, req in pairs(requests) do
58+
rest.run_request(req, opts)
5459
end
60+
5561
return true
5662
end
5763

64+
-- replace variables in header values
65+
local function splice_headers(headers)
66+
for name, value in pairs(headers) do
67+
headers[name] = utils.replace_vars(value)
68+
end
69+
return headers
70+
end
71+
72+
-- return the spliced/resolved filename
73+
-- @param string the filename w/o variables
74+
local function load_external_payload(fileimport_string)
75+
local fileimport_spliced = utils.replace_vars(fileimport_string)
76+
if path:new(fileimport_spliced):is_absolute() then
77+
return fileimport_spliced
78+
else
79+
local file_dirname = vim.fn.expand("%:p:h")
80+
local file_name = path:new(path:new(file_dirname), fileimport_spliced)
81+
return file_name:absolute()
82+
end
83+
end
84+
85+
86+
-- @param headers table HTTP headers
87+
-- @param payload table of the form { external = bool, filename_tpl= path, body_tpl = string }
88+
-- with body_tpl an array of lines
89+
local function splice_body(headers, payload)
90+
local external_payload = payload.external
91+
local lines -- array of strings
92+
if external_payload then
93+
local importfile = load_external_payload(payload.filename_tpl)
94+
if not utils.file_exists(importfile) then
95+
error("import file " .. importfile .. " not found")
96+
end
97+
-- TODO we dont necessarily want to load the file, it can be slow
98+
-- https://github.com/rest-nvim/rest.nvim/issues/203
99+
lines = utils.read_file(importfile)
100+
else
101+
lines = payload.body_tpl
102+
end
103+
local content_type = ""
104+
for key, val in pairs(headers) do
105+
if string.lower(key) == "content-type" then
106+
content_type = val
107+
break
108+
end
109+
end
110+
local has_json = content_type:find("application/[^ ]*json")
111+
112+
local body = ""
113+
local vars = utils.read_variables()
114+
-- nvim_buf_get_lines is zero based and end-exclusive
115+
-- but start_line and stop_line are one-based and inclusive
116+
-- magically, this fits :-) start_line is the CRLF between header and body
117+
-- which should not be included in the body, stop_line is the last line of the body
118+
for _, line in ipairs(lines) do
119+
body = body .. utils.replace_vars(line, vars)
120+
end
121+
122+
local is_json, json_body = pcall(vim.json.decode, body)
123+
124+
if is_json and json_body then
125+
if has_json then
126+
-- convert entire json body to string.
127+
return vim.fn.json_encode(json_body)
128+
else
129+
-- convert nested tables to string.
130+
for key, val in pairs(json_body) do
131+
if type(val) == "table" then
132+
json_body[key] = vim.fn.json_encode(val)
133+
end
134+
end
135+
return vim.fn.json_encode(json_body)
136+
end
137+
end
138+
end
58139

59140
-- run will retrieve the required request information from the current buffer
60141
-- and then execute curl
61142
-- @param req table see validate_request to check the expected format
62143
-- @param opts table
63144
-- 1. keep_going boolean keep running even when last request failed
64145
rest.run_request = function(req, opts)
146+
-- TODO rename result to request
65147
local result = req
66148
opts = vim.tbl_deep_extend(
67149
"force", -- use value from rightmost map
68-
{ verbose = false,
69-
highlight = false
70-
}, -- defaults
150+
defaultRequestOpts,
71151
opts or {}
72152
)
73153

154+
-- body =
155+
74156
Opts = {
75157
method = result.method:lower(),
76158
url = result.url,
77159
-- plenary.curl can't set http protocol version
78160
-- http_version = result.http_version,
79-
headers = result.headers,
161+
headers = splice_headers(result.headers),
80162
raw = config.get("skip_ssl_verification") and vim.list_extend(result.raw, { "-k" })
81163
or result.raw,
82-
body = result.body,
164+
body = splice_body(result.headers, result.body),
83165
dry_run = opts.verbose,
84166
bufnr = result.bufnr,
85167
start_line = result.start_line,
@@ -92,23 +174,26 @@ rest.run_request = function(req, opts)
92174
end
93175

94176
if opts.highlight then
95-
request.highlight(result.bufnr, result.start_line, result.end_line)
177+
backend.highlight(result.bufnr, result.start_line, result.end_line)
96178
end
97179

98180
local request_id = vim.loop.now()
99181
local data = {
100-
requestId = request_id,
101-
request = req
102-
}
182+
requestId = request_id,
183+
request = req,
184+
}
103185

104186
vim.api.nvim_exec_autocmds("User", {
105-
pattern = "RestStartRequest",
106-
modeline = false,
107-
data = data
108-
})
187+
pattern = "RestStartRequest",
188+
modeline = false,
189+
data = data,
190+
})
109191
local success_req, req_err = pcall(curl.curl_cmd, Opts)
110-
vim.api.nvim_exec_autocmds("User", { pattern = "RestStopRequest", modeline = false,
111-
data = vim.tbl_extend("keep", { status = success_req, message = req_err }, data) })
192+
vim.api.nvim_exec_autocmds("User", {
193+
pattern = "RestStopRequest",
194+
modeline = false,
195+
data = vim.tbl_extend("keep", { status = success_req, message = req_err }, data),
196+
})
112197

113198
if not success_req then
114199
vim.api.nvim_err_writeln(
@@ -127,7 +212,7 @@ rest.last = function()
127212
end
128213

129214
if config.get("highlight").enabled then
130-
request.highlight(LastOpts.bufnr, LastOpts.start_line, LastOpts.end_line)
215+
backend.highlight(LastOpts.bufnr, LastOpts.start_line, LastOpts.end_line)
131216
end
132217

133218
local success_req, req_err = pcall(curl.curl_cmd, LastOpts)
@@ -140,12 +225,12 @@ rest.last = function()
140225
end
141226
end
142227

143-
rest.request = request
228+
rest.request = backend
144229

145-
rest.select_env = function(path)
230+
rest.select_env = function(env_file)
146231
if path ~= nil then
147-
vim.validate({ path = { path, "string" } })
148-
config.set({ env_file = path })
232+
vim.validate({ env_file = { env_file, "string" } })
233+
config.set({ env_file = env_file })
149234
else
150235
print("No path given")
151236
end

0 commit comments

Comments
 (0)