utility/surround: vendor keybindings for surround

Adds an opioniated set of keybindings for nvim-surround that avoids
conflicts with nvim-leap by default and an option to disable those.
This commit is contained in:
Kalle Jepsen 2023-10-17 12:47:44 +02:00
parent 570eb32a5c
commit 997a345cda
2 changed files with 52 additions and 23 deletions

View file

@ -6,7 +6,7 @@
with lib; with lib;
with builtins; let with builtins; let
cfg = config.vim.utility.surround; cfg = config.vim.utility.surround;
self = import ./surround.nix {inherit lib;}; self = import ./surround.nix {inherit lib config;};
mappingDefinitions = self.options.vim.utility.surround.mappings; mappingDefinitions = self.options.vim.utility.surround.mappings;
mappings = addDescriptionsToMappings cfg.mappings mappingDefinitions; mappings = addDescriptionsToMappings cfg.mappings mappingDefinitions;
in { in {
@ -22,20 +22,20 @@ in {
maps = { maps = {
insert = mkMerge [ insert = mkMerge [
(mkSetBinding mappings.insert "<Plug>(nvim-surround-insert)") (mkIf (mappings.insert != null) (mkSetBinding mappings.insert "<Plug>(nvim-surround-insert)"))
(mkSetBinding mappings.insertLine "<Plug>(nvim-surround-insert-line)") (mkIf (mappings.insertLine != null) (mkSetBinding mappings.insertLine "<Plug>(nvim-surround-insert-line)"))
]; ];
normal = mkMerge [ normal = mkMerge [
(mkSetBinding mappings.normal "<Plug>(nvim-surround-normal)") (mkIf (mappings.normal != null) (mkSetBinding mappings.normal "<Plug>(nvim-surround-normal)"))
(mkSetBinding mappings.normalCur "<Plug>(nvim-surround-normal-cur)") (mkIf (mappings.normalCur != null) (mkSetBinding mappings.normalCur "<Plug>(nvim-surround-normal-cur)"))
(mkSetBinding mappings.normalLine "<Plug>(nvim-surround-normal-line)") (mkIf (mappings.normalLine != null) (mkSetBinding mappings.normalLine "<Plug>(nvim-surround-normal-line)"))
(mkSetBinding mappings.normalCurLine "<Plug>(nvim-surround-normal-cur-line)") (mkIf (mappings.normalCurLine != null) (mkSetBinding mappings.normalCurLine "<Plug>(nvim-surround-normal-cur-line)"))
(mkSetBinding mappings.delete "<Plug>(nvim-surround-delete)") (mkIf (mappings.delete != null) (mkSetBinding mappings.delete "<Plug>(nvim-surround-delete)"))
(mkSetBinding mappings.change "<Plug>(nvim-surround-change)") (mkIf (mappings.change != null) (mkSetBinding mappings.change "<Plug>(nvim-surround-change)"))
]; ];
visualOnly = mkMerge [ visualOnly = mkMerge [
(mkSetBinding mappings.visual "<Plug>(nvim-surround-visual)") (mkIf (mappings.visual != null) (mkSetBinding mappings.visual "<Plug>(nvim-surround-visual)"))
(mkSetBinding mappings.visualLine "<Plug>(nvim-surround-visual-line)") (mkIf (mappings.visualLine != null) (mkSetBinding mappings.visualLine "<Plug>(nvim-surround-visual-line)"))
]; ];
}; };
}; };

View file

@ -1,59 +1,88 @@
{lib, ...}: {
lib,
config,
...
}:
with lib; with lib;
with builtins; { with builtins; {
options.vim.utility.surround = { options.vim.utility.surround = {
enable = mkEnableOption "nvim-surround: add/change/delete surrounding delimiter pairs with ease"; enable = mkOption {
type = types.bool;
default = false;
description = "nvim-surround: add/change/delete surrounding delimiter pairs with ease. Note that the default mappings deviate from upstreeam to avoid conflicts with nvim-leap.";
};
useVendoredKeybindings = mkOption {
type = types.bool;
default = true;
description = "Use alternative set of keybindings that avoids conflicts with other popular plugins, e.g. nvim-leap";
};
mappings = { mappings = {
insert = mkOption { insert = mkOption {
type = types.nullOr types.str; type = types.nullOr types.str;
default = "<C-g>s"; default = "<C-g>z";
description = "Add surround character around the cursor"; description = "Add surround character around the cursor";
}; };
insertLine = mkOption { insertLine = mkOption {
type = types.nullOr types.str; type = types.nullOr types.str;
default = "<C-g>S"; default = "<C-g>Z";
description = "Add surround character around the cursor on new lines"; description = "Add surround character around the cursor on new lines";
}; };
normal = mkOption { normal = mkOption {
type = types.nullOr types.str; type = types.nullOr types.str;
default = "ys"; default = "gz";
description = "Surround motion with character"; description = "Surround motion with character";
}; };
normalCur = mkOption { normalCur = mkOption {
type = types.nullOr types.str; type = types.nullOr types.str;
default = "yss"; default = "gZ";
description = "Surround motion with character on new lines"; description = "Surround motion with character on new lines";
}; };
normalLine = mkOption { normalLine = mkOption {
type = types.nullOr types.str; type = types.nullOr types.str;
default = "yS"; default = "gzz";
description = "Surround line with character"; description = "Surround line with character";
}; };
normalCurLine = mkOption { normalCurLine = mkOption {
type = types.nullOr types.str; type = types.nullOr types.str;
default = "ySS"; default = "gZZ";
description = "Surround line with character on new lines"; description = "Surround line with character on new lines";
}; };
visual = mkOption { visual = mkOption {
type = types.nullOr types.str; type = types.nullOr types.str;
default = "S"; default = "gz";
description = "Surround selection with character"; description = "Surround selection with character";
}; };
visualLine = mkOption { visualLine = mkOption {
type = types.nullOr types.str; type = types.nullOr types.str;
default = "gS"; default = "gZ";
description = "Surround selection with character on new lines"; description = "Surround selection with character on new lines";
}; };
delete = mkOption { delete = mkOption {
type = types.nullOr types.str; type = types.nullOr types.str;
default = "ds"; default = "gzd";
description = "Delete surrounding character"; description = "Delete surrounding character";
}; };
change = mkOption { change = mkOption {
type = types.nullOr types.str; type = types.nullOr types.str;
default = "cs"; default = "gzr";
description = "Change surrounding character"; description = "Change surrounding character";
}; };
}; };
}; };
config.vim.utility.surround = let
cfg = config.vim.utility.surround;
in {
mappings = mkIf (! cfg.useVendoredKeybindings) (mkDefault {
insert = null;
insertLine = null;
normal = null;
normalCur = null;
normalLine = null;
normalCurLine = null;
visual = null;
visualLine = null;
delete = null;
change = null;
});
};
} }