Skip to content

Commit

Permalink
feat: load options from cmp and automatic load envs
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioRibera committed Dec 26, 2023
1 parent 0848a4f commit c6434d3
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 26 deletions.
2 changes: 2 additions & 0 deletions after/plugin/cmp-dotenv.lua
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
require('cmp').register_source('dotenv', require('cmp-dotenv').new())

require('cmp-dotenv.dotenv').load()
10 changes: 7 additions & 3 deletions lua/cmp-dotenv/dotenv.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
local load = require('cmp-dotenv.load')
local option = require('cmp-dotenv.option')

local M = {}

M.completion_items = {}
Expand All @@ -21,10 +23,11 @@ function M.set_env_variable(name, value, docs)
M.env_variables[name] = { value = value, docs = docs }
end

function M.load(opts)
function M.load()
if vim.tbl_count(M.env_variables) > 0 then
return
end
local opts = option.get()

local raw_files = vim.fn.globpath(opts.path, '.env*', false, true)
local files = vim.tbl_filter(function(v)
Expand All @@ -49,10 +52,11 @@ function M.load(opts)
end
end

function M.as_completion(opts)
function M.as_completion()
if vim.tbl_count(M.completion_items) > 0 then
return M.completion_items
end
local opts = option.get()

for key, value in pairs(M.env_variables) do
table.insert(M.completion_items, {
Expand All @@ -63,7 +67,7 @@ function M.as_completion(opts)
-- Show documentation if `show_documentation_window` is true
documentation = opts.show_documentation and {
kind = opts.documentation_kind,
value = value.docs,
value = value.docs .. '\n\nContent: ' .. value,
},
kind = opts.item_kind,
})
Expand Down
25 changes: 5 additions & 20 deletions lua/cmp-dotenv/init.lua
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
local cmp = require('cmp')
local dotenv = require('cmp-dotenv.dotenv')
local option = require('cmp-dotenv.option')

local source = {}

local defaults = {
path = '.',
load_shell = true,
item_kind = cmp.lsp.CompletionItemKind.Variable,
eval_on_confirm = false,
show_documentation = true,
documentation_kind = 'markdown',
dotenv_environment = '.*', -- local,example or .* for any
file_priority = function(a, b)
return a:upper() < b:upper()
end,
}

source.new = function()
return setmetatable({}, { __index = source })
end
Expand All @@ -24,8 +11,8 @@ source.get_keyword_pattern = function()
return [[\k\+]]
end

source._validate_option = function(_, params)
local opt = vim.tbl_deep_extend('keep', params.option, defaults)
source._validate_option = function(_, _)
local opt = option.get()
vim.validate {
path = { opt.path, 'string' },
load_shell = { opt.load_shell, 'boolean' },
Expand All @@ -39,10 +26,8 @@ source._validate_option = function(_, params)
return opt
end

source.complete = function(_, params, callback)
local opt = vim.tbl_deep_extend('keep', params.option, defaults)
dotenv.load(opt)
callback(dotenv.as_completion(opt))
source.complete = function(_, _, callback)
callback(dotenv.as_completion())
end

return source
29 changes: 29 additions & 0 deletions lua/cmp-dotenv/option.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
local cmp = require('cmp')
local config = require('cmp.config')

local option = {}
option.opts = nil

local defaults = {
path = '.',
load_shell = true,
item_kind = cmp.lsp.CompletionItemKind.Variable,
eval_on_confirm = false,
show_documentation = true,
documentation_kind = 'markdown',
dotenv_environment = '.*', -- local,example or .* for any
file_priority = function(a, b)
return a:upper() < b:upper()
end,
}

function option.set(options)
option.opts = options
end

function option.get(options)
local opt = options or option.opts or config.get_source_config('dotenv').option
return vim.tbl_deep_extend('keep', opt or {}, defaults)
end

return option
10 changes: 7 additions & 3 deletions spec/dotenv_spec.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local dotenv = require('cmp-dotenv.dotenv')
local option = require('cmp-dotenv.option')

local default_opts = {
path = './spec/dotenv',
Expand All @@ -13,7 +14,8 @@ local default_opts = {
describe('Load dotenv workspace', function()
it('Load env text', function()
dotenv.env_variables = {}
dotenv.load(default_opts)
option.set(default_opts)
dotenv.load()
local all_env = dotenv.get_all_env()
assert.are.same(3, vim.tbl_count(all_env))
assert.are.same({ value = 'Hello From Local', docs = 'Local Documentation' }, all_env.VARIABLE)
Expand All @@ -24,7 +26,8 @@ describe('Load dotenv workspace', function()
it('Load local env variables', function()
dotenv.env_variables = {}
local opt = vim.tbl_deep_extend('keep', { dotenv_environment = 'local' }, default_opts)
dotenv.load(opt)
option.set(opt)
dotenv.load()
local all_env = dotenv.get_all_env()
assert.are.same(3, vim.tbl_count(all_env))
assert.are.same({ value = 'Hello From Local', docs = 'Local Documentation' }, all_env.VARIABLE)
Expand All @@ -34,7 +37,8 @@ describe('Load dotenv workspace', function()
it('Load example env variables', function()
dotenv.env_variables = {}
local opt = vim.tbl_deep_extend('keep', { dotenv_environment = 'example' }, default_opts)
dotenv.load(opt)
option.set(opt)
dotenv.load()
local all_env = dotenv.get_all_env()
assert.are.same(2, vim.tbl_count(all_env))
end)
Expand Down

0 comments on commit c6434d3

Please sign in to comment.