-- 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