Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Nov 20, 2022
0 parents commit e73626a
Show file tree
Hide file tree
Showing 8 changed files with 1,052 additions and 0 deletions.
87 changes: 87 additions & 0 deletions lua/lazy/cache.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
local cache_file = vim.fn.stdpath("cache") .. "/lazy/cache.mpack"
vim.fn.mkdir(vim.fn.fnamemodify(cache_file, ":p:h"), "p")

local M = {}
---@alias CacheEntry {hash:string, chunk:string, used:boolean}

---@type table<string, CacheEntry>
M.cache = {}
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)
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
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
end

function M.setup()
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 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

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
68 changes: 68 additions & 0 deletions lua/lazy/config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
local Util = require("lazy.util")

local M = {}

---@class LazyConfig
M.defaults = {
opt = true,
plugins = {},
plugins_local = {
path = vim.fn.expand("~/projects"),
patterns = {
"folke",
},
},
plugins_config = {
module = "plugins",
path = vim.fn.stdpath("config") .. "/lua/plugins",
},
package_path = vim.fn.stdpath("data") .. "/site/pack/lazy",
}

M.ns = vim.api.nvim_create_namespace("lazy")

---@type table<string, LazyPlugin>
M.plugins = {}

---@type LazyConfig
M.options = {}

---@type table<string, string>
M.has_config = {}

---@param opts? LazyConfig
function M.setup(opts)
M.options = vim.tbl_deep_extend("force", M.defaults, opts or {})

vim.fn.mkdir(M.options.package_path, "p")

for _, entry in ipairs(Util.scandir(M.options.plugins_config.path)) do
local name, modpath

if entry.type == "file" then
modpath = entry.path
name = entry.name:match("(.*)%.lua")
elseif entry.type == "directory" then
modpath = M.options.plugins_config.path .. "/" .. entry.name .. "/init.lua"
if vim.loop.fs_stat(modpath) then
name = entry.name
end
end

if name then
M.has_config[M.options.plugins_config.module .. "." .. name] = modpath
end
end

vim.api.nvim_create_autocmd("User", {
pattern = "VeryLazy",
once = true,
callback = function()
-- require("lazy.view").setup()
end,
})

Util.very_lazy()
end

return M
70 changes: 70 additions & 0 deletions lua/lazy/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
local M = {}

---@param opts? LazyConfig
function M.setup(opts)
--FIXME: preload()

local Util = require("lazy.util")
local Config = require("lazy.config")
local Plugin = require("lazy.plugin")

Util.track("lazy_setup")

Util.track("lazy_config")
Config.setup(opts)
Util.track()

Util.track("plugin_normalize")
Plugin.normalize(Config.options.plugins)
if not Config.plugins.lazy then
Plugin.plugin({
"folke/lazy.nvim",
opt = false,
})
end
Util.track()

Util.track("plugin_process")
Plugin.process()
Util.track()

Util.track("lazy_install")
for _, plugin in pairs(Config.plugins) do
if not plugin.installed then
-- require("lazy.manager").install({
-- wait = true,
-- })
break
end
end
Util.track()

Util.track("loader_setup")
local Loader = require("lazy.loader")
Loader.setup()
Util.track()

Loader.init_plugins()

Util.track() -- end setup
vim.cmd("do User LazyDone")
end

function M.stats()
local ret = {
count = 0,
loaded = 0,
}

for _, plugin in pairs(require("lazy.config").plugins) do
ret.count = ret.count + 1

if plugin.loaded then
ret.loaded = ret.loaded + 1
end
end

return ret
end

return M

0 comments on commit e73626a

Please sign in to comment.