Skip to content

Commit

Permalink
Environment Scope
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Stocker authored and Jan Stocker committed Sep 6, 2024
1 parent f5b0a69 commit 32d4e53
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 37 deletions.
26 changes: 26 additions & 0 deletions docs/docs/getting-started/configuration-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ Here is a full example of setting up the Kulala plugin with the available `opts`
-- disable the vim.print output of the scripts
-- they will be still written to disk, but not printed immediately
disable_script_print_output = false,
-- set scope for environment and request variables
-- possible values: b = buffer, g = global
environment_scope = "b",
},
}
```
Expand Down Expand Up @@ -511,3 +514,26 @@ Example:
},
}
```
### environment_scope

While using request variables the results will be stored for later use.
As usual variables they are file relevant and should be stored in the buffer.
If you want to share the variables between buffers you can use the global scope.

Possible values:

- `"b"` (buffer)
- `"g"` (global)

Default: `"b"`

Example:

```lua
{
"mistweaverco/kulala.nvim",
opts = {
environment_scope = "b",
},
}
```
3 changes: 3 additions & 0 deletions lua/kulala/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ M.defaults = {
-- disable the vim.print output of the scripts
-- they will be still written to disk, but not printed immediately
disable_script_print_output = false,
-- set scope for environment and request variables
-- possible values: b = buffer, g = global
environment_scope = "b",
}

M.default_contenttype = {
Expand Down
59 changes: 53 additions & 6 deletions lua/kulala/db/init.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,57 @@
local CONFIG = require("kulala.config")

local M = {}

M.data = {
selected_env = nil, -- string - name of selected env
http_client_env = nil, -- table of envs from http-client.env.json
http_client_env_base = nil, -- table of base env values which should be applied to all requests
env = {}, -- table of envs from document sources
}
M.data = nil

local function default_data()
return {
selected_env = nil, -- string - name of selected env
http_client_env = nil, -- table of envs from http-client.env.json
http_client_env_base = nil, -- table of base env values which should be applied to all requests
env = {}, -- table of envs from document sources
scope_nr = nil, -- number - buffer number of the current scope
}
end

local function get_current_scope_nr()
if CONFIG.get().environment_scope == "b" then
return vim.fn.bufnr()
elseif CONFIG.get().environment_scope == "g" then
return 0
end
end

local function load_data()
if CONFIG.get().environment_scope == "b" then
M.data = vim.b.kulala_data or default_data()
elseif CONFIG.get().environment_scope == "g" then
-- keep in lua only
if not M.data then
M.data = default_data()
end
end
M.data.scope_nr = get_current_scope_nr()
end

local function save_data()
if CONFIG.get().environment_scope == "b" then
if vim.fn.bufexists(M.data.scope_nr) ~= -1 then
vim.b[M.data.scope_nr].kulala_data = M.data
end
elseif CONFIG.get().environment_scope == "g" then
-- keep in lua only
end
end

M.get = function()
if not M.data or not M.data.scope_nr then
load_data()
elseif M.data.scope_nr ~= get_current_scope_nr() then
save_data()
load_data()
end
return M.data
end

return M
2 changes: 1 addition & 1 deletion lua/kulala/external_processing/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ M.env_stdin_cmd = function(cmdstring, contents)
end

-- save the result to the environment variable
DB.data.env[env_name] = M.stdin_cmd(cmd_string, contents)
DB.get().env[env_name] = M.stdin_cmd(cmd_string, contents)
end

return M
12 changes: 6 additions & 6 deletions lua/kulala/internal_processing/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ M.set_env_for_named_request = function(name, body)
cookies = get_cookies_as_table(),
},
request = {
headers = DB.data.current_request.headers,
body = DB.data.current_request.body,
headers = DB.get().current_request.headers,
body = DB.get().current_request.body,
},
}
DB.data.env[name] = named_request
DB.get().env[name] = named_request
end

M.env_header_key = function(cmd)
Expand All @@ -122,7 +122,7 @@ M.env_header_key = function(cmd)
if value == nil then
vim.notify("env-header-key --> Header not found.", vim.log.levels.ERROR)
else
DB.data.env[variable_name] = value
DB.get().env[variable_name] = value
end
end

Expand Down Expand Up @@ -151,7 +151,7 @@ M.env_json_key = function(cmd, body)
else
local kv = vim.split(cmd, " ")
local value = get_nested_value(json, kv[2])
DB.data.env[kv[1]] = value
DB.get().env[kv[1]] = value
end
end

Expand All @@ -163,7 +163,7 @@ M.prompt_var = function(metadata_value)
if value == nil or value == "" then
return false
end
DB.data.env[key] = value
DB.get().env[key] = value
return true
end

Expand Down
22 changes: 11 additions & 11 deletions lua/kulala/parser/env.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ M.get_env = function()
env[key] = value
end

DB.data.http_client_env_base = {}
DB.data.http_client_env = {}
DB.get().http_client_env_base = {}
DB.get().http_client_env = {}

