nixeovim/modules/plugins/snippet/luasnip.nix

202 lines
5 KiB
Nix
Raw Normal View History

2024-05-09 18:41:09 +03:00
{ config, lib, pkgs, ... }:
let
2024-05-20 17:37:37 +03:00
inherit (lib.nix2lua) LuaNil scope lset pipe1 call call1;
2024-05-09 18:41:09 +03:00
cfg = config.plugins.snippet.luasnip;
2024-05-20 17:37:37 +03:00
nodes = lib.mkOption {
type = with lib.types; listOf (submodule snippetNodeOpts);
2024-05-21 01:14:29 +03:00
default = [ ];
2024-05-20 17:37:37 +03:00
};
unwrapNodes = map (n: n.genConfig);
2024-05-21 01:14:29 +03:00
2024-05-20 17:37:37 +03:00
genConfig = lib.mkOption {
type = lib.types.attrs;
internal = true;
};
snippetNodeOpts = { config, ... }: {
options = with lib; with types; {
2024-05-21 01:14:29 +03:00
kind = mkOption {
type = enum [ "text" "insert" "choice" "snippet" ];
};
2024-05-20 17:37:37 +03:00
text = mkOption {
type = nullOr (either str (listOf str));
default = null;
};
2024-05-21 01:14:29 +03:00
jump = lib.mkOption {
type = with lib.types; nullOr number;
2024-05-20 17:37:37 +03:00
default = null;
2024-05-21 01:14:29 +03:00
description = ''
This determines when this node will be jumped to.
2024-05-20 17:37:37 +03:00
2024-05-21 01:14:29 +03:00
`:help luasnip-insertnode`
`:help luasnip-basics-jump-index`
'';
2024-05-20 17:37:37 +03:00
};
2024-05-21 01:14:29 +03:00
inherit nodes;
choices = nodes;
2024-05-21 01:14:29 +03:00
# only for kind=snippet
indentString = mkOption {
type = types.str;
default = "";
2024-05-20 17:37:37 +03:00
};
inherit genConfig;
};
2024-05-21 01:14:29 +03:00
config =
let
inherit (config) kind text jump nodes choices;
2024-05-21 01:14:29 +03:00
unwrapJump = index: if index == null then LuaNil else index;
in
{
kind = lib.mkDefault (
if choices != [ ] then "choice"
else if nodes != [ ] then "snippet"
2024-05-21 01:14:29 +03:00
else if jump != null then "insert"
else "text"
);
genConfig =
if kind == "text" then
if text != null then call1 "t" text
else throw "luasnip textnode require a 'text' config"
else if kind == "insert" then call "i" [ (unwrapJump jump) text ]
else if kind == "choice" then call "c" [ (unwrapJump jump) (unwrapNodes choices) ]
2024-05-21 01:14:29 +03:00
else if kind == "snippet" then
if config.indentString != "" then
call "isn" [ (unwrapJump jump) (unwrapNodes nodes) config.indentString ]
else
call "sn" [ (unwrapJump jump) (unwrapNodes nodes) ]
else null;
};
2024-05-20 17:37:37 +03:00
};
snippetOpts = { name, ... } @ sub: {
options = with lib; with types; {
context = mkOption {
type = either str attrs;
description = ''
`:help luasnip-snippets';
'';
};
2024-05-21 01:14:29 +03:00
inherit nodes;
2024-05-20 17:37:37 +03:00
inherit genConfig;
};
config = {
context = lib.mkDefault name;
genConfig = call "s" [ sub.config.context (unwrapNodes sub.config.nodes) ];
};
};
snippetGroupOpts = { ... } @ sub: {
options = with lib; {
filetype = mkOption {
type = types.str;
};
opts = {
type = mkOption {
type = types.nullOr (types.enum [ "snippets" "autosnippets" ]);
default = null;
};
key = mkOption {
type = types.nullOr types.str;
default = null;
};
override_priority = mkOption {
type = types.nullOr types.number;
default = null;
description = ''
Set priority for all snippets.
`:help luasnip-api`
'';
};
default_priority = mkOption {
type = types.nullOr types.number;
default = null;
description = ''
Set priority only for snippets without snippet priority.
`:help luasnip-api`
'';
};
};
snippets = mkOption {
type = types.attrsOf (types.submodule snippetOpts);
default = { };
};
inherit genConfig;
};
config = {
genConfig =
let plugin = config.plugin.luasnip; in
let inherit (sub.config) filetype snippets opts; in
pipe1 plugin.var (call "add_snippets" [
filetype
(map (s: s.genConfig) (builtins.attrValues snippets))
opts
]);
};
};
2024-05-09 18:41:09 +03:00
in
{
options.plugins.snippet.luasnip = with lib; {
enable = mkEnableOption "luasnip";
package = mkPackageOption pkgs.vimPlugins "luasnip" { };
2024-05-10 00:27:03 +03:00
settings = mkOption {
type = types.attrs;
default = { };
};
2024-05-20 17:37:37 +03:00
snippetGroups = mkOption {
type = types.listOf (types.submodule snippetGroupOpts);
default = [ ];
};
2024-05-09 18:41:09 +03:00
};
config = lib.mkIf cfg.enable {
2024-05-20 17:37:37 +03:00
plugin.luasnip = rec {
2024-05-09 18:41:09 +03:00
inherit (cfg) package;
2024-05-20 17:37:37 +03:00
varName = "luasnip";
2024-05-10 00:27:03 +03:00
setupSettings = cfg.settings;
2024-05-21 00:06:11 +03:00
extraImports = lib.mkIf (cfg.snippetGroups != [ ]) {
luasnip_types = "luasnip.util.types";
};
2024-05-20 17:37:37 +03:00
afterSetup = lib.mkIf (cfg.snippetGroups != [ ]) [
(scope (lib.flatten [
(lib.mapAttrsToList (k: v: lset k (pipe1 varName v)) {
s = "snippet";
sn = "snippet_node";
isn = "indent_snippet_node";
t = "text_node";
i = "insert_node";
f = "function_node";
c = "choice_node";
d = "dynamic_node";
r = "restore_node";
})
(map (sg: sg.genConfig) cfg.snippetGroups)
]))
];
2024-05-09 18:41:09 +03:00
};
};
}