From f3926f63655a87d360eac52d7df8b9ef2d6faa73 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Fri, 10 May 2024 01:31:47 +0300 Subject: [PATCH] modules/plugin: remove isDependency do not import to local variable by default. Instead you should set varName option. --- modules/build/neovim.nix | 4 +-- modules/plugin.nix | 37 +++++++++++++++-------- modules/plugins/interface/lualine.nix | 4 +-- modules/plugins/navigation/hop-nvim.nix | 1 + modules/plugins/navigation/nvim-tree.nix | 2 +- modules/plugins/navigation/telescope.nix | 12 +++----- modules/plugins/nvim-cmp.nix | 18 +++++------ modules/plugins/snippet/snippy.nix | 2 +- modules/plugins/snippet/ultisnips.nix | 1 - modules/plugins/snippet/vsnip.nix | 1 - modules/plugins/style/neoformat.nix | 7 ++--- modules/plugins/style/nvim-treesitter.nix | 17 ++++++----- modules/profiles/recommended-plugins.nix | 2 +- 13 files changed, 55 insertions(+), 53 deletions(-) diff --git a/modules/build/neovim.nix b/modules/build/neovim.nix index aa1ab49..92bb6f8 100644 --- a/modules/build/neovim.nix +++ b/modules/build/neovim.nix @@ -40,7 +40,7 @@ in # Opts (flip mapAttrsToList config.vim.opt (k: set "vim.opt.${k}")) # Plugins - (map (v: v.genConfig) (filter (v: !v.isDependency) (attrValues config.plugin))) + (map (v: v.genConfig) (filter (v: v.enable) (attrValues config.plugin))) # Cmd (optional (config.vim.cmd != "") (call "vim.cmd" config.vim.cmd)) # Autocommands @@ -53,7 +53,7 @@ in )) ])); - plugins = lib.mapAttrsToList (k: v: v.package) config.plugin; + plugins = map (v: v.package) (filter (v: v.enable) (attrValues config.plugin)); toplevel = pkgs.wrapNeovim pkgs.neovim-unwrapped { viAlias = false; diff --git a/modules/plugin.nix b/modules/plugin.nix index 459f7d1..f5809d1 100644 --- a/modules/plugin.nix +++ b/modules/plugin.nix @@ -2,27 +2,29 @@ let + inherit (lib.nix2lua) require var local set pipe1 call; + pluginOpts = ({ name, config, ... }: { options = with lib; with types; { + enable = mkEnableOption "Enable plugin " // { default = true; }; name = mkOption { type = str; }; extraImports = mkOption { - type = attrsOf str; + type = attrsOf (either str attrs); default = { }; example = { hint = "hop.hint"; }; }; varName = mkOption { - type = str; - # TODO: add validation + type = nullOr str; + default = null; + }; + var = mkOption { + type = attrs; + internal = true; }; package = mkPackageOption pkgs.vimPlugins name { }; - isDependency = mkOption { - type = bool; - default = false; - }; - beforeSetup = mkOption { type = listOf attrs; default = [ ]; @@ -59,16 +61,25 @@ let config = { name = lib.mkDefault name; - varName = lib.mkDefault (builtins.replaceStrings [ "-" "/" "." ] [ "_" "_" "_" ] config.name); - genConfig = with lib; with nix2lua; mkIf (!config.isDependency) (flatten [ - (local (set config.varName (require config.name))) - (mapAttrsToList (k: v: local (set k (require v))) config.extraImports) + var = if config.varName != null then var config.varName else require config.name; + + genConfig = with lib; mkIf config.enable (flatten [ + (lib.optional (config.varName != null) (local (set config.varName (require config.name)))) + (mapAttrsToList + (k: v: local (set k (if builtins.isString v then require v else v))) + config.extraImports + ) + config.beforeSetup (optional (config.setupSettings != null) - (pipe1 (var config.varName) (call config.setupFnName config.setupSettings)) + (pipe1 + config.var + (call config.setupFnName config.setupSettings) + ) ) config.afterSetup + config.extra ]); diff --git a/modules/plugins/interface/lualine.nix b/modules/plugins/interface/lualine.nix index 0426990..a2e629a 100644 --- a/modules/plugins/interface/lualine.nix +++ b/modules/plugins/interface/lualine.nix @@ -31,9 +31,7 @@ in } (lib.mkIf config.plugins.language-server.lspconfig.enable { - plugin.lualine-lsp-progress = lib.mkDefault { - isDependency = true; - }; + plugin.lualine-lsp-progress = { }; }) ]); } diff --git a/modules/plugins/navigation/hop-nvim.nix b/modules/plugins/navigation/hop-nvim.nix index 6d04b8f..1e0bdf5 100644 --- a/modules/plugins/navigation/hop-nvim.nix +++ b/modules/plugins/navigation/hop-nvim.nix @@ -44,6 +44,7 @@ in config = lib.mkIf cfg.enable { plugin.hop-nvim = { name = "hop"; + varName = "hop"; extraImports = { hop_hint = "hop.hint"; }; package = cfg.package; setupSettings = cfg.settings; diff --git a/modules/plugins/navigation/nvim-tree.nix b/modules/plugins/navigation/nvim-tree.nix index 2e635c3..40b7181 100644 --- a/modules/plugins/navigation/nvim-tree.nix +++ b/modules/plugins/navigation/nvim-tree.nix @@ -64,7 +64,7 @@ in ]; }; - plugin.nvim-web-devicons = lib.mkDefault { isDependency = true; }; + plugin.nvim-web-devicons = { }; vim.g = lib.mkIf cfg.disableNetrw { loaded_netrw = 1; diff --git a/modules/plugins/navigation/telescope.nix b/modules/plugins/navigation/telescope.nix index 4e177da..de80f18 100644 --- a/modules/plugins/navigation/telescope.nix +++ b/modules/plugins/navigation/telescope.nix @@ -40,7 +40,7 @@ let config = { name = lib.mkDefault name; loadExtension = lib.mkDefault (pipe1 - (var config.plugin.telescope-nvim.varName) + config.plugin.telescope-nvim.var (call "load_extension" sub.config.name) ); }; @@ -84,9 +84,10 @@ in config = lib.mkIf cfg.enable { plugin = lib.mkMerge (lib.flatten [ { - plenary-nvim = lib.mkDefault { isDependency = true; }; - telescope-nvim = { + plenary-nvim = { }; + telescope-nvim = rec { name = "telescope"; + varName = name; package = cfg.package; setupSettings = lib.mkMerge (lib.flatten [ cfg.settings @@ -102,10 +103,7 @@ in # extensions (lib.flip lib.mapAttrsToList extensions (packageName: ext: { - "${packageName}" = { - inherit (ext) package; - isDependency = lib.mkDefault true; - }; + "${packageName}" = { inherit (ext) package; }; })) ]); }; diff --git a/modules/plugins/nvim-cmp.nix b/modules/plugins/nvim-cmp.nix index 7f7158d..602749e 100644 --- a/modules/plugins/nvim-cmp.nix +++ b/modules/plugins/nvim-cmp.nix @@ -32,18 +32,16 @@ in # Backend dependencies (lib.mkIf snippet.luasnip.enable { - plugin.cmp_luasnip = { - isDependency = true; - }; + plugin.cmp_luasnip = { }; }) (lib.mkIf snippet.snippy.enable { - plugin.cmp-snippy = { isDependency = true; }; + plugin.cmp-snippy = { }; }) (lib.mkIf snippet.vsnip.enable { - plugin.cmp-vsnip = { isDependency = true; }; + plugin.cmp-vsnip = { }; }) (lib.mkIf snippet.ultisnips.enable { - plugin.cmp-nvim-ultisnips = { isDependency = true; }; + plugin.cmp-nvim-ultisnips = { }; }) # Lsp configuration @@ -54,9 +52,7 @@ in }; plugins.language-server.lspconfig.defaultServerSettings.capabilities = - pipe1 - (var config.plugin.cmp-nvim-lsp.varName) - (call0 "default_capabilities"); + pipe1 config.plugin.cmp-nvim-lsp.var (call0 "default_capabilities"); }) # Base configuration @@ -70,11 +66,11 @@ in let body = pipe1 args (var "body"); in { extra = if snippet.luasnip.enable then - pipe1 (var config.plugin.luasnip.varName) (call "lsp_expand" body) + pipe1 config.plugin.luasnip.var (call "lsp_expand" body) else if snippet.vsnip.enable then call "vim.fn[\"${config.plugin.vim-vsnip.name}#anonymous\"]" body else if snippet.snippy.enable then - pipe1 (var config.plugin.nvim-snippy.varName) (call "expand_snipped" body) + pipe1 config.plugin.nvim-snippy.var (call "expand_snipped" body) else if snippet.ultisnips.enable then call "vim.fn[\"${config.plugin.ultisnips.name}#Anon\"]" body else diff --git a/modules/plugins/snippet/snippy.nix b/modules/plugins/snippet/snippy.nix index 995a371..e7dc98c 100644 --- a/modules/plugins/snippet/snippy.nix +++ b/modules/plugins/snippet/snippy.nix @@ -12,8 +12,8 @@ in config = lib.mkIf cfg.enable { plugin.nvim-snippy = { - name = "snippy"; inherit (cfg) package; + name = "snippy"; }; }; } diff --git a/modules/plugins/snippet/ultisnips.nix b/modules/plugins/snippet/ultisnips.nix index 6529a03..3112d34 100644 --- a/modules/plugins/snippet/ultisnips.nix +++ b/modules/plugins/snippet/ultisnips.nix @@ -14,7 +14,6 @@ in plugin.ultisnips = { inherit (cfg) package; name = "UltiSnips"; - isDependency = true; }; }; } diff --git a/modules/plugins/snippet/vsnip.nix b/modules/plugins/snippet/vsnip.nix index 334871e..b52aab3 100644 --- a/modules/plugins/snippet/vsnip.nix +++ b/modules/plugins/snippet/vsnip.nix @@ -14,7 +14,6 @@ in plugin.vim-vsnip = { inherit (cfg) package; name = "vsnip"; - isDependency = true; }; }; } diff --git a/modules/plugins/style/neoformat.nix b/modules/plugins/style/neoformat.nix index 84d6c4d..f601d65 100644 --- a/modules/plugins/style/neoformat.nix +++ b/modules/plugins/style/neoformat.nix @@ -27,11 +27,8 @@ let cfg = config.plugins.style.neoformat; in }; config = lib.mkIf cfg.enable { - plugin.neoformat = { - # we shouldn't import this plugin - isDependency = true; - package = cfg.package; - }; + # we shouldn't import this plugin + plugin.neoformat.package = cfg.package; vim.g = { neoformat_try_node_exe = 1; diff --git a/modules/plugins/style/nvim-treesitter.nix b/modules/plugins/style/nvim-treesitter.nix index 525114b..8a8c82c 100644 --- a/modules/plugins/style/nvim-treesitter.nix +++ b/modules/plugins/style/nvim-treesitter.nix @@ -2,10 +2,13 @@ let inherit (builtins) elem attrNames; + inherit (lib.nix2lua) pipe1 require call0 set; cfg = config.plugins.style.nvim-treesitter; - treesitterWithGrammars = (cfg.treesitter.package.override { inherit (cfg) extraGrammars; }).withPlugins (lib.filterAtttrs (k: elem k (attrNames cfg.extraGrammars))); + treesitterWithGrammars = (cfg.treesitter.package.override { + inherit (cfg) extraGrammars; + }).withPlugins (lib.filterAtttrs (k: elem k (attrNames cfg.extraGrammars))); nvimTreeSitterWithBuiltinGrammars = if cfg.grammars == null then cfg.package.withAllGrammars @@ -83,13 +86,13 @@ in name = "nvim-treesitter.configs"; package = finalNvimTreeSitter; - beforeSetup = with lib; with nix2lua; optionals (cfg.extraGrammars != { }) (flatten [ - (local (set "parser_config") - (pipe1 (require "nvim-treesitter.parsers") (call0 "get_parser_configs")) - ) + extraImports = { + parser_config = pipe1 (require "nvim-treesitter.parsers") (call0 "get_parser_configs"); + }; - (mapAttrsToList (k: v: set "parser_config.${v.language}" { }) cfg.extraGrammars) - ]); + beforeSetup = with lib; optionals (cfg.extraGrammars != { }) ( + mapAttrsToList (k: v: set "parser_config.${v.language}" { }) cfg.extraGrammars + ); setupSettings = lib.mkMerge [ # enable hihlight and indent by default diff --git a/modules/profiles/recommended-plugins.nix b/modules/profiles/recommended-plugins.nix index 3bf6776..6494644 100644 --- a/modules/profiles/recommended-plugins.nix +++ b/modules/profiles/recommended-plugins.nix @@ -2,7 +2,7 @@ let inherit (lib.nix2lua) nf; in { - plugin.editorconfig-nvim.isDependency = lib.mkDefault true; + plugin.editorconfig-nvim = { }; plugins = { interface = {