plugins/luasnip: add SnipMate loader

This commit is contained in:
Cezary Drożak 2024-06-07 08:51:13 +02:00
parent 0ddf66249f
commit 0f2c31e6a5
No known key found for this signature in database
GPG key ID: 4F88014312292BC9

View file

@ -8,6 +8,46 @@
with lib; with lib;
let let
cfg = config.plugins.luasnip; cfg = config.plugins.luasnip;
loaderSubmodule = types.submodule {
options = {
lazyLoad = mkOption {
type = types.bool;
default = true;
description = ''
Whether or not to lazy load the snippets
'';
};
# TODO: add option to also include the default runtimepath
paths =
helpers.mkNullOrOption
(
with helpers.nixvimTypes;
oneOf [
str
path
rawLua
(listOf (oneOf [
str
path
rawLua
]))
]
)
''
List of paths to load.
'';
exclude = helpers.mkNullOrOption (with helpers.nixvimTypes; maybeRaw (listOf (maybeRaw str))) ''
List of languages to exclude, by default is empty.
'';
include = helpers.mkNullOrOption (with helpers.nixvimTypes; maybeRaw (listOf (maybeRaw str))) ''
List of languages to include, by default is not set.
'';
};
};
in in
{ {
options.plugins.luasnip = { options.plugins.luasnip = {
@ -44,55 +84,24 @@ in
# require("luasnip.loaders.from_vscode").lazy_load({['paths'] = {'/nix/store/.../path/to/snippets'}}) # require("luasnip.loaders.from_vscode").lazy_load({['paths'] = {'/nix/store/.../path/to/snippets'}})
# #
''; '';
type = types.listOf ( type = types.listOf loaderSubmodule;
types.submodule { };
options = {
lazyLoad = mkOption { fromSnipmate = mkOption {
type = types.bool; default = [ ];
default = true;
description = '' description = ''
Whether or not to lazy load the snippets Luasnip does not support the full snipmate format: Only
`./{ft}.snippets` and `./{ft}/*.snippets` will be loaded. See
<https://github.com/honza/vim-snippets> for lots of examples.
''; '';
example = literalExpression ''
[
{ }
{ paths = ./path/to/snippets; }
]'';
type = types.listOf loaderSubmodule;
}; };
# TODO: add option to also include the default runtimepath
paths = mkOption {
default = null;
type =
with types;
nullOr (oneOf [
str
path
helpers.nixvimTypes.rawLua
(listOf (oneOf [
str
path
helpers.nixvimTypes.rawLua
]))
]);
};
exclude = mkOption {
type = types.nullOr (types.listOf types.str);
default = null;
description = ''
List of languages to exclude, by default is empty.
'';
};
include = mkOption {
type = types.nullOr (types.listOf types.str);
default = null;
description = ''
List of languages to include, by default is not set.
'';
};
};
}
);
};
# TODO: add support for snipmate
fromLua = mkOption { fromLua = mkOption {
default = [ ]; default = [ ];
description = '' description = ''
@ -107,40 +116,28 @@ in
} }
] ]
''; '';
type = types.listOf ( type = types.listOf loaderSubmodule;
types.submodule {
options = {
lazyLoad = mkOption {
type = types.bool;
default = true;
description = ''
Whether or not to lazy load the snippets
'';
};
paths = helpers.defaultNullOpts.mkNullable (
with types;
nullOr (oneOf [
str
path
helpers.nixvimTypes.rawLua
(listOf (oneOf [
str
path
helpers.nixvimTypes.rawLua
]))
])
) "" "Paths with snippets specified with native lua";
};
}
);
}; };
}; };
config = config =
let let
fromVscodeLoaders = lists.map ( loaderConfig =
loader: trivial.pipe
{
vscode = cfg.fromVscode;
snipmate = cfg.fromSnipmate;
lua = cfg.fromLua;
}
[
# Convert loader options to [{ name = "vscode"; loader = ...; }]
(attrsets.mapAttrsToList (name: loaders: lists.map (loader: { inherit name loader; }) loaders))
lists.flatten
(lists.map (
pair:
let let
inherit (pair) name loader;
options = attrsets.getAttrs [ options = attrsets.getAttrs [
"paths" "paths"
"exclude" "exclude"
@ -148,18 +145,10 @@ in
] loader; ] loader;
in in
'' ''
require("luasnip.loaders.from_vscode").${optionalString loader.lazyLoad "lazy_"}load(${helpers.toLuaObject options}) require("luasnip.loaders.from_${name}").${optionalString loader.lazyLoad "lazy_"}load(${helpers.toLuaObject options})
'' ''
) cfg.fromVscode; ))
fromLuaLoaders = lists.map ( ];
loader:
let
options = attrsets.getAttrs [ "paths" ] loader;
in
''
require("luasnip.loaders.from_lua").${optionalString loader.lazyLoad "lazy_"}load(${helpers.toLuaObject options})
''
) cfg.fromLua;
extraConfig = [ extraConfig = [
'' ''
require("luasnip").config.set_config(${helpers.toLuaObject cfg.extraConfig}) require("luasnip").config.set_config(${helpers.toLuaObject cfg.extraConfig})
@ -169,6 +158,6 @@ in
mkIf cfg.enable { mkIf cfg.enable {
extraPlugins = [ cfg.package ]; extraPlugins = [ cfg.package ];
extraLuaPackages = ps: [ ps.jsregexp ]; extraLuaPackages = ps: [ ps.jsregexp ];
extraConfigLua = concatStringsSep "\n" (extraConfig ++ fromVscodeLoaders ++ fromLuaLoaders); extraConfigLua = concatStringsSep "\n" (extraConfig ++ loaderConfig);
}; };
} }