1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
-- Integración nativa con direnv (reemplaza direnv/direnv.vim)
-- `direnv export json` emite el diff de variables a aplicar para el cwd actual;
-- las variables a eliminar llegan como null (vim.NIL).
local M = {}
local function apply(env)
if type(env) ~= "table" then
return
end
for k, v in pairs(env) do
if v == vim.NIL then
vim.env[k] = nil
else
vim.env[k] = v
end
end
end
local function load(sync)
if vim.fn.executable("direnv") == 0 then
return
end
if sync then
-- Carga inicial sincrónica: el entorno debe estar listo antes de los LSP.
local out = vim.fn.system({ "direnv", "export", "json" })
if vim.v.shell_error ~= 0 or vim.trim(out) == "" then
return
end
local ok, env = pcall(vim.json.decode, out)
if ok then
apply(env)
end
else
vim.system({ "direnv", "export", "json" }, { text = true }, function(obj)
if obj.code ~= 0 then
return
end
local out = vim.trim(obj.stdout or "")
if out == "" then
return
end
local ok, env = pcall(vim.json.decode, out)
if ok then
vim.schedule(function()
apply(env)
end)
end
end)
end
end
function M.setup()
vim.api.nvim_create_autocmd("DirChanged", {
group = vim.api.nvim_create_augroup("direnv", { clear = true }),
callback = function()
load(false)
end,
})
load(true)
end
return M
|