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 nix package from local files
- [X] Generate default `init.lua` - [X] Generate default `init.lua`
- [X] Accepts your own `init.lua` - [X] Accepts your own `init.lua`
- [ ] Lazy load your plugins - [X] Lazy load your plugins
- [X] on command - [X] on command
- [ ] on filetype - [X] on filetype
- [ ] on event - [ ] on event
- [ ] on keybinding - [ ] on keybinding
- [X] load plugins in order (via plugin `dependencies` property) - [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⁴ | | `dependencies` | `[]` | The plugin dependencies⁴ |
| `lazy` | `false` | Should the plugin be loaded lazily | | `lazy` | `false` | Should the plugin be loaded lazily |
| `cmd` | `[]` | Command to put as place_holder to lazy load the plugin⁵ | | `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 > ² 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

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

View File

@ -55,7 +55,7 @@
cmd = option stringList; cmd = option stringList;
# List of filetypes on which the plugin should be loaded # 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 # List of keystrokes on which the plugin should be loaded
# keys = option stringList; # keys = option stringList;

View File

@ -113,7 +113,7 @@ function M.lazy_deps()
end):totable() end):totable()
end end
function lazy_load_dep(dep, cmd) function lazy_load_cmd(dep, cmd)
return function(param) return function(param)
M.load(dep.name) M.load(dep.name)
local bang = param.bang and '!' or '' local bang = param.bang and '!' or ''
@ -121,20 +121,46 @@ function lazy_load_dep(dep, cmd)
end end
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) function shim_plugin(dep)
if priv.is.shim[dep.name] then if priv.is.shim[dep.name] then
return return
end end
priv.is.shim[dep.name] = true priv.is.shim[dep.name] = true
if dep.cmd then if dep.cmd then
for _, cmd in ipairs(dep.cmd) do 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, desc = "Sloth-flake placeholder for plugin " .. dep.name,
nargs = '*', nargs = '*',
bang = true, bang = true,
}) })
end end
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 end
function unshim_plugin(name) function unshim_plugin(name)
@ -143,11 +169,16 @@ function unshim_plugin(name)
return return
end end
priv.is.shim[name] = nil priv.is.shim[name] = nil
if dep.cmd then if dep.cmd then
for _, cmd in ipairs(dep.cmd) do for _, cmd in ipairs(dep.cmd) do
vim.api.nvim_del_user_command(cmd) vim.api.nvim_del_user_command(cmd)
end end
end end
if dep.ft then
vim.api.nvim_del_augroup_by_name(augroup_name(dep))
end
end end
function M.setup(config) function M.setup(config)