Skip to content

Commit

Permalink
fix: force load envs
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioRibera committed Jan 1, 2024
1 parent 37d8abe commit ab722ec
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 37 deletions.
36 changes: 7 additions & 29 deletions lua/cmp-dotenv/dotenv.lua
Original file line number Diff line number Diff line change
@@ -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 = {}
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -91,7 +69,7 @@ function M.load(force, options)
end
end

build_completions(opts)
utils.build_completions(M, opts)
end

function M.as_completion()
Expand Down
33 changes: 33 additions & 0 deletions lua/cmp-dotenv/utils.lua
Original file line number Diff line number Diff line change
@@ -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
12 changes: 4 additions & 8 deletions spec/dotenv_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -47,18 +45,16 @@ 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)
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))
Expand Down

0 comments on commit ab722ec

Please sign in to comment.