diff --git a/modules/modules.nix b/modules/modules.nix index 1204e43..784c413 100644 --- a/modules/modules.nix +++ b/modules/modules.nix @@ -50,6 +50,7 @@ "build" "rc" "warnings" + "lazy" ]; # Extra modules, such as deprecation warnings diff --git a/modules/wrapper/lazy/config.nix b/modules/wrapper/lazy/config.nix new file mode 100644 index 0000000..9ee15e7 --- /dev/null +++ b/modules/wrapper/lazy/config.nix @@ -0,0 +1,76 @@ +{ + lib, + config, + ... +}: let + inherit (builtins) toJSON typeOf head length tryEval filter; + inherit (lib.modules) mkIf; + inherit (lib.generators) mkLuaInline; + inherit (lib.strings) optionalString; + inherit (lib.nvim.lua) toLuaObject; + inherit (lib.nvim.dag) entryBefore; + cfg = config.vim.lazy; + + toLuaLznKeySpec = keySpec: + (removeAttrs keySpec ["key" "lua" "action"]) + // { + "@1" = keySpec.key; + "@2" = + if keySpec.lua + then mkLuaInline keySpec.action + else keySpec.action; + }; + + toLuaLznSpec = spec: let + name = + if typeOf spec.package == "string" + then spec.package + else if (spec.package ? pname && (tryEval spec.package.pname).success) + then spec.package.pname + else spec.package.name; + in + (removeAttrs spec ["package" "setupModule" "setupOpts" "keys"]) + // { + "@1" = name; + before = + if spec.before != null + then + mkLuaInline '' + function() + ${spec.before} + end + '' + else null; + + after = + if spec.setupModule == null && spec.after == null + then null + else + mkLuaInline '' + function() + ${ + optionalString (spec.setupModule != null) + "require(${toJSON spec.setupModule}).setup(${toLuaObject spec.setupOpts})" + } + ${optionalString (spec.after != null) spec.after} + end + ''; + + keys = + if typeOf spec.keys == "list" && length spec.keys > 0 && typeOf (head spec.keys) == "set" + then map toLuaLznKeySpec (filter (keySpec: keySpec.key != null) spec.keys) + # empty list or str or (listOf str) + else spec.keys; + }; + lznSpecs = map toLuaLznSpec cfg.plugins; +in { + config.vim = mkIf cfg.enable { + startPlugins = ["lz-n" "lzn-auto-require"]; + + optPlugins = map (plugin: plugin.package) cfg.plugins; + + luaConfigRC.lzn-load = entryBefore ["pluginConfigs"] '' + require('lz.n').load(${toLuaObject lznSpecs}) + ''; + }; +} diff --git a/modules/wrapper/lazy/default.nix b/modules/wrapper/lazy/default.nix new file mode 100644 index 0000000..fa40127 --- /dev/null +++ b/modules/wrapper/lazy/default.nix @@ -0,0 +1,6 @@ +_: { + imports = [ + ./lazy.nix + ./config.nix + ]; +} diff --git a/modules/wrapper/lazy/lazy.nix b/modules/wrapper/lazy/lazy.nix new file mode 100644 index 0000000..5f20c12 --- /dev/null +++ b/modules/wrapper/lazy/lazy.nix @@ -0,0 +1,207 @@ +{lib, ...}: let + inherit (lib.options) mkOption mkEnableOption; + inherit (lib.types) enum listOf submodule nullOr str bool int attrsOf anything either oneOf; + inherit (lib.nvim.types) pluginType; + inherit (lib.nvim.config) mkBool; + + lznKeysSpec = submodule { + options = { + key = mkOption { + type = nullOr str; + description = "Key to bind to. If key is null this entry is ignored."; + }; + + action = mkOption { + type = nullOr str; + default = null; + description = "Action to trigger."; + }; + lua = mkBool false '' + If true, `action` is considered to be lua code. + Thus, it will not be wrapped in `""`. + ''; + + desc = mkOption { + description = "Description of the key map"; + type = nullOr str; + default = null; + }; + + ft = mkOption { + description = "TBD"; + type = nullOr (listOf str); + default = null; + }; + + mode = mkOption { + description = "Modes to bind in"; + type = listOf str; + default = ["n" "x" "s" "o"]; + }; + + silent = mkBool true "Whether this mapping should be silent. Equivalent to adding to a map."; + nowait = mkBool false "Whether to wait for extra input on ambiguous mappings. Equivalent to adding to a map."; + script = mkBool false "Equivalent to adding