feat: can load lazy plugins on FileType autocommand

main
LeMarsu 2024-05-30 01:18:13 +02:00
parent cece8f6464
commit 805b41ed58
4 changed files with 43 additions and 6 deletions

View File

@ -37,9 +37,9 @@ A [neovim] plugin and configuration management plugin, highly inspired by [lazy]
- [X] Generate nix package from local files
- [X] Generate default `init.lua`
- [X] Accepts your own `init.lua`
- [ ] Lazy load your plugins
- [X] Lazy load your plugins
- [X] on command
- [ ] on filetype
- [X] on filetype
- [ ] on event
- [ ] on keybinding
- [X] load plugins in order (via plugin `dependencies` property)
@ -154,6 +154,7 @@ The Plugin configuration object accepts the following properties:
| `dependencies` | `[]` | The plugin dependencies⁴ |
| `lazy` | `false` | Should the plugin be loaded lazily |
| `cmd` | `[]` | Command to put as place_holder to lazy load the plugin⁵ |
| `ft` | `[]` | Filetype to watch 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

View File

@ -21,6 +21,7 @@
dependencies = [];
lazy = false;
cmd = [];
ft = [];
};
remotePluginToNeovimPlugin = p:
@ -46,7 +47,8 @@
p
// rec {
hasCommands = p.cmd != [];
lazy = p.lazy || hasCommands;
hasFileTypes = p.ft != [];
lazy = p.lazy || hasCommands || hasFileTypes;
};
normalizeOrImportPlugin = dep:
@ -120,6 +122,9 @@
})
// (optionalAttrs plugin.hasCommands {
inherit (plugin) cmd;
})
// (optionalAttrs plugin.hasFileTypes {
inherit (plugin) ft;
});
};
pluginsLuaDef = plugins: lua.nix2lua (foldl' pluginLuaDef {} plugins);

View File

@ -55,7 +55,7 @@
cmd = option stringList;
# List of filetypes on which the plugin should be loaded
# filetypes = option stringList;
ft = option stringList;
# List of keystrokes on which the plugin should be loaded
# keys = option stringList;

View File

@ -113,7 +113,7 @@ function M.lazy_deps()
end):totable()
end
function lazy_load_dep(dep, cmd)
function lazy_load_cmd(dep, cmd)
return function(param)
M.load(dep.name)
local bang = param.bang and '!' or ''
@ -121,20 +121,46 @@ function lazy_load_dep(dep, cmd)
end
end
function lazy_load_ft(dep)
return function(param)
M.load(dep.name)
print(param.match)
vim.api.nvim_exec_autocmds('FileType', {
pattern = param.match,
})
end
end
function augroup_name(dep)
return "Sloth-plugin-" .. dep.name
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), {
vim.api.nvim_create_user_command(cmd, lazy_load_cmd(dep, cmd), {
desc = "Sloth-flake placeholder for plugin " .. dep.name,
nargs = '*',
bang = true,
})
end
end
if dep.ft then
local group_id = vim.api.nvim_create_augroup(augroup_name(dep), {
clear = true,
})
vim.api.nvim_create_autocmd('FileType', {
group = group_id,
pattern = dep.ft,
callback = lazy_load_ft(dep)
})
end
end
function unshim_plugin(name)
@ -143,11 +169,16 @@ function unshim_plugin(name)
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
if dep.ft then
vim.api.nvim_del_augroup_by_name(augroup_name(dep))
end
end
function M.setup(config)