From c34c941c8fee09baa1a3c1bfb085a3c9637f5e93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=A9tan=20Lepage?= <33058747+GaetanLepage@users.noreply.github.com> Date: Sun, 12 Mar 2023 22:31:32 +0100 Subject: [PATCH] plugins/nvim-cmp: add enum type for option snippet.expand (#244) --- plugins/completion/nvim-cmp/default.nix | 58 +++++++++++++++++-------- tests/plugins/nvim-cmp.nix | 9 +++- 2 files changed, 49 insertions(+), 18 deletions(-) diff --git a/plugins/completion/nvim-cmp/default.nix b/plugins/completion/nvim-cmp/default.nix index 22b2defe..34b45c70 100644 --- a/plugins/completion/nvim-cmp/default.nix +++ b/plugins/completion/nvim-cmp/default.nix @@ -8,6 +8,13 @@ with lib; let cfg = config.plugins.nvim-cmp; helpers = import ../../helpers.nix {inherit lib;}; cmpLib = import ./cmp-helpers.nix args; + + snippetEngines = { + "vsnip" = ''vim.fn["vsnip#anonymous"](args.body)''; + "luasnip" = ''require('luasnip').lsp_expand(args.body)''; + "snippy" = ''require('snippy').expand_snippet(args.body)''; + "ultisnips" = ''vim.fn["UltiSnips#Anon"](args.body)''; + }; in { options.plugins.nvim-cmp = { enable = mkEnableOption "nvim-cmp"; @@ -94,24 +101,34 @@ in { snippet = helpers.mkCompositeOption "Snippet options" { expand = - helpers.defaultNullOpts.mkStr - '' - function(_) - error('snippet engine is not configured.') - end - '' + helpers.mkNullOrOption + ( + types.either + helpers.rawType + (types.enum (attrNames snippetEngines)) + ) '' The snippet expansion function. That's how nvim-cmp interacts with a particular snippet engine. - Example: + You may directly provide one of those four supported engines: + - vsnip + - luasnip + - snippy + - ultisnips + + You can also provide a custom function: ``` - function(args) - vim.fn["vsnip#anonymous"](args.body) -- For `vsnip` users. - -- require('luasnip').lsp_expand(args.body) -- For `luasnip` users. - -- require('snippy').expand_snippet(args.body) -- For `snippy` users. - -- vim.fn["UltiSnips#Anon"](args.body) -- For `ultisnips` users. - end + { + __raw = \'\' + function(args) + vim.fn["vsnip#anonymous"](args.body) -- For `vsnip` users. + -- require('luasnip').lsp_expand(args.body) -- For `luasnip` users. + -- require('snippy').expand_snippet(args.body) -- For `snippy` users. + -- vim.fn["UltiSnips#Anon"](args.body) -- For `ultisnips` users. + end + \'\'; + }; ``` ''; }; @@ -486,10 +503,17 @@ in { helpers.mkRaw wrapped; snippet = helpers.ifNonNull' cfg.snippet { - expand = - helpers.ifNonNull' - cfg.snippet.expand - (helpers.mkRaw cfg.snippet.expand); + expand = let + expand = cfg.snippet.expand; + in + if isString expand + then + helpers.mkRaw '' + function(args) + ${snippetEngines.${expand}} + end + '' + else expand; }; completion = helpers.ifNonNull' cfg.completion { diff --git a/tests/plugins/nvim-cmp.nix b/tests/plugins/nvim-cmp.nix index 50474830..51ff1fb3 100644 --- a/tests/plugins/nvim-cmp.nix +++ b/tests/plugins/nvim-cmp.nix @@ -4,6 +4,13 @@ plugins.nvim-cmp.enable = true; }; + snippetEngine = { + plugins.nvim-cmp = { + enable = true; + snippet.expand = "luasnip"; + }; + }; + # All the upstream default options of nvim-cmp defaults = { plugins.nvim-cmp = { @@ -18,7 +25,7 @@ preselect = "Item"; snippet = { - expand = '' + expand.__raw = '' function(_) error('snippet engine is not configured.') end