feat(nvim): use lazy to load plugins

This commit is contained in:
Moritz Böhme 2023-06-10 21:52:57 +02:00
parent 3204ac12e7
commit 80c38b5120
No known key found for this signature in database
GPG key ID: 970C6E89EB0547A9
11 changed files with 604 additions and 266 deletions

View file

@ -7,22 +7,207 @@
with lib;
let
cfg = config.my.programs.nvim;
mkPlugin = fileName:
boolToString = bool: if bool then "true" else "false";
quote = str: ''"${toString str}"'';
listToString = list: ''{ ${concatStringsSep ", " (map quote list)} }'';
keybinding =
{ key
, cmd
, func
, mode
, desc
}:
let
path = ./plugins + "/${fileName}";
pluginName = lib.removeSuffix ".lua" fileName;
cmdString =
if cmd != null
then quote cmd
else
(
if func != null
then func
else abort "Either cmd or function must be set"
);
in
{
plugin = pkgs.vimPlugins.${pluginName};
type = "lua";
config = lib.readFile path;
};
pluginFileNames = builtins.attrNames (builtins.readDir ./plugins);
pluginsWithConfig = builtins.map mkPlugin pluginFileNames;
''{ ${quote key}, ${cmdString}, mode = ${quote mode}, ${optionalString (desc != null) "desc = ${quote desc},"} }'';
lazySpecFromPlugin =
{ plugin
, dependencies
, init
, conf
, lazy
, event
, enabled
, cmd
, ft
, priority
, keys
}:
''
{
dir = "${plugin}",
name = "${plugin.name}",
lazy = ${boolToString lazy},
enabled = ${boolToString enabled},
dependencies = { ${concatStringsSep ", " (map lazySpecFromPlugin dependencies)} },
${optionalString (init != null)
"init = function(plugin)
${toString init}
end,"
}
${optionalString (conf != null)
"config = function(plugin, opts)
${toString conf}
end,"
}
keys = { ${concatStringsSep ",\n" (map keybinding keys)} },
event = ${listToString event},
cmd = ${listToString cmd},
ft = ${listToString ft},
priority = ${toString priority},
}
'';
lazySpecs = concatStringsSep ", " (map lazySpecFromPlugin cfg.plugins);
lazy = ''
require("lazy").setup({
${lazySpecs}
})
'';
in
{
options.my.programs.nvim.enable = mkEnableOption "nvim";
imports = [ ./plugins ];
options.my.programs.nvim = {
enable = mkEnableOption "nvim";
plugins = mkOption {
default = [ ];
description = ''
List of plugins with config.
'';
type = with types; listOf (
let
sub = submodule {
options = {
conf = mkOption {
type = nullOr str;
default = null;
description = ''
Lua code to be executed when the plugin is loaded.
'';
};
dependencies = mkOption {
type = listOf sub;
default = [ ];
description = ''
List of plugins this plugin depends on.
'';
};
init = mkOption {
type = nullOr str;
default = null;
description = ''
Lua code to be executed when the plugin is initialized.
'';
};
event = mkOption {
type = listOf str;
default = [ ];
description = ''
Event to load the plugin on.
'';
};
lazy = mkOption {
type = bool;
default = false;
description = ''
Whether to load the plugin lazily.
'';
};
plugin = mkOption {
type = package;
description = ''
The plugin package.
'';
};
enabled = mkOption {
type = bool;
default = true;
description = ''
Whether to enable the plugin.
'';
};
cmd = mkOption {
type = listOf str;
default = [ ];
description = ''
Command to load the plugin.
'';
};
ft = mkOption {
type = listOf str;
default = [ ];
description = ''
Filetype to load the plugin on.
'';
};
priority = mkOption {
type = int;
default = 50;
description = ''
Priority to load the plugin.
'';
};
keys = mkOption {
default = [ ];
description = ''
List of keybindings.
'';
type = listOf (submodule {
options = {
key = mkOption {
type = str;
description = ''
Key to bind.
'';
};
cmd = mkOption {
type = nullOr str;
default = null;
description = ''
Command to execute.
'';
};
func = mkOption {
type = nullOr str;
default = null;
description = ''
Function to execute.
'';
};
mode = mkOption {
type = str;
default = "n";
description = ''
Mode to bind the key in.
'';
};
desc = mkOption {
type = nullOr str;
default = null;
description = ''
Description of the keybinding.
'';
};
};
});
};
};
};
in
sub
);
};
};
config = mkIf cfg.enable {
home-manager.users.moritz = {
@ -41,11 +226,7 @@ in
vimdiffAlias = true;
withNodeJs = true;
withPython3 = true;
extraLuaConfig = lib.concatLines (
builtins.map
builtins.readFile
[ ./options.lua ./keybinds.lua ./init.lua ]
);
extraLuaConfig = lib.concatLines [ (builtins.readFile ./options.lua) lazy ];
extraPackages = with pkgs; [
alejandra
black
@ -66,30 +247,10 @@ in
yamlfmt
];
plugins = with pkgs.vimPlugins; [
cmp-async-path
cmp-nvim-lsp
cmp_luasnip
copilot-cmp
direnv-vim
friendly-snippets
lsp_lines-nvim
lspkind-nvim
lspsaga-nvim-original
luasnip
nui-nvim
nvim-cmp
nvim-lspconfig
lazy-nvim
nvim-treesitter.withAllGrammars
nvim-ufo
nvim-web-devicons
plenary-nvim
popup-nvim
promise-async
vim-fugitive
vim-tmux-navigator
] ++ pluginsWithConfig;
];
};
};
};
}