if Config.get().vscode_rest_client_environmentvars then
local vscode_dir = FS.find_file_in_parent_dirs(".vscode")
Expand All @@ -29,10 +29,10 @@ M.get_env = function()
if settings and settings["rest-client.environmentVariables"] then
local f = settings["rest-client.environmentVariables"]
if f["$shared"] then
DB.data.http_client_env_base = vim.tbl_deep_extend("force", DB.data.http_client_env_base, f["$shared"])
DB.get().http_client_env_base = vim.tbl_deep_extend("force", DB.get().http_client_env_base, f["$shared"])
end
f["$shared"] = nil
DB.data.http_client_env = vim.tbl_deep_extend("force", DB.data.http_client_env, f)
DB.get().http_client_env = vim.tbl_deep_extend("force", DB.get().http_client_env, f)
end
end
end
Expand All @@ -43,10 +43,10 @@ M.get_env = function()
if settings and settings["rest-client.environmentVariables"] then
local f = settings["rest-client.environmentVariables"]
if f["$shared"] then
DB.data.http_client_env_base = vim.tbl_deep_extend("force", DB.data.http_client_env_base, f["$shared"])
DB.get().http_client_env_base = vim.tbl_deep_extend("force", DB.get().http_client_env_base, f["$shared"])
end
f["$shared"] = nil
DB.data.http_client_env = vim.tbl_deep_extend("force", DB.data.http_client_env, f)
DB.get().http_client_env = vim.tbl_deep_extend("force", DB.get().http_client_env, f)
end
end
end
Expand All @@ -67,24 +67,24 @@ M.get_env = function()
if http_client_env_json then
local f = vim.fn.json_decode(vim.fn.readfile(http_client_env_json))
if f._base then
DB.data.http_client_env_base = vim.tbl_deep_extend("force", DB.data.http_client_env_base, f._base)
DB.get().http_client_env_base = vim.tbl_deep_extend("force", DB.get().http_client_env_base, f._base)
end
f._base = nil
DB.data.http_client_env = vim.tbl_deep_extend("force", DB.data.http_client_env, f)
DB.get().http_client_env = vim.tbl_deep_extend("force", DB.get().http_client_env, f)
end

for key, value in pairs(DB.data.http_client_env_base) do
for key, value in pairs(DB.get().http_client_env_base) do
if key ~= "DEFAULT_HEADERS" then
env[key] = value
end
end

local selected_env = DB.data.http_client_env[vim.g.kulala_selected_env or Config.get().default_env]
local selected_env = DB.get().http_client_env[vim.g.kulala_selected_env or Config.get().default_env]
if selected_env then
env = vim.tbl_extend("force", env, selected_env)
end

for key, value in pairs(DB.data.env) do
for key, value in pairs(DB.get().env) do
env[key] = value
end

Expand Down
8 changes: 4 additions & 4 deletions lua/kulala/parser/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ function M.parse(start_request_linenr)
Scripts.javascript.run("pre_request", req.scripts.pre_request)
local env = ENV_PARSER.get_env()

DB.data.previous_request = DB.data.current_request
DB.get().previous_request = DB.get().current_request

document_variables = extend_document_variables(document_variables, req)

Expand Down Expand Up @@ -531,8 +531,8 @@ function M.parse(start_request_linenr)
end

-- Merge headers from the _base environment if it exists
if DB.data.http_client_env_base then
local default_headers = DB.data.http_client_env_base["DEFAULT_HEADERS"]
if DB.get().http_client_env_base then
local default_headers = DB.get().http_client_env_base["DEFAULT_HEADERS"]
if default_headers then
for key, value in pairs(default_headers) do
key = key:lower()
Expand Down Expand Up @@ -664,7 +664,7 @@ function M.parse(start_request_linenr)
if CONFIG.get().debug then
FS.write_file(PLUGIN_TMP_DIR .. "/request.txt", table.concat(res.cmd, " "), false)
end
DB.data.current_request = res
DB.get().current_request = res
return res
end

Expand Down
6 changes: 3 additions & 3 deletions lua/kulala/parser/request_variables.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ local function get_config_contenttype(headers)
end

local function get_body_value_from_path(name, method, subpath)
local base_table = DB.data.env[name]
local base_table = DB.get().env[name]
if not base_table then
return nil
end
Expand Down Expand Up @@ -80,7 +80,7 @@ local function get_body_value_from_path(name, method, subpath)
end

local function get_header_value_from_path(name, method, subpath)
local base_table = DB.data.env[name]
local base_table = DB.get().env[name]
if not base_table then
return nil
end
Expand Down Expand Up @@ -110,7 +110,7 @@ local function get_header_value_from_path(name, method, subpath)
end

local function get_cookies_value_from_path(name, subpath)
local base_table = DB.data.env[name]
local base_table = DB.get().env[name]
if not base_table then
return nil
end
Expand Down
2 changes: 1 addition & 1 deletion lua/kulala/ui/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ M.show_script_output = function()
end

M.replay = function()
local result = DB.data.current_request
local result = DB.get().current_request
if result == nil then
vim.notify("No request to replay", vim.log.levels.WARN, { title = "kulala" })
return
Expand Down
4 changes: 2 additions & 2 deletions lua/kulala/ui/selector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ local FS = require("kulala.utils.fs")
local M = {}

function M.select_env()
if not DB.data.http_client_env then
if not DB.get().http_client_env then
return
end

local envs = {}
for key, _ in pairs(DB.data.http_client_env) do
for key, _ in pairs(DB.get().http_client_env) do
table.insert(envs, key)
end

Expand Down
6 changes: 3 additions & 3 deletions lua/telescope/_extensions/kulala.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ local function kulala_search(_)
end

local function kulala_env_select(_)
if not DB.data.http_client_env then
if not DB.get().data.http_client_env then
return
end

local envs = {}
for key, _ in pairs(DB.data.http_client_env) do
for key, _ in pairs(DB.get().data.http_client_env) do
table.insert(envs, key)
end

Expand All @@ -74,7 +74,7 @@ local function kulala_env_select(_)
previewer = previewers.new_buffer_previewer({
title = "Environment",
define_preview = function(self, entry)
local env = DB.data.http_client_env[entry.value]
local env = DB.get().data.http_client_env[entry.value]
if env == nil then
return
end
Expand Down

0 comments on commit 32d4e53

Please sign in to comment.