diff --git a/lua/cmp-dotenv/dotenv.lua b/lua/cmp-dotenv/dotenv.lua index b74b531..015884e 100644 --- a/lua/cmp-dotenv/dotenv.lua +++ b/lua/cmp-dotenv/dotenv.lua @@ -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, }) diff --git a/spec/dotenv_spec.lua b/spec/dotenv_spec.lua index 6deb6a4..dcdd7b1 100644 --- a/spec/dotenv_spec.lua +++ b/spec/dotenv_spec.lua @@ -4,6 +4,7 @@ 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 @@ -11,6 +12,14 @@ local default_opts = { 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 = {} @@ -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)