diff --git a/lua/cmp-dotenv/dotenv.lua b/lua/cmp-dotenv/dotenv.lua index eaf6a4a..5f28684 100644 --- a/lua/cmp-dotenv/dotenv.lua +++ b/lua/cmp-dotenv/dotenv.lua @@ -1,7 +1,9 @@ local load = require('cmp-dotenv.load') local option = require('cmp-dotenv.option') +local utils = require('cmp-dotenv.utils') local M = {} +M.__index = M M.files = {} M.completion_items = {} @@ -24,32 +26,8 @@ function M.set_env_variable(name, value, docs) M.env_variables[name] = { value = value, docs = docs } end -local function build_completions(opts) - for key, v in pairs(M.env_variables) do - local docs = '' - if opts.show_content_on_docs then - docs = 'Content: ' .. v.value - end - - if v.docs ~= nil then - docs = v.docs .. '\n\n' .. docs - end - - table.insert(M.completion_items, { - label = key, - insertText = opts.eval_on_confirm and v.value or key, - word = key, - documentation = opts.show_documentation and { - kind = opts.documentation_kind, - value = docs, - }, - kind = opts.item_kind, - }) - end -end - function M.load(force, options) - if vim.tbl_count(M.env_variables) > 0 or force then + if vim.tbl_count(M.env_variables) > 0 and force ~= nil and not force then return end local opts = option.get(options) @@ -68,13 +46,13 @@ function M.load(force, options) -- If the new file list is same as cached -- return - if vim.tbl_count(diff_files) == 0 and vim.tbl_count(M.env_variables) > 0 then + if vim.tbl_count(diff_files) == 0 and vim.tbl_count(M.env_variables) > 0 and force ~= nil and not force then return end M.files = files - M.env_variables = {} - M.completion_items = {} + utils.clear_table(M.env_variables) + utils.clear_table(M.completion_items) if opts.load_shell then local env_vars = vim.fn.environ() @@ -91,7 +69,7 @@ function M.load(force, options) end end - build_completions(opts) + utils.build_completions(M, opts) end function M.as_completion() diff --git a/lua/cmp-dotenv/utils.lua b/lua/cmp-dotenv/utils.lua new file mode 100644 index 0000000..d9a31a4 --- /dev/null +++ b/lua/cmp-dotenv/utils.lua @@ -0,0 +1,33 @@ +local utils = {} + +function utils.build_completions(instance, opts) + for key, v in pairs(instance.env_variables) do + local docs = '' + if opts.show_content_on_docs then + docs = 'Content: ' .. v.value + end + + if v.docs ~= nil then + docs = v.docs .. '\n\n' .. docs + end + + table.insert(instance.completion_items, { + label = key, + insertText = opts.eval_on_confirm and v.value or key, + word = key, + documentation = opts.show_documentation and { + kind = opts.documentation_kind, + value = docs, + }, + kind = opts.item_kind, + }) + end +end + +function utils.clear_table(t) + for k in pairs(t) do + t[k] = nil + end +end + +return utils diff --git a/spec/dotenv_spec.lua b/spec/dotenv_spec.lua index c602be4..66af05b 100644 --- a/spec/dotenv_spec.lua +++ b/spec/dotenv_spec.lua @@ -21,8 +21,7 @@ end describe('Load dotenv workspace', function() it('Load env text', function() - dotenv.env_variables = {} - dotenv.load(nil, default_opts) + dotenv.load(true, default_opts) local all_env = dotenv.get_all_env() assert.are.same(3, vim.tbl_count(all_env)) assert.are.same( @@ -34,9 +33,8 @@ describe('Load dotenv workspace', function() end) it('Load local env variables', function() - dotenv.env_variables = {} local opt = vim.tbl_deep_extend('keep', { dotenv_environment = 'local' }, default_opts) - dotenv.load(nil, opt) + dotenv.load(true, opt) local all_env = dotenv.get_all_env() assert.are.same(3, vim.tbl_count(all_env)) assert.are.same( @@ -47,9 +45,8 @@ describe('Load dotenv workspace', function() end) it('Load example env variables', function() - dotenv.env_variables = {} local opt = vim.tbl_deep_extend('keep', { dotenv_environment = 'example' }, default_opts) - dotenv.load(nil, opt) + dotenv.load(true, opt) local all_env = dotenv.get_all_env() assert.are.same(2, vim.tbl_count(all_env)) end) @@ -57,8 +54,7 @@ end) describe('Completion dotenv workspace', function() it('Load completion table', function() - dotenv.env_variables = {} - dotenv.load(nil, default_opts) + dotenv.load(true, default_opts) local all = dotenv.as_completion() assert.are.same(3, vim.tbl_count(all))