Skip to content

Commit e73626a

Browse files
committed
feat: initial commit
0 parents  commit e73626a

File tree

8 files changed

+1052
-0
lines changed

8 files changed

+1052
-0
lines changed

lua/lazy/cache.lua

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
local cache_file = vim.fn.stdpath("cache") .. "/lazy/cache.mpack"
2+
vim.fn.mkdir(vim.fn.fnamemodify(cache_file, ":p:h"), "p")
3+
4+
local M = {}
5+
---@alias CacheEntry {hash:string, chunk:string, used:boolean}
6+
7+
---@type table<string, CacheEntry>
8+
M.cache = {}
9+
M.dirty = false
10+
M.did_setup = false
11+
12+
function M.hash(modpath)
13+
local stat = vim.loop.fs_stat(modpath)
14+
if stat then
15+
return stat.mtime.sec .. stat.mtime.nsec .. stat.size
16+
end
17+
error("Could not hash " .. modpath)
18+
end
19+
20+
function M.load_cache()
21+
local f = io.open(cache_file, "rb")
22+
if f then
23+
M.cache = vim.mpack.decode(f:read("*a")) or {}
24+
f:close()
25+
end
26+
end
27+
28+
function M.save_cache()
29+
if M.dirty then
30+
for key, entry in pairs(M.cache) do
31+
if not entry.used then
32+
M.cache[key] = nil
33+
end
34+
entry.used = nil
35+
end
36+
local f = assert(io.open(cache_file, "wb"))
37+
f:write(vim.mpack.encode(M.cache))
38+
f:close()
39+
end
40+
end
41+
42+
function M.setup()
43+
M.load_cache()
44+
vim.api.nvim_create_autocmd("VimLeave", {
45+
callback = function()
46+
M.save_cache()
47+
end,
48+
})
49+
end
50+
51+
function M.load(modpath, modname)
52+
if not M.did_setup then
53+
M.setup()
54+
M.did_setup = true
55+
end
56+
if type(package.loaded[modname]) ~= "table" then
57+
---@type fun()?, string?
58+
local chunk, err
59+
local entry = M.cache[modname]
60+
61+
if entry and M.hash(modpath) == entry.hash then
62+
entry.used = true
63+
chunk, err = loadstring(entry.chunk, "@" .. modpath)
64+
end
65+
66+
-- not cached, or failed to load chunk
67+
if not chunk then
68+
vim.schedule(function()
69+
vim.notify("not cached")
70+
end)
71+
chunk, err = loadfile(modpath)
72+
if chunk then
73+
M.cache[modname] = { hash = M.hash(modpath), chunk = string.dump(chunk, true), used = true }
74+
M.dirty = true
75+
end
76+
end
77+
78+
if not chunk then
79+
error(err)
80+
end
81+
---@diagnostic disable-next-line: no-unknown
82+
package.loaded[modname] = chunk()
83+
end
84+
return package.loaded[modname]
85+
end
86+
87+
return M

lua/lazy/config.lua

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
local Util = require("lazy.util")
2+
3+
local M = {}
4+
5+
---@class LazyConfig
6+
M.defaults = {
7+
opt = true,
8+
plugins = {},
9+
plugins_local = {
10+
path = vim.fn.expand("~/projects"),
11+
patterns = {
12+
"folke",
13+
},
14+
},
15+
plugins_config = {
16+
module = "plugins",
17+
path = vim.fn.stdpath("config") .. "/lua/plugins",
18+
},
19+
package_path = vim.fn.stdpath("data") .. "/site/pack/lazy",
20+
}
21+
22+
M.ns = vim.api.nvim_create_namespace("lazy")
23+
24+
---@type table<string, LazyPlugin>
25+
M.plugins = {}
26+
27+
---@type LazyConfig
28+
M.options = {}
29+
30+
---@type table<string, string>
31+
M.has_config = {}
32+
33+
---@param opts? LazyConfig
34+
function M.setup(opts)
35+
M.options = vim.tbl_deep_extend("force", M.defaults, opts or {})
36+
37+
vim.fn.mkdir(M.options.package_path, "p")
38+
39+
for _, entry in ipairs(Util.scandir(M.options.plugins_config.path)) do
40+
local name, modpath
41+
42+
if entry.type == "file" then
43+
modpath = entry.path
44+
name = entry.name:match("(.*)%.lua")
45+
elseif entry.type == "directory" then
46+
modpath = M.options.plugins_config.path .. "/" .. entry.name .. "/init.lua"
47+
if vim.loop.fs_stat(modpath) then
48+
name = entry.name
49+
end
50+
end
51+
52+
if name then
53+
M.has_config[M.options.plugins_config.module .. "." .. name] = modpath
54+
end
55+
end
56+
57+
vim.api.nvim_create_autocmd("User", {
58+
pattern = "VeryLazy",
59+
once = true,
60+
callback = function()
61+
-- require("lazy.view").setup()
62+
end,
63+
})
64+
65+
Util.very_lazy()
66+
end
67+
68+
return M

lua/lazy/init.lua

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
local M = {}
2+
3+
---@param opts? LazyConfig
4+
function M.setup(opts)
5+
--FIXME: preload()
6+
7+
local Util = require("lazy.util")
8+
local Config = require("lazy.config")
9+
local Plugin = require("lazy.plugin")
10+
11+
Util.track("lazy_setup")
12+
13+
Util.track("lazy_config")
14+
Config.setup(opts)
15+
Util.track()
16+
17+
Util.track("plugin_normalize")
18+
Plugin.normalize(Config.options.plugins)
19+
if not Config.plugins.lazy then
20+
Plugin.plugin({
21+
"folke/lazy.nvim",
22+
opt = false,
23+
})
24+
end
25+
Util.track()
26+
27+
Util.track("plugin_process")
28+
Plugin.process()
29+
Util.track()
30+
31+
Util.track("lazy_install")
32+
for _, plugin in pairs(Config.plugins) do
33+
if not plugin.installed then
34+
-- require("lazy.manager").install({
35+
-- wait = true,
36+
-- })
37+
break
38+
end
39+
end
40+
Util.track()
41+
42+
Util.track("loader_setup")
43+
local Loader = require("lazy.loader")
44+
Loader.setup()
45+
Util.track()
46+
47+
Loader.init_plugins()
48+
49+
Util.track() -- end setup
50+
vim.cmd("do User LazyDone")
51+
end
52+
53+
function M.stats()
54+
local ret = {
55+
count = 0,
56+
loaded = 0,
57+
}
58+
59+
for _, plugin in pairs(require("lazy.config").plugins) do
60+
ret.count = ret.count + 1
61+
62+
if plugin.loaded then
63+
ret.loaded = ret.loaded + 1
64+
end
65+
end
66+
67+
return ret
68+
end
69+
70+
return M

0 commit comments

Comments
 (0)