From 3a36ca876abeb97679e9345ff29d4f2b7a5f48fa Mon Sep 17 00:00:00 2001 From: Ching Pei Yang <59727193+horriblename@users.noreply.github.com> Date: Mon, 30 Sep 2024 21:56:31 +0200 Subject: [PATCH] lazy: init module --- modules/modules.nix | 1 + modules/wrapper/lazy/config.nix | 85 +++++++++++++ modules/wrapper/lazy/default.nix | 6 + modules/wrapper/lazy/lazy.nix | 205 +++++++++++++++++++++++++++++++ 4 files changed, 297 insertions(+) create mode 100644 modules/wrapper/lazy/config.nix create mode 100644 modules/wrapper/lazy/default.nix create mode 100644 modules/wrapper/lazy/lazy.nix diff --git a/modules/modules.nix b/modules/modules.nix index 1204e43f..784c413e 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 00000000..5db378bc --- /dev/null +++ b/modules/wrapper/lazy/config.nix @@ -0,0 +1,85 @@ +{ + 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 = { + desc, + noremap, + expr, + nowait, + ft, + key, + action, + lua, + mode, + }: { + "@1" = key; + "@2" = + if lua + then mkLuaInline action + else action; + inherit desc noremap expr nowait ft mode; + }; + + 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 00000000..fa401272 --- /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 00000000..c00b1fe4 --- /dev/null +++ b/modules/wrapper/lazy/lazy.nix @@ -0,0 +1,205 @@ +{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"]; + }; + + noremap = mkBool true "Whether to use the 'noremap' variant of the command, ignoring any custom mappings on the defined action. It is highly advised to keep this on, which is the default."; + expr = mkBool false "Means that the action is actually an expression. Equivalent to adding to a map."; + nowait = mkBool false "Whether to wait for extra input on ambiguous mappings. Equivalent to adding to a map."; + }; + }; + + lznPluginType = submodule { + options = { + ## Should probably infer from the actual plugin somehow + ## In general this is the name passed to packadd, so the dir name of the plugin + # name = mkOption { + # type= str; + # } + + # Non-lz.n options + + package = mkOption { + type = pluginType; + description = "Plugin package"; + }; + + setupModule = mkOption { + type = nullOr str; + description = "Lua module to run setup function on."; + default = null; + }; + + setupOpts = mkOption { + type = submodule {freeformType = attrsOf anything;}; + description = "Options to pass to the setup function"; + default = {}; + }; + + # lz.n options + + enabled = mkOption { + type = nullOr (either bool str); + description = "When false, or if the lua function returns false, this plugin will not be included in the spec"; + default = null; + }; + + beforeAll = mkOption { + type = nullOr str; + description = "Lua code to run before any plugins are loaded. This will be wrapped in a function."; + default = null; + }; + + before = mkOption { + type = nullOr str; + description = "Lua code to run before plugin is loaded. This will be wrapped in a function."; + default = null; + }; + + after = mkOption { + type = nullOr str; + description = "Lua code to run after plugin is loaded. This will be wrapped in a function."; + default = null; + }; + + event = mkOption { + description = "Lazy-load on event"; + default = null; + type = let + event = submodule { + options = { + event = mkOption { + type = nullOr (either str (listOf str)); + description = "Exact event name"; + example = "BufEnter"; + }; + pattern = mkOption { + type = nullOr (either str (listOf str)); + description = "Event pattern"; + example = "BufEnter *.lua"; + }; + }; + }; + in + nullOr (oneOf [str (listOf str) event]); + }; + + cmd = mkOption { + description = "Lazy-load on command"; + default = null; + type = nullOr (either str (listOf str)); + }; + + ft = mkOption { + description = "Lazy-load on filetype"; + default = null; + type = nullOr (either str (listOf str)); + }; + + keys = mkOption { + description = "Lazy-load on key mapping"; + default = null; + type = nullOr (oneOf [str (listOf lznKeysSpec) (listOf str)]); + example = '' + keys = [ + { + mode = "n"; + key = "s"; + action = ":DapStepOver"; + desc = "DAP Step Over"; + } + { + mode = ["n", "x"]; + key = "dc"; + action = "function() require('dap').continue() end"; + lua = true; + desc = "DAP Continue"; + } + ] + ''; + }; + + colorscheme = mkOption { + description = "Lazy-load on colorscheme."; + type = nullOr (either str (listOf str)); + default = null; + }; + + priority = mkOption { + type = nullOr int; + description = "Only useful for stat plugins (not lazy-loaded) to force loading certain plugins first."; + default = null; + }; + + load = mkOption { + type = nullOr str; + default = null; + description = '' + Lua code to override the `vim.g.lz_n.load()` function for a single plugin. + + This will be wrapped in a function + ''; + }; + }; + }; +in { + options.vim.lazy = { + enable = mkEnableOption "plugin lazy-loading" // {default = true;}; + loader = mkOption { + description = "Lazy loader to use"; + type = enum ["lz.n"]; + default = "lz.n"; + }; + + plugins = mkOption { + default = []; + type = listOf lznPluginType; + description = "list of plugins to lazy load"; + example = '' + [ + { + package = "toggleterm-nvim"; + after = lib.generators.mkLuaInline "function() require('toggleterm').setup{} end"; + cmd = ["ToggleTerm"]; + } + ] + ''; + }; + }; +}