Skip to content

Commit 2402414

Browse files
fix(parser): handle variables in URI
- never uri-encode variables - parse variables with quotes
1 parent b7ab923 commit 2402414

File tree

3 files changed

+12
-18
lines changed

3 files changed

+12
-18
lines changed

lua/rest-nvim/context.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
local dotenv = require("rest-nvim.dotenv")
44
local config = require("rest-nvim.config")
5+
local logger = require("rest-nvim.logger")
56
local M = {}
67

78
---@class rest.Context
@@ -57,6 +58,7 @@ end
5758

5859
---@param filepath string
5960
function Context:load_file(filepath)
61+
logger.debug(("load file `%s` to context"):format(filepath))
6062
dotenv.load_file(filepath, function(key, value)
6163
self:set_global(key, value)
6264
end)

lua/rest-nvim/parser/dotenv.lua

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,16 @@ function M.parse(path, setter)
6262
end
6363
end
6464
else
65-
local vars_tbl = vim.split(file_contents, "\n")
66-
table.remove(vars_tbl, #vars_tbl)
65+
-- TODO: rewrite the parser with tree-sitter-bash instead
66+
local vars_tbl = vim.split(file_contents, "\n", { trimempty = true })
67+
logger.debug(vars_tbl)
6768
for _, var in ipairs(vars_tbl) do
68-
local variable = vim.split(var, "=")
69-
local variable_name = variable[1]
70-
local variable_value
71-
-- In case some weirdo adds a `=` character to his ENV value
72-
if #variable > 2 then
73-
table.remove(variable, 1)
74-
variable_value = table.concat(variable, "=")
75-
else
76-
variable_value = variable[2]
69+
local variable_name, variable_value = var:match("([^#=%s]+)%s*=%s*([^#]*)")
70+
if variable_name then
71+
variable_value = variable_value:match('^"(.*)"$') or variable_value:match("^'(.*)'$") or variable_value
72+
logger.debug("set " .. variable_name .. "=" .. variable_value)
73+
setter(variable_name, value_tostring(variable_value))
7774
end
78-
setter(variable_name, value_tostring(variable_value))
7975
end
8076
end
8177
return true, vars

lua/rest-nvim/parser/init.lua

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,12 @@ end
4343

4444
---@param src string
4545
---@param context rest.Context
46-
---@param encoder? fun(s:string):string
4746
---@return string
4847
---@return integer
49-
local function expand_variables(src, context, encoder)
48+
local function expand_variables(src, context)
5049
return src:gsub("{{(.-)}}", function(name)
5150
name = vim.trim(name)
5251
local res = context:resolve(name)
53-
if encoder then
54-
res = encoder(res)
55-
end
5652
return res
5753
end)
5854
end
@@ -383,7 +379,7 @@ function parser.parse(node, source, ctx)
383379
for child, _ in node:iter_children() do
384380
local child_type = child:type()
385381
if child_type == "request" then
386-
url = expand_variables(assert(get_node_field_text(req_node, "url", source)), ctx, utils.escape)
382+
url = expand_variables(assert(get_node_field_text(req_node, "url", source)), ctx)
387383
url = url:gsub("\n%s+", "")
388384
elseif child_type == "pre_request_script" then
389385
parser.parse_pre_request_script(child, source, ctx)

0 commit comments

Comments
 (0)