lib: add useful utils for mods

This commit is contained in:
Dmitriy Pleshevskiy 2024-05-08 01:30:47 +03:00
parent 4a7c1b3d97
commit fdeca99cb8
Signed by: pleshevskiy
GPG key ID: 17041163DA10A9A2
4 changed files with 78 additions and 76 deletions

View file

@ -2,7 +2,7 @@
let let
inherit (builtins) listToAttrs; inherit (builtins) listToAttrs;
inherit (lib) nameValuePair toLower; inherit (lib) nameValuePair;
wrap = val: "<${val}>"; wrap = val: "<${val}>";
@ -12,40 +12,40 @@ let
meta = key: wrap "M-${key}"; # alt-key or meta-key *META* *ALT* *<M-* meta = key: wrap "M-${key}"; # alt-key or meta-key *META* *ALT* *<M-*
super = key: wrap "D-${key}"; # command-key or "super" key *<D-* super = key: wrap "D-${key}"; # command-key or "super" key *<D-*
keys = listToAttrs (map (v: nameValuePair v (wrap (toLower v))) ( keys = listToAttrs (map (v: nameValuePair v (wrap v)) (
[ [
"leader" "Leader"
"localLeader" "LocalLeader"
"cmd" "Cmd"
"plug" "Plug"
"mouseMove" "MouseMove"
"nul" "Nul"
"bs" "Bs"
"tab" "Tab"
"nl" "Nl"
"cr" "Cr"
"esc" "Esc"
"space" "Space"
"lt" "Lt"
"bslash" "Bslash"
"bar" "Bar"
"del" "Del"
"csi" "Csi"
"eol" "Eol"
"ignore" "Ignore"
"nop" "Nop"
"up" "Up"
"down" "Down"
"left" "Left"
"right" "Right"
"help" "Help"
"undo" "Undo"
"insert" "Insert"
"home" "Home"
"end" "End"
"pageUp" "PageUp"
"pageDown" "PageDown"
"kUp" "kUp"
"kDown" "kDown"
"kLeft" "kLeft"
@ -74,4 +74,8 @@ in
return = keys.cr; return = keys.cr;
enter = keys.cr; enter = keys.cr;
backslash = keys.bslash; backslash = keys.bslash;
cmd = expr: keys.Cmd + expr + keys.Cr;
leader = expr: keys.Leader + expr;
localLeader = expr: keys.LocalLeader + expr;
} }

View file

@ -5,19 +5,19 @@ let
cfg = config.input; cfg = config.input;
disableKeymaps = mode: lhss: lib.flip map lhss (lhs: { inherit mode lhs; rhs = mod.nop; }); disableKeymaps = mode: lhss: lib.flip map lhss (lhs: { inherit mode lhs; rhs = mod.Nop; });
in in
{ {
options.input = with lib; { options.input = with lib; {
leader = mkOption { leader = mkOption {
type = types.str; type = types.str;
default = ''\''; default = ''\'';
apply = x: assert (builtins.stringLength x == 1 || abort "${mod.leader} `${x}` is longer than one character is not allowed"); x; apply = x: assert (builtins.stringLength x == 1 || abort "${mod.Leader} `${x}` is longer than one character is not allowed"); x;
}; };
localLeader = mkOption { localLeader = mkOption {
type = types.str; type = types.str;
default = ''\''; default = ''\'';
apply = x: assert (builtins.stringLength x == 1 || abort "${mod.localLeader} `${x}` is longer than one character is not allowed"); x; apply = x: assert (builtins.stringLength x == 1 || abort "${mod.LocalLeader} `${x}` is longer than one character is not allowed"); x;
}; };
exMode.enable = mkEnableOption "Ex mode"; exMode.enable = mkEnableOption "Ex mode";
@ -52,9 +52,9 @@ in
lib.optionals (!cfg.exMode.enable) (disableKeymaps "n" [ "Q" "gQ" ]) lib.optionals (!cfg.exMode.enable) (disableKeymaps "n" [ "Q" "gQ" ])
# Disable arrow keys # Disable arrow keys
++ lib.optionals cfg.arrowKeys.disable ++ lib.optionals cfg.arrowKeys.disable
(disableKeymaps cfg.arrowKeys.mode [ mod.up mod.down mod.left mod.right ]) (disableKeymaps cfg.arrowKeys.mode [ mod.Up mod.Down mod.Left mod.Right ])
# Disable PageUp / PageDown # Disable PageUp / PageDown
++ lib.optionals cfg.pageButtons.disable ++ lib.optionals cfg.pageButtons.disable
(disableKeymaps cfg.pageButtons.mode [ mod.pageUp mod.pageDown ]); (disableKeymaps cfg.pageButtons.mode [ mod.PageUp mod.PageDown ]);
}; };
} }

View file

@ -1,7 +1,7 @@
{ config, lib, ... }: { config, lib, ... }:
let let
inherit (lib.mod) leader cmd cr ctrl; inherit (lib.mod) leader cmd ctrl;
in in
{ {
vim.opt = { vim.opt = {
@ -90,14 +90,14 @@ in
++ lib.optionals config.plugins.navigation.nvim-tree.enable [ ++ lib.optionals config.plugins.navigation.nvim-tree.enable [
{ {
mode = "n"; mode = "n";
lhs = "${leader}nt"; lhs = leader "nt";
rhs = "${cmd}NvimTreeToggle${cr}"; rhs = cmd "NvimTreeToggle";
desc = "Open/Close file tree"; desc = "Open/Close file tree";
} }
{ {
mode = "n"; mode = "n";
lhs = "${leader}nf"; lhs = leader "nf";
rhs = "${cmd}NvimTreeFindFile${cr}"; rhs = cmd "NvimTreeFindFile";
desc = "Open file tree and find current file"; desc = "Open file tree and find current file";
} }
]; ];

View file

@ -2,7 +2,7 @@
with lib.nix2lua; with lib.nix2lua;
let let
inherit (lib.mod) leader localLeader space cr cmd ctrl; inherit (lib.mod) Space cmd ctrl leader localLeader;
gs = config.plugin.gitsigns-nvim.varName; gs = config.plugin.gitsigns-nvim.varName;
hop = config.plugin.hop-nvim.varName; hop = config.plugin.hop-nvim.varName;
in in
@ -34,72 +34,70 @@ in
(call1 "${gs}.nav_hunk" "prev") (call1 "${gs}.nav_hunk" "prev")
); );
} }
{ mode = "n"; lhs = "${localLeader}gs"; rhs = raw "${gs}.stage_hunk"; } { mode = "n"; lhs = localLeader "gs"; rhs = raw "${gs}.stage_hunk"; }
{ mode = "n"; lhs = "${localLeader}gr"; rhs = raw "${gs}.reset_hunk"; } { mode = "n"; lhs = localLeader "gr"; rhs = raw "${gs}.reset_hunk"; }
{ mode = "n"; lhs = "${localLeader}gu"; rhs = raw "${gs}.undo_stage_hunk"; } { mode = "n"; lhs = localLeader "gu"; rhs = raw "${gs}.undo_stage_hunk"; }
{ mode = "n"; lhs = "${localLeader}gp"; rhs = raw "${gs}.preview_hunk"; } { mode = "n"; lhs = localLeader "gp"; rhs = raw "${gs}.preview_hunk"; }
{ mode = "n"; lhs = "${localLeader}gb"; rhs = lambda0 (call "${gs}.blame_line" { full = true; }); } { mode = "n"; lhs = localLeader "gb"; rhs = lambda0 (call "${gs}.blame_line" { full = true; }); }
{ mode = "n"; lhs = "${localLeader}gd"; rhs = raw "${gs}.diffthis"; } { mode = "n"; lhs = localLeader "gd"; rhs = raw "${gs}.diffthis"; }
{ mode = "n"; lhs = "${localLeader}gD"; rhs = lambda0 (call "${gs}.diffthis" "~"); } { mode = "n"; lhs = localLeader "gD"; rhs = lambda0 (call "${gs}.diffthis" "~"); }
{ mode = "n"; lhs = "${localLeader}gtb"; rhs = raw "${gs}.toggle_current_line_blame"; } { mode = "n"; lhs = localLeader "gtb"; rhs = raw "${gs}.toggle_current_line_blame"; }
{ mode = "n"; lhs = "${localLeader}gtd"; rhs = raw "${gs}.toggle_deleted"; } { mode = "n"; lhs = localLeader "gtd"; rhs = raw "${gs}.toggle_deleted"; }
]; ];
plugins.navigation.hop-nvim.keymap.set = { after, before }: [ plugins.navigation.hop-nvim.keymap.set = { after, before }: [
{ lhs = "${leader}hc"; rhs = "${cmd}HopChar1${cr}"; } { lhs = leader "hc"; rhs = cmd "HopChar1"; }
{ {
lhs = "${leader}hf"; lhs = leader "hf";
rhs = lambda0 (call "${hop}.hint_char1" { rhs = lambda0 (call "${hop}.hint_char1" {
direction = after; direction = after;
current_line_only = true; current_line_only = true;
}); });
} }
{ {
lhs = "${leader}hF"; lhs = leader "hF";
rhs = lambda0 (call "${hop}.hint_char1" { rhs = lambda0 (call "${hop}.hint_char1" {
direction = before; direction = before;
current_line_only = true; current_line_only = true;
}); });
} }
{ {
lhs = "${leader}hbc"; lhs = leader "hbc";
rhs = lambda0 (call "${hop}.hint_char1" { direction = after; }); rhs = lambda0 (call "${hop}.hint_char1" { direction = after; });
} }
{ {
lhs = "${leader}htc"; lhs = leader "htc";
rhs = lambda0 (call "${hop}.hint_char1" { direction = before; }); rhs = lambda0 (call "${hop}.hint_char1" { direction = before; });
} }
{ lhs = "${leader}hw"; rhs = "${cmd}HopWord${cr}"; } { lhs = leader "hw"; rhs = cmd "HopWord"; }
{ {
lhs = "${leader}hbw"; lhs = leader "hbw";
rhs = lambda0 (call "${hop}.hint_words" { direction = after; }); rhs = lambda0 (call "${hop}.hint_words" { direction = after; });
} }
{ {
lhs = "${leader}htw"; lhs = leader "htw";
rhs = lambda0 (call "${hop}.hint_words" { direction = before; }); rhs = lambda0 (call "${hop}.hint_words" { direction = before; });
} }
{ lhs = "${leader}hp"; rhs = "${cmd}HopPattern${cr}"; } { lhs = leader "hp"; rhs = cmd "HopPattern"; }
{ {
lhs = "${leader}hbp"; lhs = leader "hbp";
rhs = lambda0 (call "${hop}.hint_patterns" { direction = after; }); rhs = lambda0 (call "${hop}.hint_patterns" { direction = after; });
} }
{ {
lhs = "${leader}htp"; lhs = leader "htp";
rhs = lambda0 (call "${hop}.hint_patterns" { direction = before; }); rhs = lambda0 (call "${hop}.hint_patterns" { direction = before; });
} }
{ {
lhs = "${leader}hbv"; lhs = leader "hbv";
rhs = lambda0 (call "${hop}.hint_vertical" { direction = after; }); rhs = lambda0 (call "${hop}.hint_vertical" { direction = after; });
} }
{ {
lhs = "${leader}htv"; lhs = leader "htv";
rhs = lambda0 (call "${hop}.hint_vertical" { direction = before; }); rhs = lambda0 (call "${hop}.hint_vertical" { direction = before; });
} }
]; ];
plugins.language-server.lspconfig.keymap.set = [ plugins.language-server.lspconfig.keymap.set = [
{ mode = "n"; lhs = "gD"; rhs = raw "vim.lsp.buf.declaration"; } { mode = "n"; lhs = "gD"; rhs = raw "vim.lsp.buf.declaration"; }
{ mode = "n"; lhs = "gd"; rhs = raw "vim.lsp.buf.definition"; } { mode = "n"; lhs = "gd"; rhs = raw "vim.lsp.buf.definition"; }
@ -108,24 +106,24 @@ in
{ mode = "n"; lhs = "gr"; rhs = raw "vim.lsp.buf.references"; } { mode = "n"; lhs = "gr"; rhs = raw "vim.lsp.buf.references"; }
{ mode = "n"; lhs = "gy"; rhs = raw "vim.lsp.buf.type_definition"; } { mode = "n"; lhs = "gy"; rhs = raw "vim.lsp.buf.type_definition"; }
{ mode = "n"; lhs = ctrl "k"; rhs = raw "vim.lsp.buf.signature_help"; } { mode = "n"; lhs = ctrl "k"; rhs = raw "vim.lsp.buf.signature_help"; }
{ mode = "n"; lhs = "${localLeader}n"; rhs = raw "vim.lsp.buf.rename"; } { mode = "n"; lhs = localLeader "n"; rhs = raw "vim.lsp.buf.rename"; }
{ mode = [ "n" "v" ]; lhs = "${localLeader}a"; rhs = raw "vim.lsp.buf.code_action"; } { mode = [ "n" "v" ]; lhs = localLeader "a"; rhs = raw "vim.lsp.buf.code_action"; }
]; ];
vim.keymap.set = lib.optionals config.plugins.language-server.lspconfig.enable [ vim.keymap.set = lib.optionals config.plugins.language-server.lspconfig.enable [
{ mode = "n"; lhs = "${space}e"; rhs = raw "vim.diagnostic.open_float"; } { mode = "n"; lhs = "${Space}e"; rhs = raw "vim.diagnostic.open_float"; }
{ mode = "n"; lhs = "[d"; rhs = raw "vim.diagnostic.goto_prev"; } { mode = "n"; lhs = "[d"; rhs = raw "vim.diagnostic.goto_prev"; }
{ mode = "n"; lhs = "]d"; rhs = raw "vim.diagnostic.goto_next"; } { mode = "n"; lhs = "]d"; rhs = raw "vim.diagnostic.goto_next"; }
{ mode = "n"; lhs = "${space}q"; rhs = raw "vim.diagnostic.setloclist"; } { mode = "n"; lhs = "${Space}q"; rhs = raw "vim.diagnostic.setloclist"; }
] ++ lib.optionals config.plugins.navigation.telescope.enable ( ] ++ lib.optionals config.plugins.navigation.telescope.enable (
[ [
{ mode = "n"; lhs = "${leader}ff"; rhs = "${cmd}Telescope find_files${cr}"; } { mode = "n"; lhs = leader "ff"; rhs = cmd "Telescope find_files"; }
{ mode = "n"; lhs = "${leader}fb"; rhs = "${cmd}Telescope buffers${cr}"; } { mode = "n"; lhs = leader "fb"; rhs = cmd "Telescope buffers"; }
{ mode = "n"; lhs = "${leader}fh"; rhs = "${cmd}Telescope help_tags${cr}"; } { mode = "n"; lhs = leader "fh"; rhs = cmd "Telescope help_tags"; }
] ++ lib.optionals (config.plugins.navigation.telescope.extensions ? telescope-live-grep-args-nvim) [ ] ++ lib.optionals (config.plugins.navigation.telescope.extensions ? telescope-live-grep-args-nvim) [
{ {
mode = "n"; mode = "n";
lhs = "${leader}fg"; lhs = leader "fg";
rhs = lambda0 (call0 "telescope.extensions.live_grep_args.live_grep_args"); rhs = lambda0 (call0 "telescope.extensions.live_grep_args.live_grep_args");
} }
] ]