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; cfg = config.plugins.nvim-cmp;
helpers = import ../../helpers.nix {inherit lib;}; helpers = import ../../helpers.nix {inherit lib;};
cmpLib = import ./cmp-helpers.nix args; 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 { in {
options.plugins.nvim-cmp = { options.plugins.nvim-cmp = {
enable = mkEnableOption "nvim-cmp"; enable = mkEnableOption "nvim-cmp";
@ -94,24 +101,34 @@ in {
snippet = helpers.mkCompositeOption "Snippet options" { snippet = helpers.mkCompositeOption "Snippet options" {
expand = expand =
helpers.defaultNullOpts.mkStr helpers.mkNullOrOption
'' (
function(_) types.either
error('snippet engine is not configured.') helpers.rawType
end (types.enum (attrNames snippetEngines))
'' )
'' ''
The snippet expansion function. That's how nvim-cmp interacts with a The snippet expansion function. That's how nvim-cmp interacts with a
particular snippet engine. 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. __raw = \'\'
-- require('luasnip').lsp_expand(args.body) -- For `luasnip` users. function(args)
-- require('snippy').expand_snippet(args.body) -- For `snippy` users. vim.fn["vsnip#anonymous"](args.body) -- For `vsnip` users.
-- vim.fn["UltiSnips#Anon"](args.body) -- For `ultisnips` users. -- require('luasnip').lsp_expand(args.body) -- For `luasnip` users.
end -- 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; helpers.mkRaw wrapped;
snippet = helpers.ifNonNull' cfg.snippet { snippet = helpers.ifNonNull' cfg.snippet {
expand = expand = let
helpers.ifNonNull' expand = cfg.snippet.expand;
cfg.snippet.expand in
(helpers.mkRaw cfg.snippet.expand); if isString expand
then
helpers.mkRaw ''
function(args)
${snippetEngines.${expand}}
end
''
else expand;
}; };
completion = helpers.ifNonNull' cfg.completion { completion = helpers.ifNonNull' cfg.completion {

View file

@ -4,6 +4,13 @@
plugins.nvim-cmp.enable = true; plugins.nvim-cmp.enable = true;
}; };
snippetEngine = {
plugins.nvim-cmp = {
enable = true;
snippet.expand = "luasnip";
};
};
# All the upstream default options of nvim-cmp # All the upstream default options of nvim-cmp
defaults = { defaults = {
plugins.nvim-cmp = { plugins.nvim-cmp = {
@ -18,7 +25,7 @@
preselect = "Item"; preselect = "Item";
snippet = { snippet = {
expand = '' expand.__raw = ''
function(_) function(_)
error('snippet engine is not configured.') error('snippet engine is not configured.')
end end