From b7431ca7d79c64427c5b3344baf9a51992db41bc Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Thu, 9 May 2024 18:41:09 +0300 Subject: [PATCH] modules: add snippet backends --- module-list.nix | 4 + modules/plugins/nvim-cmp.nix | 97 ++++++++++++++++++------ modules/plugins/snippet/luasnip.nix | 18 +++++ modules/plugins/snippet/snippy.nix | 19 +++++ modules/plugins/snippet/ultisnips.nix | 20 +++++ modules/plugins/snippet/vsnip.nix | 20 +++++ modules/profiles/recommended-plugins.nix | 2 + 7 files changed, 157 insertions(+), 23 deletions(-) create mode 100644 modules/plugins/snippet/luasnip.nix create mode 100644 modules/plugins/snippet/snippy.nix create mode 100644 modules/plugins/snippet/ultisnips.nix create mode 100644 modules/plugins/snippet/vsnip.nix diff --git a/module-list.nix b/module-list.nix index c1e9581..1152ca5 100644 --- a/module-list.nix +++ b/module-list.nix @@ -18,6 +18,10 @@ ./modules/plugins/navigation/hop-nvim.nix ./modules/plugins/navigation/telescope.nix ./modules/plugins/navigation/nvim-tree.nix + ./modules/plugins/snippet/luasnip.nix + ./modules/plugins/snippet/snippy.nix + ./modules/plugins/snippet/ultisnips.nix + ./modules/plugins/snippet/vsnip.nix ./modules/plugins/style/neoformat.nix ./modules/plugins/style/nvim-treesitter.nix ./modules/plugins/theme/catppuccin.nix diff --git a/modules/plugins/nvim-cmp.nix b/modules/plugins/nvim-cmp.nix index a9d6783..7f7158d 100644 --- a/modules/plugins/nvim-cmp.nix +++ b/modules/plugins/nvim-cmp.nix @@ -3,6 +3,8 @@ let inherit (lib.nix2lua) pipe1 var call call0; cfg = config.plugins.nvim-cmp; + + inherit (config.plugins) snippet language-server; in { options.plugins.nvim-cmp = with lib; { @@ -16,33 +18,37 @@ in }; }; + 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; } - ]; - }; + assertions = [ + { + assertion = snippet.luasnip.enable || snippet.snippy.enable || snippet.vsnip.enable || snippet.ultisnips.enable; + message = "One of snippet backend (luasnip, snippy, vsnip, ultisnip) is required to use nvim-cmp"; + } + ]; } - (lib.mkIf config.plugins.language-server.lspconfig.enable { + # Backend dependencies + + (lib.mkIf snippet.luasnip.enable { + plugin.cmp_luasnip = { + isDependency = true; + }; + }) + (lib.mkIf snippet.snippy.enable { + plugin.cmp-snippy = { isDependency = true; }; + }) + (lib.mkIf snippet.vsnip.enable { + plugin.cmp-vsnip = { isDependency = true; }; + }) + (lib.mkIf snippet.ultisnips.enable { + plugin.cmp-nvim-ultisnips = { isDependency = true; }; + }) + + # Lsp configuration + + (lib.mkIf language-server.lspconfig.enable { plugin.cmp-nvim-lsp = { name = "cmp_nvim_lsp"; }; @@ -52,6 +58,51 @@ in (var config.plugin.cmp-nvim-lsp.varName) (call0 "default_capabilities"); }) + + # Base configuration + + { + vim.opt.completeopt = [ "menu" "menuone" "noselect" ]; + + fn.nvim-cmp-snippet-expand = { + args = [ "args" ]; + content = { args }: + let body = pipe1 args (var "body"); in { + extra = + if snippet.luasnip.enable then + pipe1 (var config.plugin.luasnip.varName) (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) + else if snippet.ultisnips.enable then + call "vim.fn[\"${config.plugin.ultisnips.name}#Anon\"]" body + else + call "vim.snippet.expand" body; # For native neovim snippets (Neovim v0.10+); + }; + }; + + plugin.nvim-cmp = rec { + name = "cmp"; + varName = name; + package = cfg.package; + setupSettings = lib.mkMerge [ + { + mapping = call0 "${varName}.mapping.preset.insert"; + sources = + lib.optional language-server.lspconfig.enable { name = "nvim_lsp"; group_index = 1; } + ++ lib.optional snippet.luasnip.enable { name = "luasnip"; group_index = 5; } + ++ lib.optional snippet.snippy.enable { name = "snippy"; group_index = 5; } + ++ lib.optional snippet.vsnip.enable { name = "vsnip"; group_index = 5; } + ++ lib.optional snippet.ultisnips.enable { name = "ultisnips"; group_index = 5; } + ++ [{ name = "buffer"; group_index = 10; max_item_count = 10; }]; + } + cfg.settings + { snippet.expand = config.fn.nvim-cmp-snippet-expand.lambda; } + ]; + }; + } + ]); } diff --git a/modules/plugins/snippet/luasnip.nix b/modules/plugins/snippet/luasnip.nix new file mode 100644 index 0000000..2f333fd --- /dev/null +++ b/modules/plugins/snippet/luasnip.nix @@ -0,0 +1,18 @@ +{ config, lib, pkgs, ... }: + +let + cfg = config.plugins.snippet.luasnip; +in +{ + options.plugins.snippet.luasnip = with lib; { + enable = mkEnableOption "luasnip"; + + package = mkPackageOption pkgs.vimPlugins "luasnip" { }; + }; + + config = lib.mkIf cfg.enable { + plugin.luasnip = { + inherit (cfg) package; + }; + }; +} diff --git a/modules/plugins/snippet/snippy.nix b/modules/plugins/snippet/snippy.nix new file mode 100644 index 0000000..995a371 --- /dev/null +++ b/modules/plugins/snippet/snippy.nix @@ -0,0 +1,19 @@ +{ config, lib, pkgs, ... }: + +let + cfg = config.plugins.snippet.snippy; +in +{ + options.plugins.snippet.snippy = with lib; { + enable = mkEnableOption "snippy"; + + package = mkPackageOption pkgs.vimPlugins "nvim-snippy" { }; + }; + + config = lib.mkIf cfg.enable { + plugin.nvim-snippy = { + name = "snippy"; + inherit (cfg) package; + }; + }; +} diff --git a/modules/plugins/snippet/ultisnips.nix b/modules/plugins/snippet/ultisnips.nix new file mode 100644 index 0000000..6529a03 --- /dev/null +++ b/modules/plugins/snippet/ultisnips.nix @@ -0,0 +1,20 @@ +{ config, lib, pkgs, ... }: + +let + cfg = config.plugins.snippet.ultisnips; +in +{ + options.plugins.snippet.ultisnips = with lib; { + enable = mkEnableOption "ultisnips"; + + package = mkPackageOption pkgs.vimPlugins "ultisnips" { }; + }; + + config = lib.mkIf cfg.enable { + plugin.ultisnips = { + inherit (cfg) package; + name = "UltiSnips"; + isDependency = true; + }; + }; +} diff --git a/modules/plugins/snippet/vsnip.nix b/modules/plugins/snippet/vsnip.nix new file mode 100644 index 0000000..334871e --- /dev/null +++ b/modules/plugins/snippet/vsnip.nix @@ -0,0 +1,20 @@ +{ config, lib, pkgs, ... }: + +let + cfg = config.plugins.snippet.vsnip; +in +{ + options.plugins.snippet.vsnip = with lib; { + enable = mkEnableOption "vsnip"; + + package = mkPackageOption pkgs.vimPlugins "vim-vsnip" { }; + }; + + config = lib.mkIf cfg.enable { + plugin.vim-vsnip = { + inherit (cfg) package; + name = "vsnip"; + isDependency = true; + }; + }; +} diff --git a/modules/profiles/recommended-plugins.nix b/modules/profiles/recommended-plugins.nix index f518d02..3bf6776 100644 --- a/modules/profiles/recommended-plugins.nix +++ b/modules/profiles/recommended-plugins.nix @@ -53,6 +53,8 @@ let inherit (lib.nix2lua) nf; in }; }; + snippet.luasnip.enable = lib.mkDefault true; + style = { neoformat.enable = lib.mkDefault true; nvim-treesitter.enable = lib.mkDefault true;