Skip to content

Commit 0d90bcf

Browse files
authored
Add in-memory context variables (#241)
1 parent 5bcaa10 commit 0d90bcf

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

lua/rest-nvim/curl/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ local function create_callback(curl_cmd, opts)
114114
pretty_print = vim.pretty_print,
115115
json_decode = vim.fn.json_decode,
116116
set_env = utils.set_env,
117+
set = utils.set_context,
117118
}
118119
local env = { context = context }
119120
setmetatable(env, { __index = _G })

lua/rest-nvim/utils/init.lua

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ local random = math.random
44
math.randomseed(os.time())
55

66
local M = {}
7+
local contexts = {}
78

89
M.binary_content_types = {
910
"octet-stream",
@@ -30,6 +31,16 @@ M.set_env = function(key, value)
3031
M.write_env_file(variables)
3132
end
3233

34+
-- set_context sets a context variable for the current file
35+
-- @param key The key to set
36+
-- @param value The value to set
37+
M.set_context = function(key, value)
38+
local env_file = "/" .. (config.get("env_file") or ".env")
39+
local context = contexts[env_file] or {}
40+
context[key] = value
41+
contexts[env_file] = context
42+
end
43+
3344
M.write_env_file = function(variables)
3445
local env_file = "/" .. (config.get("env_file") or ".env")
3546

@@ -142,6 +153,11 @@ M.get_env_variables = function()
142153
return variables
143154
end
144155

156+
M.get_context_variables = function()
157+
local env_file = "/" .. (config.get("env_file") or ".env")
158+
return contexts[env_file] or {}
159+
end
160+
145161
-- get_variables Reads the environment variables found in the env_file option
146162
-- (default: .env) specified in configuration or from the files being read
147163
-- with variables beginning with @ and returns a table with the variables
@@ -229,10 +245,11 @@ end
229245

230246
M.read_variables = function()
231247
local first = M.get_variables()
232-
local second = M.read_dynamic_variables()
233-
local third = M.read_document_variables()
248+
local second = M.get_context_variables()
249+
local third = M.read_dynamic_variables()
250+
local fourth = M.read_document_variables()
234251

235-
return vim.tbl_extend("force", first, second, third)
252+
return vim.tbl_extend("force", first, second, third, fourth)
236253
end
237254

238255
-- replace_vars replaces the env variables fields in the provided string

0 commit comments

Comments
 (0)