neovimUtils: makeNeovimConfig accepts plugins/customRc

mimics home-manager interface and makes it easier to associate configs with plugins. Added a test as well.
This commit is contained in:
Matthieu Coudron 2021-05-21 12:48:43 +02:00
parent abb1e5cd4c
commit 7836469dbe
3 changed files with 76 additions and 12 deletions

View file

@ -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;

View file

@ -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);

View file

@ -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" ];
};