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,24 +4,13 @@ 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;
jump = lib.mkOption {
type = with lib.types; nullOr number;
default = null;
description = ''
This determines when this node will be jumped to.
`:help luasnip-insertnode`
`:help luasnip-basics-jump-index`
'';
};
unwrapJump = index: if index == null then LuaNil else index;
nodes = lib.mkOption { nodes = lib.mkOption {
type = with lib.types; listOf (submodule snippetNodeOpts); type = with lib.types; listOf (submodule snippetNodeOpts);
default = [ ];
}; };
unwrapNodes = map (n: n.genConfig); unwrapNodes = map (n: n.genConfig);
genConfig = lib.mkOption { genConfig = lib.mkOption {
type = lib.types.attrs; type = lib.types.attrs;
internal = true; internal = true;
@ -29,72 +18,63 @@ let
snippetNodeOpts = { config, ... }: { snippetNodeOpts = { config, ... }: {
options = with lib; with types; { options = with lib; with types; {
kind = mkOption {
type = enum [ "text" "insert" "choice" "snippet" ];
};
text = mkOption { text = mkOption {
type = nullOr (either str (listOf str)); type = nullOr (either str (listOf str));
default = null; default = null;
}; };
insert = mkOption { jump = lib.mkOption {
type = nullOr (either number (submodule ({ ... }: { type = with lib.types; nullOr number;
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; default = null;
description = ''
This determines when this node will be jumped to.
`:help luasnip-insertnode`
`:help luasnip-basics-jump-index`
'';
}; };
choice = mkOption { inherit nodes;
type = nullOr (submodule ({ ... }: {
options = { inherit jump nodes; };
}));
default = null;
};
snippet = mkOption { # only for kind=snippet
type = nullOr (submodule ({ ... }: { indentString = mkOption {
options = { type = types.str;
inherit jump nodes; default = "";
indent = mkOption {
type = types.str;
default = "";
};
};
}));
default = null;
}; };
inherit genConfig; inherit genConfig;
}; };
config = { config =
genConfig = let
let inherit (config) kind text jump nodes;
ins = config.insert;
ch = config.choice; unwrapJump = index: if index == null then LuaNil else index;
sn = config.snippet; in
in {
if config.text != null then call1 "t" config.text kind = lib.mkDefault (
else if ins != null then if nodes != [ ] then "snippet"
if builtins.isInt ins then call "i" [ ins ] else if jump != null then "insert"
else call "i" [ (unwrapJump ins.jump) ins.text ] else "text"
else if ch != null then );
call "c" [ (unwrapJump ch.jump) (unwrapNodes ch.nodes) ]
else if sn != null then genConfig =
if sn.indent != "" then if kind == "text" then
call "isn" [ (unwrapJump sn.jump) (unwrapNodes sn.nodes) sn.indent ] if text != null then call1 "t" text
else call "sn" [ (unwrapJump sn.jump) (unwrapNodes sn.nodes) ] else throw "luasnip textnode require a 'text' config"
else null; 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;
};
}; };
snippetOpts = { name, ... } @ sub: { snippetOpts = { name, ... } @ sub: {
@ -106,7 +86,7 @@ let
''; '';
}; };
nodes = nodes // { default = [ ]; }; inherit nodes;
inherit genConfig; inherit genConfig;
}; };