feat: can load lazy plugins via placeholder `cmd`s

main
LeMarsu 2024-05-29 04:05:18 +02:00
parent 5291529e6f
commit 81d82e05a2
5 changed files with 73 additions and 11 deletions

View File

@ -38,7 +38,7 @@ A [neovim] plugin and configuration management plugin, highly inspired by [lazy]
- [X] Generate default `init.lua`
- [X] Accepts your own `init.lua`
- [ ] Lazy load your plugins
- [ ] on command
- [X] on command
- [ ] on filetype
- [ ] on event
- [ ] on keybinding
@ -149,10 +149,11 @@ The Plugin configuration object accepts the following properties:
| name | default | description |
|----------------|---------|-----------------------------------------------------------------|
| `plugin` | N/A | The plugin to load² **REQUIRED** |
| `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 |
| `dependencies` | [] | The plugin dependencies⁴ |
| `lazy` | `false` | should the plugin be loaded lazily |
| `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 |
| `dependencies` | `[]` | The plugin dependencies⁴ |
| `lazy` | `false` | Should the plugin be loaded lazily |
| `cmd` | `[]` | Command to put as place_holder to lazy load the plugin⁵ |
> ² 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
@ -166,6 +167,8 @@ The Plugin configuration object accepts the following properties:
> plugins should be loaded... when `sloth-flake` will handle asynchronous
> loading... (Soon™).
> ⁵ Setting this property implicitly set `lazy` to `true`.
### neovim (lua)
#### Using default `init.lua`

View File

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

View File

@ -24,7 +24,7 @@
extractPlugin = p: {
inherit (p) plugin;
optional = p ? lazy && p.lazy;
optional = p ? lazy && p.lazy || p ? cmd;
};
extractPlugins = map extractPlugin;

View File

@ -23,8 +23,9 @@
};
# The plugin type of dependencies
pluginType = with yants;
# let stringList = list string in
pluginType = with yants; let
stringList = list string;
in
struct "plugin" {
# Whether this plugin should be enabled. This option allows specific
# plugins to be disabled.
@ -51,7 +52,7 @@
# events = option stringList;
# List of commands on which the plugin should be loaded
# commands = option stringList;
cmd = option stringList;
# List of filetypes on which the plugin should be loaded
# filetypes = option stringList;

View File

@ -4,6 +4,7 @@ local priv = {
init = {},
import = {},
config = {},
shim = {},
},
}
@ -62,7 +63,12 @@ function M.is_imported(name)
return priv.is.import[name] or false
end
function M.is_loaded(name)
return priv.is.config[name] or false
end
function M.load(name)
unshim_plugin(name)
M.init(name)
M.import(name)
M.config(name)
@ -86,6 +92,49 @@ function M.non_lazy_deps()
end):totable()
end
function M.lazy_deps()
return M.deps_iter_by(function(dep)
return dep.lazy
end):totable()
end
function lazy_load_dep(dep, cmd)
return function(param)
M.load(dep.name)
local bang = param.bang and '!' or ''
vim.cmd(cmd .. bang .. ' ' .. param.args)
end
end
function shim_plugin(dep)
if priv.is.shim[dep.name] then
return
end
priv.is.shim[dep.name] = true
if dep.cmd then
for _, cmd in ipairs(dep.cmd) do
vim.api.nvim_create_user_command(cmd, lazy_load_dep(dep, cmd), {
desc = "Sloth-flake placeholder for plugin " .. dep.name,
nargs = '*',
bang = true,
})
end
end
end
function unshim_plugin(name)
local dep = M.get(name)
if not priv.is.shim[name] then
return
end
priv.is.shim[name] = nil
if dep.cmd then
for _, cmd in ipairs(dep.cmd) do
vim.api.nvim_del_user_command(cmd)
end
end
end
function M.setup(config)
local post_init = config and config.post_init or function() end
@ -93,6 +142,11 @@ function M.setup(config)
post_init()
M.import_non_lazy()
M.config_non_lazy()
local lazy_deps = M.lazy_deps()
for _, dep in ipairs(lazy_deps) do
shim_plugin(dep)
end
end
return M