Skip to content

Commit 8462ec2

Browse files
author
Matthieu Coudron
committed
utils.replace_vars: pass variables as input
to ease passing context
1 parent 9156861 commit 8462ec2

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

lua/rest-nvim/request/init.lua

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ local function get_body(bufnr, start_line, stop_line, has_json)
6565
-- but start_line and stop_line are one-based and inclusive
6666
-- magically, this fits :-) start_line is the CRLF between header and body
6767
-- which should not be included in the body, stop_line is the last line of the body
68+
local vars = utils.read_variables()
6869
for i, line in ipairs(lines) do
6970
log.fmt_debug("Line %s", line)
7071
-- stop if a script opening tag is found
@@ -75,7 +76,7 @@ local function get_body(bufnr, start_line, stop_line, has_json)
7576
end
7677
-- Ignore commented lines with and without indent
7778
if not utils.contains_comments(line) then
78-
body = body .. utils.replace_vars(line)
79+
body = body .. utils.replace_vars(line, vars)
7980
end
8081
end
8182

@@ -143,7 +144,8 @@ local function get_headers(bufnr, start_line, end_line)
143144
local header_name, header_value = line_content:match("^(.-): ?(.*)$")
144145

145146
if not utils.contains_comments(header_name) then
146-
headers[header_name:lower()] = utils.replace_vars(header_value)
147+
local vars = utils.read_variables()
148+
headers[header_name:lower()] = utils.replace_vars(header_value, vars)
147149
end
148150
::continue::
149151
end
@@ -232,10 +234,11 @@ local function parse_url(stmt)
232234
table.remove(parsed, 1)
233235
local target_url = table.concat(parsed, " ")
234236

237+
local vars = utils.load
235238
return {
236239
method = http_method,
237240
-- Encode URL
238-
url = utils.encode_url(utils.replace_vars(target_url)),
241+
url = utils.encode_url(utils.replace_vars(target_url, vars)),
239242
http_version = http_version,
240243
}
241244
end
@@ -274,7 +277,7 @@ M.get_current_request = function()
274277
string.find(headers["content-type"] or "", "application/[^ ]-json")
275278
)
276279
log.fmt_debug("Identified body as:\n %s", body)
277-
script_str = get_response_script(bufnr, script_line, end_line)
280+
script_str = get_response_script(bufnr, body_start, end_line)
278281

279282
if config.get("jump_to_request") then
280283
utils.move_cursor(bufnr, start_line)

lua/rest-nvim/utils/init.lua

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,19 +182,18 @@ end
182182
-- replace_vars replaces the env variables fields in the provided string
183183
-- with the env variable value
184184
-- @param str Where replace the placers for the env variables
185-
M.replace_vars = function(str)
186-
local vars = M.read_variables()
185+
M.replace_vars = function(str, vars)
187186
-- remove $dotenv tags, which are used by the vscode rest client for cross compatibility
188187
str = str:gsub("%$dotenv ", ""):gsub("%$DOTENV ", "")
189188

190189
for var in string.gmatch(str, "{{[^}]+}}") do
191190
var = var:gsub("{", ""):gsub("}", "")
192-
-- If the env variable wasn't found in the `.env` file or in the dynamic variables then search it
193-
-- in the OS environment variables
191+
-- If the env variable wasn't found in the `.env` file or in the dynamic variables then
194192
if M.has_key(vars, var) then
195193
str = type(vars[var]) == "function" and str:gsub("{{" .. var .. "}}", vars[var]())
196194
or str:gsub("{{" .. var .. "}}", vars[var])
197195
else
196+
-- search key in the OS environment variables
198197
if os.getenv(var) then
199198
str = str:gsub("{{" .. var .. "}}", os.getenv(var))
200199
else

0 commit comments

Comments
 (0)