From 4b85dbd927768cf327c4bcfcf53af0f1f8a4b2da Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Thu, 9 May 2024 02:28:24 +0300 Subject: [PATCH] modules: add nvim-cmp --- module-list.nix | 1 + modules/build/neovim.nix | 16 +++--- modules/fn.nix | 4 +- modules/plugin.nix | 11 ++-- modules/plugins/language-server/lspconfig.nix | 30 ++++++++-- modules/plugins/navigation/hop-nvim.nix | 3 +- modules/plugins/navigation/nvim-tree.nix | 8 ++- modules/plugins/navigation/telescope.nix | 3 +- modules/plugins/nvim-cmp.nix | 57 +++++++++++++++++++ modules/plugins/style/nvim-treesitter.nix | 4 +- modules/profiles/recommended-plugins.nix | 2 + modules/vim/augroup.nix | 4 +- 12 files changed, 115 insertions(+), 28 deletions(-) create mode 100644 modules/plugins/nvim-cmp.nix diff --git a/module-list.nix b/module-list.nix index a5b2c72..c1e9581 100644 --- a/module-list.nix +++ b/module-list.nix @@ -22,6 +22,7 @@ ./modules/plugins/style/nvim-treesitter.nix ./modules/plugins/theme/catppuccin.nix ./modules/plugins/gitsigns.nix + ./modules/plugins/nvim-cmp.nix ################################################## (pkgs.path + "/nixos/modules/misc/assertions.nix") diff --git a/modules/build/neovim.nix b/modules/build/neovim.nix index 9634921..aa1ab49 100644 --- a/modules/build/neovim.nix +++ b/modules/build/neovim.nix @@ -34,22 +34,20 @@ in }; config.build.neovim = { - luaConfig = with lib.nix2lua; toLua (spaceBetween (lib.flatten [ + luaConfig = with lib; with nix2lua; toLua (spaceBetween (flatten [ # Global Opts - (lib.flip lib.mapAttrsToList config.vim.g (k: set "vim.g.${k}")) + (flip mapAttrsToList config.vim.g (k: set "vim.g.${k}")) # Opts - (lib.flip lib.mapAttrsToList config.vim.opt (k: set "vim.opt.${k}")) + (flip mapAttrsToList config.vim.opt (k: set "vim.opt.${k}")) # Plugins (map (v: v.genConfig) (filter (v: !v.isDependency) (attrValues config.plugin))) # Cmd - (lib.optional (config.vim.cmd != "") (call "vim.cmd" config.vim.cmd)) + (optional (config.vim.cmd != "") (call "vim.cmd" config.vim.cmd)) # Autocommands - (lib.flip lib.mapAttrsToList config.vim.augroup (k: v: v.genConfig)) + (flip mapAttrsToList config.vim.augroup (k: v: v.genConfig)) # Keymaps - (lib.flip map config.vim.keymap.set ({ mode, lhs, rhs, ... } @ vars: - let - m = if builtins.isList mode then lib.head mode else mode; - in + (flip map config.vim.keymap.set ({ mode, lhs, rhs, ... } @ vars: + let m = if builtins.isList mode then head mode else mode; in if config.vim.keymap._validate."${m}"."${lhs}" == rhs then vars.genConfig else abort "This case should never happen." )) diff --git a/modules/fn.nix b/modules/fn.nix index 17a6253..3c1b743 100644 --- a/modules/fn.nix +++ b/modules/fn.nix @@ -1,6 +1,8 @@ { lib, ... }: let + inherit (lib.nix2lua) lambda raw; + fnOpts = { name, config, ... }: { options = with lib; { args = mkOption { @@ -32,7 +34,7 @@ let }; config = { - lambda = with lib.nix2lua; + lambda = let fnArgs = builtins.listToAttrs (map (v: { name = v; value = raw v; }) config.args); innerCfg = config.content fnArgs; diff --git a/modules/plugin.nix b/modules/plugin.nix index e53ce31..459f7d1 100644 --- a/modules/plugin.nix +++ b/modules/plugin.nix @@ -61,11 +61,11 @@ let name = lib.mkDefault name; varName = lib.mkDefault (builtins.replaceStrings [ "-" "/" "." ] [ "_" "_" "_" ] config.name); - genConfig = with lib.nix2lua; lib.mkIf (!config.isDependency) (lib.flatten [ + genConfig = with lib; with nix2lua; mkIf (!config.isDependency) (flatten [ (local (set config.varName (require config.name))) - (lib.mapAttrsToList (k: v: local (set k (require v))) config.extraImports) + (mapAttrsToList (k: v: local (set k (require v))) config.extraImports) config.beforeSetup - (lib.optional (config.setupSettings != null) + (optional (config.setupSettings != null) (pipe1 (var config.varName) (call config.setupFnName config.setupSettings)) ) config.afterSetup @@ -78,9 +78,8 @@ let in { - options.plugin = with lib; with types; mkOption { - type = attrsOf (submodule pluginOpts); + options.plugin = with lib; mkOption { + type = with types; attrsOf (submodule pluginOpts); default = { }; }; - } diff --git a/modules/plugins/language-server/lspconfig.nix b/modules/plugins/language-server/lspconfig.nix index a2b3538..9008514 100644 --- a/modules/plugins/language-server/lspconfig.nix +++ b/modules/plugins/language-server/lspconfig.nix @@ -1,6 +1,7 @@ { config, lib, pkgs, ... }: let + inherit (lib.nix2lua) pipe pipe1 var set call; cfg = config.plugins.language-server.lspconfig; in { @@ -12,6 +13,16 @@ in serverSettings = mkOption { type = with types; attrsOf attrs; default = { }; + description = '' + Server-specific settings. + + `:help lspconfig-setup` + ''; + }; + + defaultServerSettings = mkOption { + type = with types; nullOr attrs; + default = null; }; keymap.set = mkOption { @@ -23,7 +34,7 @@ in config = lib.mkIf cfg.enable { fn.lspconfig-on-attach = { args = [ "event" ]; - content = with lib.nix2lua; { event }: { + content = { event }: { vim.keymap.set = map (attrs: attrs // { buffer = pipe1 event (var "buf"); }) cfg.keymap.set; @@ -35,13 +46,24 @@ in callback = config.fn.lspconfig-on-attach.lambda; }; - plugin.nvim-lspconfig = { + plugin.nvim-lspconfig = rec { name = "lspconfig"; + varName = name; package = cfg.package; - afterSetup = with lib.nix2lua; lib.mapAttrsToList + beforeSetup = + let varDefaultConfig = var "${varName}.util.default_config"; in + lib.optional (cfg.defaultServerSettings != null) (set + varDefaultConfig + (call "vim.tbl_extend" [ + "force" + varDefaultConfig + cfg.defaultServerSettings + ]) + ); + afterSetup = lib.mapAttrsToList (ls: lsSettings: pipe [ - (var "lspconfig") + (var varName) (var ls) (call "setup" lsSettings) ] diff --git a/modules/plugins/navigation/hop-nvim.nix b/modules/plugins/navigation/hop-nvim.nix index 10a5df6..6d04b8f 100644 --- a/modules/plugins/navigation/hop-nvim.nix +++ b/modules/plugins/navigation/hop-nvim.nix @@ -1,6 +1,7 @@ { config, lib, pkgs, ... }: let + inherit (lib.nix2lua) var; cfg = config.plugins.navigation.hop-nvim; in { @@ -48,7 +49,7 @@ in setupSettings = cfg.settings; }; - vim.keymap.set = with lib.nix2lua; cfg.keymap.set { + vim.keymap.set = cfg.keymap.set { after = var "hop_hint.HintDirection.AFTER_CURSOR"; before = var "hop_hint.HintDirection.BEFORE_CURSOR"; }; diff --git a/modules/plugins/navigation/nvim-tree.nix b/modules/plugins/navigation/nvim-tree.nix index 52f26cf..2e635c3 100644 --- a/modules/plugins/navigation/nvim-tree.nix +++ b/modules/plugins/navigation/nvim-tree.nix @@ -1,7 +1,9 @@ { config, lib, pkgs, ... }: -let cfg = config.plugins.navigation.nvim-tree; in - +let + inherit (lib.nix2lua) require pipe var call; + cfg = config.plugins.navigation.nvim-tree; +in { options.plugins.navigation.nvim-tree = with lib; { enable = mkEnableOption "nvim-tree"; @@ -40,7 +42,7 @@ let cfg = config.plugins.navigation.nvim-tree; in fn.nvim-tree-lua-on-attach = lib.mkIf (cfg.keymap.set != [ ]) { args = [ "bufnr" ]; content = { bufnr }: { - extra = with lib.nix2lua; lib.mkIf cfg.keymap.withDefault pipe [ + extra = lib.mkIf cfg.keymap.withDefault pipe [ (require "nvim-tree.api") (var "config") (var "mappings") diff --git a/modules/plugins/navigation/telescope.nix b/modules/plugins/navigation/telescope.nix index e87971a..4e177da 100644 --- a/modules/plugins/navigation/telescope.nix +++ b/modules/plugins/navigation/telescope.nix @@ -2,6 +2,7 @@ let inherit (builtins) match isFunction attrValues; + inherit (lib.nix2lua) pipe1 var call; cfg = config.plugins.navigation.telescope; @@ -38,7 +39,7 @@ let config = { name = lib.mkDefault name; - loadExtension = with lib.nix2lua; lib.mkDefault (pipe1 + loadExtension = lib.mkDefault (pipe1 (var config.plugin.telescope-nvim.varName) (call "load_extension" sub.config.name) ); diff --git a/modules/plugins/nvim-cmp.nix b/modules/plugins/nvim-cmp.nix new file mode 100644 index 0000000..a9d6783 --- /dev/null +++ b/modules/plugins/nvim-cmp.nix @@ -0,0 +1,57 @@ +{ config, lib, pkgs, ... }: + +let + inherit (lib.nix2lua) pipe1 var call call0; + cfg = config.plugins.nvim-cmp; +in +{ + options.plugins.nvim-cmp = with lib; { + enable = mkEnableOption "nvim-cmp"; + + package = mkPackageOption pkgs.vimPlugins "nvim-cmp" { }; + + settings = mkOption { + type = types.attrs; + default = { }; + }; + }; + + config = lib.mkIf cfg.enable (lib.mkMerge [ + { + plugin.luasnip = { }; + plugin.cmp_luasnip = lib.mkDefault { isDependency = true; }; + + fn.nvim-cmp-snippet-expand = { + args = [ "args" ]; + content = args: { + extra = pipe1 + (var config.plugin.luasnip.varName) + (call "lsp_expand" args); + }; + }; + + vim.opt.completeopt = [ "menu" "menuone" "noselect" ]; + + plugin.nvim-cmp = { + name = "cmp"; + package = cfg.package; + setupSettings = lib.mkMerge [ + cfg.settings + { snippet.expand = config.fn.nvim-cmp-snippet-expand.lambda; } + ]; + }; + } + + (lib.mkIf config.plugins.language-server.lspconfig.enable { + plugin.cmp-nvim-lsp = { + name = "cmp_nvim_lsp"; + }; + + plugins.language-server.lspconfig.defaultServerSettings.capabilities = + pipe1 + (var config.plugin.cmp-nvim-lsp.varName) + (call0 "default_capabilities"); + }) + ]); + +} diff --git a/modules/plugins/style/nvim-treesitter.nix b/modules/plugins/style/nvim-treesitter.nix index 928920a..525114b 100644 --- a/modules/plugins/style/nvim-treesitter.nix +++ b/modules/plugins/style/nvim-treesitter.nix @@ -83,12 +83,12 @@ in name = "nvim-treesitter.configs"; package = finalNvimTreeSitter; - beforeSetup = with lib.nix2lua; lib.optionals (cfg.extraGrammars != { }) (lib.flatten [ + beforeSetup = with lib; with nix2lua; optionals (cfg.extraGrammars != { }) (flatten [ (local (set "parser_config") (pipe1 (require "nvim-treesitter.parsers") (call0 "get_parser_configs")) ) - (lib.mapAttrsToList (k: v: set "parser_config.${v.language}" { }) cfg.extraGrammars) + (mapAttrsToList (k: v: set "parser_config.${v.language}" { }) cfg.extraGrammars) ]); setupSettings = lib.mkMerge [ diff --git a/modules/profiles/recommended-plugins.nix b/modules/profiles/recommended-plugins.nix index 226db7c..f518d02 100644 --- a/modules/profiles/recommended-plugins.nix +++ b/modules/profiles/recommended-plugins.nix @@ -66,5 +66,7 @@ let inherit (lib.nix2lua) nf; in }; gitsigns.enable = lib.mkDefault true; + + nvim-cmp.enable = lib.mkDefault true; }; } diff --git a/modules/vim/augroup.nix b/modules/vim/augroup.nix index bb8d2d8..e65267a 100644 --- a/modules/vim/augroup.nix +++ b/modules/vim/augroup.nix @@ -2,6 +2,8 @@ let + inherit (lib.nix2lua) call; + augroupsCfg = config.vim.augroup; autocmd = { name, config, ... }: { @@ -86,7 +88,7 @@ let config = { group = lib.mkDefault name; - genConfig = with lib.nix2lua; call "vim.api.nvim_create_autocmd" [ + genConfig = call "vim.api.nvim_create_autocmd" [ config.event { inherit (config) pattern buffer desc callback command once nested;