modules/luasnip!: refac nodes

This commit is contained in:
Dmitriy Pleshevskiy 2024-05-21 01:14:29 +03:00
parent 8829e32f00
commit aeb09834d6
Signed by: pleshevskiy
GPG key ID: 17041163DA10A9A2

View file

@ -4,6 +4,28 @@ let
inherit (lib.nix2lua) LuaNil scope lset pipe1 call call1; inherit (lib.nix2lua) LuaNil scope lset pipe1 call call1;
cfg = config.plugins.snippet.luasnip; cfg = config.plugins.snippet.luasnip;
nodes = lib.mkOption {
type = with lib.types; listOf (submodule snippetNodeOpts);
default = [ ];
};
unwrapNodes = map (n: n.genConfig);
genConfig = lib.mkOption {
type = lib.types.attrs;
internal = true;
};
snippetNodeOpts = { config, ... }: {
options = with lib; with types; {
kind = mkOption {
type = enum [ "text" "insert" "choice" "snippet" ];
};
text = mkOption {
type = nullOr (either str (listOf str));
default = null;
};
jump = lib.mkOption { jump = lib.mkOption {
type = with lib.types; nullOr number; type = with lib.types; nullOr number;
@ -15,84 +37,42 @@ let
`:help luasnip-basics-jump-index` `:help luasnip-basics-jump-index`
''; '';
}; };
unwrapJump = index: if index == null then LuaNil else index;
nodes = lib.mkOption { inherit nodes;
type = with lib.types; listOf (submodule snippetNodeOpts);
};
unwrapNodes = map (n: n.genConfig);
genConfig = lib.mkOption { # only for kind=snippet
type = lib.types.attrs; indentString = mkOption {
internal = true;
};
snippetNodeOpts = { config, ... }: {
options = with lib; with types; {
text = mkOption {
type = nullOr (either str (listOf str));
default = null;
};
insert = mkOption {
type = nullOr (either number (submodule ({ ... }: {
options = {
inherit jump;
text = mkOption {
type = nullOr (either str (listOf str));
default = null;
description = ''
A single string for just one line, a list with entries for multiple lines.
This text will be SELECTed when the `insertNode` is jumped into.
`:help luasnip-insertnode`
'';
};
};
})));
default = null;
};
choice = mkOption {
type = nullOr (submodule ({ ... }: {
options = { inherit jump nodes; };
}));
default = null;
};
snippet = mkOption {
type = nullOr (submodule ({ ... }: {
options = {
inherit jump nodes;
indent = mkOption {
type = types.str; type = types.str;
default = ""; default = "";
}; };
};
}));
default = null;
};
inherit genConfig; inherit genConfig;
}; };
config = { config =
genConfig =
let let
ins = config.insert; inherit (config) kind text jump nodes;
ch = config.choice;
sn = config.snippet; unwrapJump = index: if index == null then LuaNil else index;
in in
if config.text != null then call1 "t" config.text {
else if ins != null then kind = lib.mkDefault (
if builtins.isInt ins then call "i" [ ins ] if nodes != [ ] then "snippet"
else call "i" [ (unwrapJump ins.jump) ins.text ] else if jump != null then "insert"
else if ch != null then else "text"
call "c" [ (unwrapJump ch.jump) (unwrapNodes ch.nodes) ] );
else if sn != null then
if sn.indent != "" then genConfig =
call "isn" [ (unwrapJump sn.jump) (unwrapNodes sn.nodes) sn.indent ] if kind == "text" then
else call "sn" [ (unwrapJump sn.jump) (unwrapNodes sn.nodes) ] 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 nodes) ]
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; else null;
}; };
}; };
@ -106,7 +86,7 @@ let
''; '';
}; };
nodes = nodes // { default = [ ]; }; inherit nodes;
inherit genConfig; inherit genConfig;
}; };