feat: can load lazy plugins

main
LeMarsu 2024-05-29 03:27:25 +02:00
parent 067dec167d
commit 5291529e6f
5 changed files with 73 additions and 46 deletions

View File

@ -152,6 +152,7 @@ The Plugin configuration object accepts the following properties:
| `init` | `null` | lua code (as string of path) to call before loading the plugin³ | | `init` | `null` | lua code (as string of path) to call before loading the plugin³ |
| `config` | `null` | lua code (as string of path) to call after loading the plugin | | `config` | `null` | lua code (as string of path) to call after loading the plugin |
| `dependencies` | [] | The plugin dependencies⁴ | | `dependencies` | [] | The plugin dependencies⁴ |
| `lazy` | `false` | should the plugin be loaded lazily |
> ² The plugin can be either a nix package or an object with only `name` and > ² The plugin can be either a nix package or an object with only `name` and
> `src` as properties. The latter will be used to create a nix package of your > `src` as properties. The latter will be used to create a nix package of your

View File

@ -97,6 +97,7 @@
then plugin.pname then plugin.pname
else plugin.name; else plugin.name;
hasDeps = plugin ? dependencies && plugin.dependencies != []; hasDeps = plugin ? dependencies && plugin.dependencies != [];
isLazy = plugin ? lazy && plugin.lazy;
name = pluginName plugin.plugin; name = pluginName plugin.plugin;
in in
memo memo
@ -107,6 +108,9 @@
// (mkTypeFn "config") // (mkTypeFn "config")
// (optionalAttrs hasDeps { // (optionalAttrs hasDeps {
dependencies = map pluginName plugin.dependencies; dependencies = map pluginName plugin.dependencies;
})
// (optionalAttrs isLazy {
lazy = true;
}); });
}; };
pluginsLuaDef = plugins: lua.nix2lua (foldl' pluginLuaDef {} plugins); pluginsLuaDef = plugins: lua.nix2lua (foldl' pluginLuaDef {} plugins);

View File

@ -22,7 +22,11 @@
runtimePlugin.plugin = deps.mkRuntimePlugin runtime; runtimePlugin.plugin = deps.mkRuntimePlugin runtime;
plugins = deps.normalizePlugins (dependencies ++ [runtimePlugin sloth-flake]); plugins = deps.normalizePlugins (dependencies ++ [runtimePlugin sloth-flake]);
extractPlugin = map (p: p.plugin); extractPlugin = p: {
inherit (p) plugin;
optional = p ? lazy && p.lazy;
};
extractPlugins = map extractPlugin;
customRC = let customRC = let
rc = ({init ? ../lua/default_init.lua, ...}: init) runtime; rc = ({init ? ../lua/default_init.lua, ...}: init) runtime;
@ -32,7 +36,7 @@
neovimConfig = neovimConfig =
pkgs.neovimUtils.makeNeovimConfig { pkgs.neovimUtils.makeNeovimConfig {
inherit customRC; inherit customRC;
plugins = extractPlugin plugins; plugins = extractPlugins plugins;
} }
// {luaRcContent = customRC;}; // {luaRcContent = customRC;};
pkg = pkgs.wrapNeovimUnstable package (removeAttrs neovimConfig ["manifestRc" "neovimRcContent"]); pkg = pkgs.wrapNeovimUnstable package (removeAttrs neovimConfig ["manifestRc" "neovimRcContent"]);

View File

@ -45,7 +45,7 @@
dependencies = option (list drv); dependencies = option (list drv);
# Should this plugin be load lazily ? # Should this plugin be load lazily ?
# lazy = option bool; lazy = option bool;
# List of events on which the plugin should be loaded # List of events on which the plugin should be loaded
# events = option stringList; # events = option stringList;

View File

@ -1,6 +1,10 @@
local deps = require 'sloth-flake.deps' local deps = require 'sloth-flake.deps'
local priv = { local priv = {
is_loaded = {} is = {
init = {},
import = {},
config = {},
},
} }
local M = {} local M = {}
@ -9,72 +13,86 @@ function M.get(name)
return deps[name] return deps[name]
end end
function M.init_all() function M.init_non_lazy()
for k, v in pairs(deps) do for _, dep in ipairs(M.non_lazy_deps()) do
M.init(k) M.init(dep.name)
end end
end end
function M.config_all() function M.import_non_lazy()
for k, v in pairs(deps) do for _, dep in ipairs(M.non_lazy_deps()) do
M.config(k) M.import(dep.name)
end end
end end
function M.init(name) function M.config_non_lazy()
local dep = M.get(name) for _, dep in ipairs(M.non_lazy_deps()) do
if name == nil then M.config(dep.name)
-- TODO Report error ?
elseif dep.init ~= nil then
dep.init()
end end
end end
function load_fn(type)
return function(name)
local dep = M.get(name)
if priv.is[type][name] then
return
end
priv.is[type][name] = true
if dep[type] ~= nil then
dep[type]()
end
end
end
M.init = load_fn('init')
M.config = load_fn('config')
function M.import(name)
if M.is_imported(name) then
return
end
local plugin = M.get(name)
priv.is.import[name] = true
if plugin.lazy then
vim.cmd("packadd " .. name)
end
end
function M.is_imported(name)
return priv.is.import[name] or false
end
function M.load(name) function M.load(name)
if name == nil then M.init(name)
-- TODO Report error ? M.import(name)
elseif M.is_loaded(name) then M.config(name)
-- TODO Nothing todo
else
-- TODO Laod dynamic plugin
priv.is_loaded[name] = true
end
end end
function M.config(name) function M.dep_names()
local dep = M.get(name) return M.dep_names_by(function() return true end):totable()
if name == nil then
-- TODO Report error ?
elseif dep.config ~= nil then
dep.config()
end
end end
function M.get_dep_names() function M.dep_names_by(fn)
local ret = {} return M.deps_iter_by(fn):map(function(v) return v.name end)
for k, v in pairs(deps) do
ret[#ret + 1] = v.name
end
return ret
end end
function M.load_all() function M.deps_iter_by(fn)
for k, _v in pairs(deps) do return vim.iter(deps):map(function(k, v) return v end):filter(fn)
M.load(k)
end
end end
function M.is_loaded(name) function M.non_lazy_deps()
return priv.is_loaded[name] or false return M.deps_iter_by(function(dep)
return not dep.lazy
end):totable()
end end
function M.setup(config) function M.setup(config)
local post_init = config and config.post_init or function() end local post_init = config and config.post_init or function() end
M.init_all() M.init_non_lazy()
post_init() post_init()
M.load_all() M.import_non_lazy()
M.config_all() M.config_non_lazy()
end end
return M return M