feat: can declare options to pass to `setup` via `setup` plugin option

dev
LeMarsu 2026-04-21 02:07:03 +02:00
parent 84305d33fd
commit 847359287b
4 changed files with 97 additions and 18 deletions

View File

@ -3,6 +3,7 @@
sloth, sloth,
... ...
}: let }: let
inherit (pkgs) callPackage;
inherit (pkgs.lib) evalModules; inherit (pkgs.lib) evalModules;
sLib = sloth.lib; sLib = sloth.lib;
in { in {
@ -10,8 +11,9 @@ in {
modules ? [], modules ? [],
specialArgs ? {}, specialArgs ? {},
}: let }: let
lua = callPackage ./lua.nix {};
moduleConfig = evalModules { moduleConfig = evalModules {
specialArgs = specialArgs // {inherit pkgs;}; specialArgs = specialArgs // {inherit pkgs lua;};
modules = modules ++ [sLib.defaultModule]; modules = modules ++ [sLib.defaultModule];
class = "sloth"; class = "sloth";
}; };

View File

@ -3,8 +3,24 @@
callModule, callModule,
... ...
}: let }: let
inherit (builtins) isPath; inherit (builtins) concatMap elemAt isPath length pathExists readDir;
inherit (pkgs.lib) fileContents fix literalExample mergeAttrsList mkOption optionalAttrs types; inherit
(pkgs.lib)
assertMsg
attrsToList
concatStringsSep
fileContents
fix
hasSuffix
literalExample
mergeAttrsList
mkOption
optional
optionalAttrs
pipe
removeSuffix
types
;
lua = callModule ../../lua.nix {}; lua = callModule ../../lua.nix {};
modules = { modules = {
@ -32,6 +48,25 @@
else content; else content;
getPluginName = plugin: plugin.pname or plugin.name; getPluginName = plugin: plugin.pname or plugin.name;
findLuaRequire = plugin: let
luaDir = "${plugin.plugin}/lua";
readOptionalDir = dir: optionalAttrs (pathExists dir) (readDir dir);
convertPath = attr:
if attr.value == "directory"
then optional (pathExists "${luaDir}/${attr.name}/init.lua") attr.name
else optional (hasSuffix ".lua" attr.name) (removeSuffix ".lua" attr.name);
requireNames = pipe plugin.plugin [
(path: "${path}/lua")
readOptionalDir
attrsToList
(concatMap convertPath)
];
in
if length requireNames == 1
then elemAt requireNames 0
else null;
mkLuaDefinition = plugin: let mkLuaDefinition = plugin: let
mkTypeFn = type: let mkTypeFn = type: let
content = textOrContent plugin.${type}; content = textOrContent plugin.${type};
@ -41,6 +76,11 @@
}; };
name = getPluginName plugin.plugin; name = getPluginName plugin.plugin;
in in
assert assertMsg (with plugin; hasSetup -> hasLuaRequire) (concatStringsSep " " [
''Solth is unable to find the lib to require for plugin "${name}".''
''Either specify it with `luaRequire = "plugin-name";` or call setup''
''in a config option: `config = "require('plugin-name').setup { ... }";`.''
]);
mergeAttrsList ([ mergeAttrsList ([
{ {
inherit name; inherit name;
@ -55,6 +95,8 @@
(optionalAttrs hasFileTypes {inherit ft;}) (optionalAttrs hasFileTypes {inherit ft;})
(optionalAttrs hasEvents {inherit events;}) (optionalAttrs hasEvents {inherit events;})
(optionalAttrs hasKeymaps {inherit keymaps;}) (optionalAttrs hasKeymaps {inherit keymaps;})
(optionalAttrs hasLuaRequire {inherit luaRequire;})
(optionalAttrs hasSetup {inherit setup;})
])); ]));
in in
fix (self: { fix (self: {
@ -134,6 +176,8 @@ in
hasFileTypes = mkROBoolOption (config.ft != []) "file types"; hasFileTypes = mkROBoolOption (config.ft != []) "file types";
hasKeymaps = mkROBoolOption (config.keymaps != []) "keymaps"; hasKeymaps = mkROBoolOption (config.keymaps != []) "keymaps";
hasEvents = mkROBoolOption (config.events != []) "events"; hasEvents = mkROBoolOption (config.events != []) "events";
hasSetup = mkROBoolOption (config.setup != null) "setup";
hasLuaRequire = mkROBoolOption (config.luaRequire != null) "luaRequire";
lazy = mkOption { lazy = mkOption {
type = types.bool; type = types.bool;
@ -164,6 +208,26 @@ in
''; '';
}; };
luaRequire = mkOption {
type = with types; nullOr str;
default = findLuaRequire config;
description = ''
Name of the lua module to require.
An heuristic should find it, but may fail sometimes.
'';
};
setup = mkOption {
type = with types; nullOr (attrsOf anything);
default = null;
description = ''
Option to pass the the setup function.
Will call `require("''${config.luaRequire}").setup(''${toLua config.setup})`.
'';
};
# priority = mkOption { # priority = mkOption {
# type = types.int; # type = types.int;
# default = []; # default = [];

View File

@ -18,6 +18,7 @@ local function describe(dep)
utils.info('Is lazy: %s', yesno(dep.is_lazy)) utils.info('Is lazy: %s', yesno(dep.is_lazy))
utils.info('Has init: %s', yesno(dep.has_init)) utils.info('Has init: %s', yesno(dep.has_init))
utils.info('Has config: %s', yesno(dep.has_config)) utils.info('Has config: %s', yesno(dep.has_config))
utils.info('Has setup: %s', yesno(dep.has_setup))
utils.info('Dependencies: %s', list(dep.dependency_names)) utils.info('Dependencies: %s', list(dep.dependency_names))
utils.info('Filetypes: %s', list(dep.ft)) utils.info('Filetypes: %s', list(dep.ft))
utils.info('Commands: %s', list(dep.cmd)) utils.info('Commands: %s', list(dep.cmd))

View File

@ -42,8 +42,16 @@ function M.new(values)
dep:config() dep:config()
end end
local setup = self.values.setup
local config = self.values.config or function() end local config = self.values.config or function() end
config() config()
if setup then
local lib = require(self.values.luaRequire)
if type(setup) == 'function' then
setup = setup(lib)
end
lib.setup(setup)
end
end end
}, },
exit = { exit = {
@ -139,6 +147,10 @@ function M:get_has_config()
return not not self.values.config return not not self.values.config
end end
function M:get_has_setup()
return not not self.values.setup
end
function M:get_has_events() function M:get_has_events()
return self.ft or self.events return self.ft or self.events
end end