From 81d82e05a295cb7fc6a5eb563651af1e66fc8fa2 Mon Sep 17 00:00:00 2001 From: LeMarsu Date: Wed, 29 May 2024 04:05:18 +0200 Subject: [PATCH] feat: can load lazy plugins via placeholder `cmd`s --- README.md | 13 ++++++---- lib/deps.nix | 8 ++++-- lib/mkNeovimPkg.nix | 2 +- lib/types.nix | 7 +++--- lua/sloth-flake/init.lua | 54 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 73 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index bf39cd3..4a52bcc 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/lib/deps.nix b/lib/deps.nix index 621dee1..ad4f67a 100644 --- a/lib/deps.nix +++ b/lib/deps.nix @@ -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); diff --git a/lib/mkNeovimPkg.nix b/lib/mkNeovimPkg.nix index 0b5f14e..3068115 100644 --- a/lib/mkNeovimPkg.nix +++ b/lib/mkNeovimPkg.nix @@ -24,7 +24,7 @@ extractPlugin = p: { inherit (p) plugin; - optional = p ? lazy && p.lazy; + optional = p ? lazy && p.lazy || p ? cmd; }; extractPlugins = map extractPlugin; diff --git a/lib/types.nix b/lib/types.nix index e80a032..63ad0b9 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -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; diff --git a/lua/sloth-flake/init.lua b/lua/sloth-flake/init.lua index f41ad13..20352cc 100644 --- a/lua/sloth-flake/init.lua +++ b/lua/sloth-flake/init.lua @@ -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