Skip to content

Commit

Permalink
fix: some bugs and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioRibera committed Dec 26, 2023
1 parent a8d0f66 commit 27fe56a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lua/cmp-dotenv/dotenv.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,22 @@ function M.as_completion()
local opts = option.get()

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 then
docs = v.docs .. '\n\n' .. docs
end

table.insert(M.completion_items, {
label = key,
-- Evaluate the environment variable if `eval_on_confirm` is true
insertText = opts.eval_on_confirm and v.value or key,
word = key,
-- Show documentation if `show_documentation_window` is true
documentation = opts.show_documentation and {
kind = opts.documentation_kind,
value = v.docs .. opts.show_content_on_docs and '\n\nContent: ' .. v.value or '',
value = docs,
},
kind = opts.item_kind,
})
Expand Down
25 changes: 25 additions & 0 deletions spec/dotenv_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@ local option = require('cmp-dotenv.option')
local default_opts = {
path = './spec/dotenv',
load_shell = false,
eval_on_confirm = false,
dotenv_environment = '.*', -- load all .env files
file_priority = function(a, b)
-- Prioritizing local files
return a:upper() < b:upper()
end,
}

local function tbl_find(t, n)
for _, value in ipairs(t) do
if value.word == n then
return value
end
end
end

describe('Load dotenv workspace', function()
it('Load env text', function()
dotenv.env_variables = {}
Expand Down Expand Up @@ -43,3 +52,19 @@ describe('Load dotenv workspace', function()
assert.are.same(2, vim.tbl_count(all_env))
end)
end)

describe('Completion dotenv workspace', function()
it('Load completion table', function()
dotenv.env_variables = {}
option.set(default_opts)
dotenv.load()
local all = dotenv.as_completion()

assert.are.same(3, vim.tbl_count(all))

local variable = tbl_find(all, 'VARIABLE')
assert.are.same('VARIABLE', variable.label)
assert.are.same('VARIABLE', variable.insertText)
assert.are.same('Local Documentation\n\nContent: Hello From Local', variable.documentation.value)
end)
end)

0 comments on commit 27fe56a

Please sign in to comment.