plugins/snippets: move to by-name

This commit is contained in:
Matt Sturgeon 2024-09-05 02:41:27 +01:00
parent 2a91b89445
commit 82e7d153e4
No known key found for this signature in database
GPG key ID: 4F91844CED1A8299
7 changed files with 0 additions and 4 deletions

View file

@ -0,0 +1,32 @@
{
lib,
helpers,
config,
pkgs,
...
}:
with lib;
let
cfg = config.plugins.friendly-snippets;
in
{
meta.maintainers = [ maintainers.GaetanLepage ];
options.plugins.friendly-snippets = {
enable = mkEnableOption "friendly-snippets";
package = lib.mkPackageOption pkgs "friendly-snippets" {
default = [
"vimPlugins"
"friendly-snippets"
];
};
};
config = mkIf cfg.enable {
extraPlugins = [ cfg.package ];
# Simply add an element to the `fromVscode` list to trigger the import of friendly-snippets
plugins.luasnip.fromVscode = [ { } ];
};
}

View file

@ -0,0 +1,209 @@
{
lib,
helpers,
config,
pkgs,
...
}:
with lib;
let
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
{
imports =
let
basePluginPath = [
"plugins"
"luasnip"
];
in
[
# TODO introduced 2024-08-04. Remove after 24.11
(lib.mkRenamedOptionModule (basePluginPath ++ [ "extraConfig" ]) (basePluginPath ++ [ "settings" ]))
];
options.plugins.luasnip = {
enable = mkEnableOption "luasnip";
package = lib.mkPackageOption pkgs "luasnip" {
default = [
"vimPlugins"
"luasnip"
];
};
settings = mkOption {
type = with types; attrsOf anything;
description = ''
Options provided to the `require('luasnip').config.setup()` function.",
'';
example = {
enable_autosnippets = true;
store_selection_keys = "<Tab>";
};
default = { };
};
fromVscode = mkOption {
default = [ ];
example = literalExpression ''
[
{ }
{ paths = ./path/to/snippets; }
]'';
description = ''
List of custom vscode style snippets to load.
For example,
```nix
[ {} { paths = ./path/to/snippets; } ]
```
will generate the following lua:
```lua
require("luasnip.loaders.from_vscode").lazy_load({})
require("luasnip.loaders.from_vscode").lazy_load({['paths'] = {'/nix/store/.../path/to/snippets'}})
```
'';
type = types.listOf loaderSubmodule;
};
fromSnipmate = mkOption {
default = [ ];
description = ''
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;
};
fromLua = mkOption {
default = [ ];
description = ''
Load lua snippets with the lua loader.
Check <https://github.com/L3MON4D3/LuaSnip/blob/master/DOC.md#lua> for the necessary file structure.
'';
example = ''
[
{}
{
paths = ./path/to/snippets;
}
]
'';
type = types.listOf loaderSubmodule;
};
filetypeExtend = mkOption {
default = { };
type = with types; attrsOf (listOf str);
example = {
lua = [
"c"
"cpp"
];
};
description = ''
Wrapper for the `filetype_extend` function.
Keys are filetypes (`filetype`) and values are list of filetypes (`["ft1" "ft2" "ft3"]`).
Tells luasnip that for a buffer with `ft=filetype`, snippets from `extend_filetypes` should
be searched as well.
For example, `filetypeExtend.lua = ["c" "cpp"]` would search and expand c and cpp snippets
for lua files.
'';
};
};
config =
let
loaderConfig =
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
inherit (pair) name loader;
options = attrsets.getAttrs [
"paths"
"exclude"
"include"
] loader;
in
''
require("luasnip.loaders.from_${name}").${optionalString loader.lazyLoad "lazy_"}load(${helpers.toLuaObject options})
''
))
];
filetypeExtendConfig = mapAttrsToList (n: v: ''
require("luasnip").extend_filetypes("${n}", ${helpers.toLuaObject v})
'') cfg.filetypeExtend;
extraConfig = [
''
require("luasnip").config.setup(${helpers.toLuaObject cfg.settings})
''
];
in
mkIf cfg.enable {
extraPlugins = [ cfg.package ];
extraLuaPackages = ps: [ ps.jsregexp ];
extraConfigLua = concatStringsSep "\n" (extraConfig ++ loaderConfig ++ filetypeExtendConfig);
};
}

View file

@ -0,0 +1,60 @@
{
lib,
...
}:
let
inherit (lib) types;
inherit (lib.nixvim) defaultNullOpts;
in
lib.nixvim.neovim-plugin.mkNeovimPlugin {
name = "nvim-snippets";
luaName = "snippets";
maintainers = [ lib.maintainers.psfloyd ];
settingsOptions = {
create_autocmd = defaultNullOpts.mkBool false ''
Optionally load all snippets when opening a file.
Only needed if not using nvim-cmp.
'';
create_cmp_source = defaultNullOpts.mkBool true ''
Optionally create a nvim-cmp source.
Source name will be snippets.
'';
friendly_snippets = defaultNullOpts.mkBool false ''
Set to true if using friendly-snippets.
'';
ignored_filetypes = defaultNullOpts.mkListOf types.str null ''
Filetypes to ignore when loading snippets.
'';
extended_filetypes = defaultNullOpts.mkAttrsOf types.anything null ''
Filetypes to load snippets for in addition to the default ones. ex: {typescript = {
'javascript'}}'';
global_snippets = defaultNullOpts.mkListOf types.str [ "all" ] ''
Snippets to load for all filetypes.
'';
search_paths =
defaultNullOpts.mkListOf types.str [ { __raw = "vim.fn.stdpath('config') .. '/snippets'"; } ]
''
Paths to search for snippets.
'';
};
settingsExample = {
create_autocmd = true;
create_cmp_source = true;
friendly_snippets = true;
ignored_filetypes = [ "lua" ];
extended_filetypes = {
typescript = [ "javascript" ];
};
global_snippets = [ "all" ];
search_paths = [ { __raw = "vim.fn.stdpath('config') .. '/snippets'"; } ];
};
}