From cece8f646414e4b1601d8efa41fd4637d00a110c Mon Sep 17 00:00:00 2001 From: LeMarsu Date: Thu, 30 May 2024 00:28:07 +0200 Subject: [PATCH] feat: loading plugins respect dependencies option --- README.md | 14 +++++++------- lib/deps.nix | 8 ++++---- lua/sloth-flake/init.lua | 17 ++++++++++++++++- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 4a52bcc..12aa317 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ A [neovim] plugin and configuration management plugin, highly inspired by [lazy] - [ ] on filetype - [ ] on event - [ ] on keybinding -- [ ] load plugins in order (via plugin `dependencies` property) +- [X] load plugins in order (via plugin `dependencies` property) - [ ] Generate spell files on build (maybe) ## SemVer @@ -159,13 +159,13 @@ The Plugin configuration object accepts the following properties: > `src` as properties. The latter will be used to create a nix package of your > plugin on the fly. -> ³ Not yet implemented. The function is called, but the plugin is already loaded. +> ³ When the plugin is not lazy, the `init` function is called after the plugin +> is loaded as all non lazy plugin are loaded automatically. -> ⁴ Not yet implemented. `nix` handles the installation of your plugin, -> therefore, this list is **NOT** to declare dependencies that the nix package -> of the plugin doesn't know. This will tell `sloth-flake` in what order your -> plugins should be loaded... when `sloth-flake` will handle asynchronous -> loading... (Soon™). +> ⁴ `nix` handles the installation of your plugin, therefore, this list is +> **NOT** to declare dependencies that the nix package of the plugin doesn't +> know. This will tell `sloth-flake` in what order your plugins should be +> loaded. > ⁵ Setting this property implicitly set `lazy` to `true`. diff --git a/lib/deps.nix b/lib/deps.nix index 048119b..13eec91 100644 --- a/lib/deps.nix +++ b/lib/deps.nix @@ -109,12 +109,12 @@ memo // { ${name} = - {name = pluginName plugin.plugin;} + { + name = pluginName plugin.plugin; + dependencies = map pluginName plugin.dependencies; + } // (mkTypeFn "init") // (mkTypeFn "config") - // (optionalAttrs (plugin.dependencies != []) { - dependencies = map pluginName plugin.dependencies; - }) // (optionalAttrs plugin.lazy { lazy = true; }) diff --git a/lua/sloth-flake/init.lua b/lua/sloth-flake/init.lua index 20352cc..039f683 100644 --- a/lua/sloth-flake/init.lua +++ b/lua/sloth-flake/init.lua @@ -33,16 +33,24 @@ function M.config_non_lazy() end function load_fn(type) - return function(name) + local function fn(name) local dep = M.get(name) + if dep == nil then + -- TODO Handle missing deps + return + end if priv.is[type][name] then return end priv.is[type][name] = true if dep[type] ~= nil then + for _, child in ipairs(dep.dependencies) do + fn(child) + end dep[type]() end end + return fn end M.init = load_fn('init') @@ -53,8 +61,15 @@ function M.import(name) return end local plugin = M.get(name) + if plugin == nil then + -- TODO Handle missing deps + return + end priv.is.import[name] = true if plugin.lazy then + for _, dep in ipairs(plugin.dependencies) do + M.import(dep) + end vim.cmd("packadd " .. name) end end