summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthieu Coudron <mcoudron@hotmail.com>2021-05-21 12:48:43 +0200
committerMatthieu Coudron <mcoudron@hotmail.com>2021-05-25 22:41:08 +0200
commit7836469dbe40783c640e2c0fafd2e55248ffea31 (patch)
tree24e3147e286403ab290c56f8b410eb0039720755
parentabb1e5cd4cc416841c8eaa1f171a3e57198d657c (diff)
downloadnixpkgs-7836469dbe40783c640e2c0fafd2e55248ffea31.tar
nixpkgs-7836469dbe40783c640e2c0fafd2e55248ffea31.tar.gz
nixpkgs-7836469dbe40783c640e2c0fafd2e55248ffea31.tar.bz2
nixpkgs-7836469dbe40783c640e2c0fafd2e55248ffea31.tar.lz
nixpkgs-7836469dbe40783c640e2c0fafd2e55248ffea31.tar.xz
nixpkgs-7836469dbe40783c640e2c0fafd2e55248ffea31.tar.zst
nixpkgs-7836469dbe40783c640e2c0fafd2e55248ffea31.zip
neovimUtils: makeNeovimConfig accepts plugins/customRc
mimics home-manager interface and makes it easier to associate configs with plugins. Added a test as well.
-rw-r--r--pkgs/applications/editors/neovim/utils.nix40
-rw-r--r--pkgs/misc/vim-plugins/vim-utils.nix13
-rw-r--r--pkgs/test/vim/default.nix35
3 files changed, 76 insertions, 12 deletions
diff --git a/pkgs/applications/editors/neovim/utils.nix b/pkgs/applications/editors/neovim/utils.nix
index 3e6e88dd228..a7d8f00c664 100644
--- a/pkgs/applications/editors/neovim/utils.nix
+++ b/pkgs/applications/editors/neovim/utils.nix
@@ -29,6 +29,11 @@ let
     , withNodeJs ? false
     , withRuby ? true
 
+    # expects a list of plugin configuration
+    # expects { plugin=far-vim; config = "let g:far#source='rg'"; optional = false; }
+    , plugins ? []
+    # forwarded to configure.customRC
+    , customRC ? ""
     # same values as in vimUtils.vimrcContent
     , configure ? { }
 
@@ -44,7 +49,33 @@ let
         '';
       };
 
-      requiredPlugins = vimUtils.requiredPlugins configure;
+      # transform all plugins into an attrset
+      pluginsNormalized = map (x: if x ? plugin then { optional = false; } // x else { plugin = x; optional = false;}) plugins;
+
+
+      configurePatched = configure // {
+        packages.nix = {
+          start = lib.filter (f: f != null)
+            (map (x: if x.optional == false then x.plugin else null)
+              pluginsNormalized);
+          opt = lib.filter (f: f != null)
+            (map (x: if x.optional == true then x.plugin else null)
+              pluginsNormalized);
+        };
+        customRC = pluginRc + customRC;
+      };
+
+      # A function to get the configuration string (if any) from an element of 'plugins'
+      pluginConfig = p:
+        if (p.config or "") != "" then ''
+          " ${p.plugin.pname or p.plugin.name} {{{
+          ${p.config}
+          " }}}
+        '' else "";
+
+      pluginRc = lib.concatMapStrings pluginConfig pluginsNormalized;
+
+      requiredPlugins = vimUtils.requiredPlugins configurePatched;
       getDeps = attrname: map (plugin: plugin.${attrname} or (_: [ ]));
 
       pluginPython3Packages = getDeps "python3Dependencies" requiredPlugins;
@@ -89,12 +120,13 @@ let
           "--suffix" "PATH" ":" binPath
         ];
 
