mirror of
https://github.com/folke/lazy.nvim.git
synced 2025-04-19 04:46:45 +00:00
style: formatting
This commit is contained in:
parent
e73626a344
commit
0219a531ed
9 changed files with 698 additions and 695 deletions
|
@ -10,78 +10,78 @@ M.dirty = false
|
|||
M.did_setup = false
|
||||
|
||||
function M.hash(modpath)
|
||||
local stat = vim.loop.fs_stat(modpath)
|
||||
if stat then
|
||||
return stat.mtime.sec .. stat.mtime.nsec .. stat.size
|
||||
end
|
||||
error("Could not hash " .. modpath)
|
||||
local stat = vim.loop.fs_stat(modpath)
|
||||
if stat then
|
||||
return stat.mtime.sec .. stat.mtime.nsec .. stat.size
|
||||
end
|
||||
error("Could not hash " .. modpath)
|
||||
end
|
||||
|
||||
function M.load_cache()
|
||||
local f = io.open(cache_file, "rb")
|
||||
if f then
|
||||
M.cache = vim.mpack.decode(f:read("*a")) or {}
|
||||
f:close()
|
||||
end
|
||||
local f = io.open(cache_file, "rb")
|
||||
if f then
|
||||
M.cache = vim.mpack.decode(f:read("*a")) or {}
|
||||
f:close()
|
||||
end
|
||||
end
|
||||
|
||||
function M.save_cache()
|
||||
if M.dirty then
|
||||
for key, entry in pairs(M.cache) do
|
||||
if not entry.used then
|
||||
M.cache[key] = nil
|
||||
end
|
||||
entry.used = nil
|
||||
end
|
||||
local f = assert(io.open(cache_file, "wb"))
|
||||
f:write(vim.mpack.encode(M.cache))
|
||||
f:close()
|
||||
end
|
||||
if M.dirty then
|
||||
for key, entry in pairs(M.cache) do
|
||||
if not entry.used then
|
||||
M.cache[key] = nil
|
||||
end
|
||||
entry.used = nil
|
||||
end
|
||||
local f = assert(io.open(cache_file, "wb"))
|
||||
f:write(vim.mpack.encode(M.cache))
|
||||
f:close()
|
||||
end
|
||||
end
|
||||
|
||||
function M.setup()
|
||||
M.load_cache()
|
||||
vim.api.nvim_create_autocmd("VimLeave", {
|
||||
callback = function()
|
||||
M.save_cache()
|
||||
end,
|
||||
})
|
||||
M.load_cache()
|
||||
vim.api.nvim_create_autocmd("VimLeave", {
|
||||
callback = function()
|
||||
M.save_cache()
|
||||
end,
|
||||
})
|
||||
end
|
||||
|
||||
function M.load(modpath, modname)
|
||||
if not M.did_setup then
|
||||
M.setup()
|
||||
M.did_setup = true
|
||||
end
|
||||
if type(package.loaded[modname]) ~= "table" then
|
||||
---@type fun()?, string?
|
||||
local chunk, err
|
||||
local entry = M.cache[modname]
|
||||
if not M.did_setup then
|
||||
M.setup()
|
||||
M.did_setup = true
|
||||
end
|
||||
if type(package.loaded[modname]) ~= "table" then
|
||||
---@type fun()?, string?
|
||||
local chunk, err
|
||||
local entry = M.cache[modname]
|
||||
|
||||
if entry and M.hash(modpath) == entry.hash then
|
||||
entry.used = true
|
||||
chunk, err = loadstring(entry.chunk, "@" .. modpath)
|
||||
end
|
||||
if entry and M.hash(modpath) == entry.hash then
|
||||
entry.used = true
|
||||
chunk, err = loadstring(entry.chunk, "@" .. modpath)
|
||||
end
|
||||
|
||||
-- not cached, or failed to load chunk
|
||||
if not chunk then
|
||||
vim.schedule(function()
|
||||
vim.notify("not cached")
|
||||
end)
|
||||
chunk, err = loadfile(modpath)
|
||||
if chunk then
|
||||
M.cache[modname] = { hash = M.hash(modpath), chunk = string.dump(chunk, true), used = true }
|
||||
M.dirty = true
|
||||
end
|
||||
end
|
||||
-- not cached, or failed to load chunk
|
||||
if not chunk then
|
||||
vim.schedule(function()
|
||||
vim.notify("not cached")
|
||||
end)
|
||||
chunk, err = loadfile(modpath)
|
||||
if chunk then
|
||||
M.cache[modname] = { hash = M.hash(modpath), chunk = string.dump(chunk, true), used = true }
|
||||
M.dirty = true
|
||||
end
|
||||
end
|
||||
|
||||
if not chunk then
|
||||
error(err)
|
||||
end
|
||||
---@diagnostic disable-next-line: no-unknown
|
||||
package.loaded[modname] = chunk()
|
||||
end
|
||||
return package.loaded[modname]
|
||||
if not chunk then
|
||||
error(err)
|
||||
end
|
||||
---@diagnostic disable-next-line: no-unknown
|
||||
package.loaded[modname] = chunk()
|
||||
end
|
||||
return package.loaded[modname]
|
||||
end
|
||||
|
||||
return M
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue