From 32d4e53b97592f5dfe55a1d4aee3978790de68dc Mon Sep 17 00:00:00 2001 From: Jan Stocker Date: Fri, 6 Sep 2024 16:29:19 +0200 Subject: [PATCH] Environment Scope --- .../getting-started/configuration-options.md | 26 ++++++++ lua/kulala/config/init.lua | 3 + lua/kulala/db/init.lua | 59 +++++++++++++++++-- lua/kulala/external_processing/init.lua | 2 +- lua/kulala/internal_processing/init.lua | 12 ++-- lua/kulala/parser/env.lua | 22 +++---- lua/kulala/parser/init.lua | 8 +-- lua/kulala/parser/request_variables.lua | 6 +- lua/kulala/ui/init.lua | 2 +- lua/kulala/ui/selector.lua | 4 +- lua/telescope/_extensions/kulala.lua | 6 +- 11 files changed, 113 insertions(+), 37 deletions(-) diff --git a/docs/docs/getting-started/configuration-options.md b/docs/docs/getting-started/configuration-options.md index a9182d3..18daea8 100644 --- a/docs/docs/getting-started/configuration-options.md +++ b/docs/docs/getting-started/configuration-options.md @@ -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", }, } ``` @@ -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", + }, +} +``` diff --git a/lua/kulala/config/init.lua b/lua/kulala/config/init.lua index 5a71867..a64d4f0 100644 --- a/lua/kulala/config/init.lua +++ b/lua/kulala/config/init.lua @@ -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 = { diff --git a/lua/kulala/db/init.lua b/lua/kulala/db/init.lua index e7b780d..1e44593 100644 --- a/lua/kulala/db/init.lua +++ b/lua/kulala/db/init.lua @@ -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 diff --git a/lua/kulala/external_processing/init.lua b/lua/kulala/external_processing/init.lua index 69fe962..5c14b59 100644 --- a/lua/kulala/external_processing/init.lua +++ b/lua/kulala/external_processing/init.lua @@ -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 diff --git a/lua/kulala/internal_processing/init.lua b/lua/kulala/internal_processing/init.lua index 8eca9cd..3e61c83 100644 --- a/lua/kulala/internal_processing/init.lua +++ b/lua/kulala/internal_processing/init.lua @@ -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) @@ -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 @@ -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 @@ -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 diff --git a/lua/kulala/parser/env.lua b/lua/kulala/parser/env.lua index 464cc60..341a7ec 100644 --- a/lua/kulala/parser/env.lua +++ b/lua/kulala/parser/env.lua @@ -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") @@ -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 @@ -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 @@ -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 diff --git a/lua/kulala/parser/init.lua b/lua/kulala/parser/init.lua index 4137957..31b4410 100644 --- a/lua/kulala/parser/init.lua +++ b/lua/kulala/parser/init.lua @@ -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) @@ -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() @@ -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 diff --git a/lua/kulala/parser/request_variables.lua b/lua/kulala/parser/request_variables.lua index c13bbfa..37faadb 100644 --- a/lua/kulala/parser/request_variables.lua +++ b/lua/kulala/parser/request_variables.lua @@ -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 @@ -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 @@ -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 diff --git a/lua/kulala/ui/init.lua b/lua/kulala/ui/init.lua index 3db7f11..26ae708 100644 --- a/lua/kulala/ui/init.lua +++ b/lua/kulala/ui/init.lua @@ -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 diff --git a/lua/kulala/ui/selector.lua b/lua/kulala/ui/selector.lua index acbae84..a0d01c2 100644 --- a/lua/kulala/ui/selector.lua +++ b/lua/kulala/ui/selector.lua @@ -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 diff --git a/lua/telescope/_extensions/kulala.lua b/lua/telescope/_extensions/kulala.lua index a2e9b74..c1fedd5 100644 --- a/lua/telescope/_extensions/kulala.lua +++ b/lua/telescope/_extensions/kulala.lua @@ -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 @@ -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