diff --git a/lib/helpers.nix b/lib/helpers.nix index e03e11d712..204f4348e9 100644 --- a/lib/helpers.nix +++ b/lib/helpers.nix @@ -10,13 +10,9 @@ let nixvimUtils = import ./utils.nix { inherit lib nixvimTypes _nixvimTests; }; nixvimOptions = import ./options.nix { inherit lib nixvimTypes nixvimUtils; }; nixvimDeprecation = import ./deprecation.nix { inherit lib; }; -in -rec { - maintainers = import ./maintainers.nix; lua = import ./to-lua.nix { inherit lib; }; - keymaps = import ./keymap-helpers.nix { inherit lib nixvimOptions nixvimTypes; }; - autocmd = import ./autocmd-helpers.nix { inherit lib nixvimOptions nixvimTypes; }; - neovim-plugin = import ./neovim-plugin.nix { + toLuaObject = lua.toLua; + nixvimPlugins = import ./plugins { inherit lib nixvimOptions @@ -24,11 +20,16 @@ rec { toLuaObject ; }; - vim-plugin = import ./vim-plugin.nix { inherit lib nixvimOptions nixvimUtils; }; - inherit nixvimTypes; - toLuaObject = lua.toLua; +in +rec { + maintainers = import ./maintainers.nix; + keymaps = import ./keymap-helpers.nix { inherit lib nixvimOptions nixvimTypes; }; + autocmd = import ./autocmd-helpers.nix { inherit lib nixvimOptions nixvimTypes; }; + plugins = import ./plugin { }; + inherit nixvimTypes lua toLuaObject; } // nixvimUtils // nixvimOptions // nixvimBuilders // nixvimDeprecation +// nixvimPlugins diff --git a/lib/neovim-plugin.nix b/lib/neovim-plugin.nix deleted file mode 100644 index 89a6b550f9..0000000000 --- a/lib/neovim-plugin.nix +++ /dev/null @@ -1,121 +0,0 @@ -{ - lib, - nixvimOptions, - toLuaObject, - nixvimUtils, -}: -with lib; -{ - # TODO: DEPRECATED: use the `settings` option instead - extraOptionsOptions = { - extraOptions = mkOption { - default = { }; - type = with types; attrsOf anything; - description = '' - These attributes will be added to the table parameter for the setup function. - Typically, it can override NixVim's default settings. - ''; - }; - }; - - mkNeovimPlugin = - config: - { - name, - maintainers, - url ? defaultPackage.meta.homepage, - imports ? [ ], - description ? null, - # deprecations - deprecateExtraOptions ? false, - optionsRenamedToSettings ? [ ], - # colorscheme - isColorscheme ? false, - colorscheme ? name, - # options - originalName ? name, - defaultPackage, - settingsOptions ? { }, - settingsExample ? null, - settingsDescription ? "Options provided to the `require('${luaName}')${setup}` function.", - hasSettings ? true, - extraOptions ? { }, - # config - luaName ? name, - setup ? ".setup", - extraConfig ? cfg: { }, - extraPlugins ? [ ], - extraPackages ? [ ], - callSetup ? true, - }: - let - namespace = if isColorscheme then "colorschemes" else "plugins"; - in - { - meta = { - inherit maintainers; - nixvimInfo = { - inherit description url; - path = [ - namespace - name - ]; - }; - }; - - imports = - let - basePluginPath = [ - namespace - name - ]; - settingsPath = basePluginPath ++ [ "settings" ]; - in - imports - ++ (optional deprecateExtraOptions ( - mkRenamedOptionModule (basePluginPath ++ [ "extraOptions" ]) settingsPath - )) - ++ (map ( - option: - let - optionPath = if isString option then [ option ] else option; # option is already a path (i.e. a list) - - optionPathSnakeCase = map nixvimUtils.toSnakeCase optionPath; - in - mkRenamedOptionModule (basePluginPath ++ optionPath) (settingsPath ++ optionPathSnakeCase) - ) optionsRenamedToSettings); - - options.${namespace}.${name} = - { - enable = mkEnableOption originalName; - - package = nixvimOptions.mkPluginPackageOption originalName defaultPackage; - } - // optionalAttrs hasSettings { - settings = nixvimOptions.mkSettingsOption { - description = settingsDescription; - options = settingsOptions; - example = settingsExample; - }; - } - // extraOptions; - - config = - let - cfg = config.${namespace}.${name}; - extraConfigNamespace = if isColorscheme then "extraConfigLuaPre" else "extraConfigLua"; - in - mkIf cfg.enable (mkMerge [ - { - extraPlugins = [ cfg.package ] ++ extraPlugins; - inherit extraPackages; - - ${extraConfigNamespace} = optionalString callSetup '' - require('${luaName}')${setup}(${optionalString (cfg ? settings) (toLuaObject cfg.settings)}) - ''; - } - (optionalAttrs (isColorscheme && (colorscheme != null)) { colorscheme = mkDefault colorscheme; }) - (extraConfig cfg) - ]); - }; -} diff --git a/lib/plugins/default.nix b/lib/plugins/default.nix new file mode 100644 index 0000000000..fbb732b3d2 --- /dev/null +++ b/lib/plugins/default.nix @@ -0,0 +1,36 @@ +{ + lib, + nixvimOptions, + nixvimUtils, + toLuaObject, + ... +}: +let + mkPlugin = import ./mk-plugin.nix { inherit lib nixvimOptions nixvimUtils; }; +in +rec { + plugins = { + inherit mkPlugin; + inherit (neovim-plugin) mkNeovimPlugin; + inherit (vim-plugin) mkVimPlugin; + }; + + neovim-plugin = import ./neovim-plugin.nix { + inherit + lib + nixvimOptions + nixvimUtils + toLuaObject + mkPlugin + ; + }; + + vim-plugin = import ./vim-plugin.nix { + inherit + lib + nixvimOptions + nixvimUtils + mkPlugin + ; + }; +} diff --git a/lib/plugins/mk-plugin.nix b/lib/plugins/mk-plugin.nix new file mode 100644 index 0000000000..53bdc36c19 --- /dev/null +++ b/lib/plugins/mk-plugin.nix @@ -0,0 +1,90 @@ +{ + lib, + nixvimOptions, + nixvimUtils, +}: +with lib; +config: +{ + name, + maintainers, + url ? defaultPackage.meta.homepage, + imports ? [ ], + description ? null, + # deprecations + optionsRenamedToSettings ? [ ], + # colorscheme + isColorscheme ? false, + colorscheme ? name, + # options + originalName ? name, + defaultPackage ? null, + extraOptions ? { }, + # config + extraConfig ? cfg: { }, + extraPlugins ? [ ], + extraPackages ? [ ], +}: +let + namespace = if isColorscheme then "colorschemes" else "plugins"; + + optionsRenamedToSettingsWarnings = + let + basePluginPath = [ + namespace + name + ]; + settingsPath = basePluginPath ++ [ "settings" ]; + in + map ( + option: + let + optionPath = if isString option then [ option ] else option; # option is already a path (i.e. a list) + + optionPathSnakeCase = map nixvimUtils.toSnakeCase optionPath; + in + mkRenamedOptionModule ( + [ + namespace + name + ] + ++ optionPath + ) (settingsPath ++ optionPathSnakeCase) + ) optionsRenamedToSettings; +in +{ + meta = { + inherit maintainers; + nixvimInfo = { + inherit description url; + path = [ + namespace + name + ]; + }; + }; + + imports = optionsRenamedToSettingsWarnings ++ imports; + + options.${namespace}.${name} = + { + enable = mkEnableOption originalName; + } + // (optionalAttrs (defaultPackage != null) { + package = nixvimOptions.mkPluginPackageOption originalName defaultPackage; + }) + // extraOptions; + + config = + let + cfg = config.${namespace}.${name}; + in + mkIf cfg.enable (mkMerge [ + { + extraPlugins = extraPlugins ++ optional (defaultPackage != null) cfg.package; + inherit extraPackages; + } + (extraConfig cfg) + (optionalAttrs (isColorscheme && (colorscheme != null)) { colorscheme = mkDefault colorscheme; }) + ]); +} diff --git a/lib/plugins/neovim-plugin.nix b/lib/plugins/neovim-plugin.nix new file mode 100644 index 0000000000..ca5e1437a3 --- /dev/null +++ b/lib/plugins/neovim-plugin.nix @@ -0,0 +1,96 @@ +{ + lib, + nixvimOptions, + toLuaObject, + nixvimUtils, + mkPlugin, +}: +with lib; +{ + # TODO: DEPRECATED: use the `settings` option instead + extraOptionsOptions = { + extraOptions = mkOption { + default = { }; + type = with types; attrsOf anything; + description = '' + These attributes will be added to the table parameter for the setup function. + Typically, it can override NixVim's default settings. + ''; + }; + }; + + mkNeovimPlugin = + config: + { + name, + defaultPackage, # make this parameter mandatory for neovim plugins + # deprecations + deprecateExtraOptions ? false, + # colorscheme + isColorscheme ? false, + # options + settingsOptions ? { }, + settingsExample ? null, + settingsDescription ? "Options provided to the `require('${luaName}')${setup}` function.", + hasSettings ? true, + extraOptions ? { }, + # config + setup ? ".setup", + luaName ? name, + callSetup ? true, + ... + }@args: + mkPlugin config ( + (removeAttrs args [ + "callSetup" + "deprecateExtraOptions" + "extraConfig" + "extraOptions" + "hasSettings" + "imports" + "luaName" + "settingsExample" + "settingsOptions" + ]) + // { + imports = + let + basePluginPath = [ + (if isColorscheme then "colorschemes" else "plugins") + name + ]; + settingsPath = basePluginPath ++ [ "settings" ]; + in + (args.imports or [ ]) + ++ (optional deprecateExtraOptions ( + mkRenamedOptionModule (basePluginPath ++ [ "extraOptions" ]) settingsPath + )); + + extraOptions = + (optionalAttrs hasSettings { + settings = nixvimOptions.mkSettingsOption { + description = settingsDescription; + options = settingsOptions; + example = settingsExample; + }; + }) + // extraOptions; + + extraConfig = + let + extraConfigNamespace = if isColorscheme then "extraConfigLuaPre" else "extraConfigLua"; + in + cfg: + mkMerge ( + [ + { + ${extraConfigNamespace} = optionalString callSetup '' + require('${luaName}')${setup}(${optionalString (cfg ? settings) (toLuaObject cfg.settings)}) + ''; + } + ] + ++ (optional (args ? extraConfig) (args.extraConfig cfg)) + ); + } + ); +} diff --git a/lib/plugins/vim-plugin.nix b/lib/plugins/vim-plugin.nix new file mode 100644 index 0000000000..9e9629877f --- /dev/null +++ b/lib/plugins/vim-plugin.nix @@ -0,0 +1,86 @@ +{ + lib, + nixvimOptions, + nixvimUtils, + mkPlugin, +}: +with lib; +{ + mkVimPlugin = + config: + { + name, + # deprecations + deprecateExtraConfig ? false, + # colorscheme + isColorscheme ? false, + colorscheme ? name, + # options + settingsOptions ? { }, + settingsExample ? null, + globalPrefix ? "", + extraOptions ? { }, + # config + extraConfig ? cfg: { }, + ... + }@args: + mkPlugin config ( + let + cfg = config.${namespace}.${name}; + + globals = cfg.settings or { }; + + createSettingsOption = (isString globalPrefix) && (globalPrefix != ""); + + namespace = if isColorscheme then "colorschemes" else "plugins"; + + settingsOption = optionalAttrs createSettingsOption { + settings = nixvimOptions.mkSettingsOption { + options = settingsOptions; + example = settingsExample; + description = '' + The configuration options for **${name}** without the `${globalPrefix}` prefix. + + For example, the following settings are equivialent to these `:setglobal` commands: + - `foo_bar = 1` -> `:setglobal ${globalPrefix}foo_bar=1` + - `hello = "world"` -> `:setglobal ${globalPrefix}hello="world"` + - `some_toggle = true` -> `:setglobal ${globalPrefix}some_toggle` + - `other_toggle = false` -> `:setglobal no${globalPrefix}other_toggle` + ''; + }; + }; + in + (removeAttrs args [ + "deprecateExtraConfig" + "extraConfig" + "extraOptions" + "globalPrefix" + "imports" + "settingsExample" + "settingsOptions" + ]) + // { + imports = + let + basePluginPath = [ + namespace + name + ]; + settingsPath = basePluginPath ++ [ "settings" ]; + in + (args.imports or [ ]) + ++ (optional (deprecateExtraConfig && createSettingsOption) ( + mkRenamedOptionModule (basePluginPath ++ [ "extraConfig" ]) settingsPath + )); + + extraOptions = settingsOption // extraOptions; + + extraConfig = + cfg: + mkMerge ( + [ { globals = mapAttrs' (n: nameValuePair (globalPrefix + n)) globals; } ] + ++ (optional (args ? extraConfig) (args.extraConfig cfg)) + ); + } + ); +} diff --git a/lib/vim-plugin.nix b/lib/vim-plugin.nix deleted file mode 100644 index 13a17c5edb..0000000000 --- a/lib/vim-plugin.nix +++ /dev/null @@ -1,114 +0,0 @@ -{ - lib, - nixvimOptions, - nixvimUtils, -}: -with lib; -{ - mkVimPlugin = - config: - { - name, - url ? if defaultPackage != null then defaultPackage.meta.homepage else null, - maintainers, - imports ? [ ], - description ? null, - # deprecations - deprecateExtraConfig ? false, - optionsRenamedToSettings ? [ ], - # colorscheme - isColorscheme ? false, - colorscheme ? name, - # options - originalName ? name, - defaultPackage ? null, - settingsOptions ? { }, - settingsExample ? null, - globalPrefix ? "", - extraOptions ? { }, - # config - extraConfig ? cfg: { }, - extraPlugins ? [ ], - extraPackages ? [ ], - }: - let - namespace = if isColorscheme then "colorschemes" else "plugins"; - - cfg = config.${namespace}.${name}; - - globals = cfg.settings or { }; - - # does this evaluate package? - packageOption = - if defaultPackage == null then - { } - else - { package = nixvimOptions.mkPluginPackageOption name defaultPackage; }; - - createSettingsOption = (isString globalPrefix) && (globalPrefix != ""); - - settingsOption = optionalAttrs createSettingsOption { - settings = nixvimOptions.mkSettingsOption { - options = settingsOptions; - example = settingsExample; - description = '' - The configuration options for **${name}** without the `${globalPrefix}` prefix. - - For example, the following settings are equivialent to these `:setglobal` commands: - - `foo_bar = 1` -> `:setglobal ${globalPrefix}foo_bar=1` - - `hello = "world"` -> `:setglobal ${globalPrefix}hello="world"` - - `some_toggle = true` -> `:setglobal ${globalPrefix}some_toggle` - - `other_toggle = false` -> `:setglobal no${globalPrefix}other_toggle` - ''; - }; - }; - in - { - meta = { - inherit maintainers; - nixvimInfo = { - inherit description url; - path = [ - namespace - name - ]; - }; - }; - options.${namespace}.${name} = { - enable = mkEnableOption originalName; - } // settingsOption // packageOption // extraOptions; - - imports = - let - basePluginPath = [ - namespace - name - ]; - settingsPath = basePluginPath ++ [ "settings" ]; - in - imports - ++ (optional (deprecateExtraConfig && createSettingsOption) ( - mkRenamedOptionModule (basePluginPath ++ [ "extraConfig" ]) settingsPath - )) - ++ (map ( - option: - let - optionPath = if isString option then [ option ] else option; # option is already a path (i.e. a list) - - optionPathSnakeCase = map nixvimUtils.toSnakeCase optionPath; - in - mkRenamedOptionModule (basePluginPath ++ optionPath) (settingsPath ++ optionPathSnakeCase) - ) optionsRenamedToSettings); - - config = mkIf cfg.enable (mkMerge [ - { - inherit extraPackages; - globals = mapAttrs' (n: nameValuePair (globalPrefix + n)) globals; - # does this evaluate package? it would not be desired to evaluate package if we use another package. - extraPlugins = extraPlugins ++ optional (defaultPackage != null) cfg.package; - } - (optionalAttrs (isColorscheme && (colorscheme != null)) { colorscheme = mkDefault colorscheme; }) - (extraConfig cfg) - ]); - }; -}