diff --git a/README.md b/README.md index 12aa317..2a3d0cb 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/deps.nix b/lib/deps.nix index 13eec91..58fa734 100644 --- a/lib/deps.nix +++ b/lib/deps.nix @@ -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); diff --git a/lib/types.nix b/lib/types.nix index 63ad0b9..fd54a11 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -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; diff --git a/lua/sloth-flake/init.lua b/lua/sloth-flake/init.lua index 039f683..c427ee5 100644 --- a/lua/sloth-flake/init.lua +++ b/lua/sloth-flake/init.lua @@ -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)