plugins/nvim-cmp: add enum type for option snippet.expand (#244)

This commit is contained in:
Gaétan Lepage 2023-03-12 22:31:32 +01:00 committed by GitHub
parent bfbe737aa3
commit c34c941c8f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 18 deletions

View file

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

View file

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