-      manifestRc = vimUtils.vimrcContent (configure // { customRC = ""; });
-      neovimRcContent = vimUtils.vimrcContent configure;
+
+      manifestRc = vimUtils.vimrcContent (configurePatched // { customRC = ""; }) ;
+      neovimRcContent = vimUtils.vimrcContent configurePatched;
     in
     assert withPython2 -> throw "Python2 support has been removed from neovim, please remove withPython2 and extraPython2Packages.";
 
-    args // {
+    builtins.removeAttrs args ["plugins"] // {
       wrapperArgs = makeWrapperArgs;
       inherit neovimRcContent;
       inherit manifestRc;
diff --git a/pkgs/misc/vim-plugins/vim-utils.nix b/pkgs/misc/vim-plugins/vim-utils.nix
index 210e2aee5e3..52bf4341fa5 100644
--- a/pkgs/misc/vim-plugins/vim-utils.nix
+++ b/pkgs/misc/vim-plugins/vim-utils.nix
@@ -232,8 +232,7 @@ let
     let
       /* pathogen mostly can set &rtp at startup time. Its used very commonly.
       */
-      pathogenImpl = lib.optionalString (pathogen != null)
-      (let
+      pathogenImpl = let
         knownPlugins = pathogen.knownPlugins or vimPlugins;
 
         plugins = findDependenciesRecursively (map (pluginToDrv knownPlugins) pathogen.pluginNames);
@@ -248,11 +247,11 @@ let
         execute pathogen#infect('${pluginsEnv}/{}')
 
         filetype indent plugin on | syn on
-      '');
+      '';
 
       /* vim-plug is an extremely popular vim plugin manager.
       */
-      plugImpl = lib.optionalString (plug != null)
+      plugImpl =
       (''
         source ${vimPlugins.vim-plug.rtp}/plug.vim
         call plug#begin('/dev/null')
@@ -340,10 +339,12 @@ let
 
       entries = [
         beforePlugins
-        vamImpl pathogenImpl plugImpl
+        vamImpl
         (nativeImpl packages)
         customRC
-      ];
+      ]
+      ++ lib.optional (pathogen != null) pathogenImpl
+      ++ lib.optional (plug != null) plugImpl;
 
     in
       lib.concatStringsSep "\n" (lib.filter (x: x != null && x != "") entries);
diff --git a/pkgs/test/vim/default.nix b/pkgs/test/vim/default.nix
index 4ca004a60c3..9128e1bcb11 100644
--- a/pkgs/test/vim/default.nix
+++ b/pkgs/test/vim/default.nix
@@ -1,14 +1,45 @@
-{ vimUtils, vim_configurable, neovim, vimPlugins
-, lib, fetchFromGitHub,
+{ vimUtils, vim_configurable, writeText, neovim, vimPlugins
+, lib, fetchFromGitHub, neovimUtils, wrapNeovimUnstable
+, neovim-unwrapped
 }:
 let
   inherit (vimUtils) buildVimPluginFrom2Nix;
 
   packages.myVimPackage.start = with vimPlugins; [ vim-nix ];
+
+  plugins = with vimPlugins; [
+    {
+      plugin = vim-obsession;
+      config = ''
+        map <Leader>$ <Cmd>Obsession<CR>
+      '';
+    }
+  ];
+
+  nvimConfNix = neovimUtils.makeNeovimConfig {
+    inherit plugins;
+    customRC = ''
+      " just a comment
+    '';
+  };
+
+  wrapNeovim = suffix: config:
+    wrapNeovimUnstable neovim-unwrapped (config // {
+      extraName = suffix;
+      wrapperArgs = lib.escapeShellArgs (config.wrapperArgs ++
+        ["--add-flags" "-u ${writeText "init.vim" config.neovimRcContent}"]
+      );
+    });
 in
 {
   vim_empty_config = vimUtils.vimrcFile { beforePlugins = ""; customRC = ""; };
 
+  ### neovim tests
+  ##############3
+  nvim_with_plugins = wrapNeovim "-with-plugins" nvimConfNix;
+
+  ### vim tests
+  ##############3
   vim_with_vim2nix = vim_configurable.customize {
     name = "vim"; vimrcConfig.vam.pluginDictionaries = [ "vim-addon-vim2nix" ];
   };