From 59c23f28555e67e2c5dc083ef7fb8a282a34283a Mon Sep 17 00:00:00 2001 From: Kalle Jepsen Date: Mon, 16 Oct 2023 11:04:47 +0200 Subject: [PATCH 1/4] utility/surround: Add mappings for nvim-surround The default mappings for nvim-leap and nvim-surround conflict (i.e. nvim-surround uses `S` in visual mode). This change adds options to adapt the mappings for nvim-surround directly from the surround-module. --- modules/utility/surround/config.nix | 38 +++++++++++++++---- modules/utility/surround/surround.nix | 53 +++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 7 deletions(-) diff --git a/modules/utility/surround/config.nix b/modules/utility/surround/config.nix index bcd1f51..8a76c1f 100644 --- a/modules/utility/surround/config.nix +++ b/modules/utility/surround/config.nix @@ -6,14 +6,38 @@ with lib; with builtins; let cfg = config.vim.utility.surround; + self = import ./surround.nix {inherit lib;}; + mappingDefinitions = self.options.vim.utility.surround.mappings; + mappings = addDescriptionsToMappings cfg.mappings mappingDefinitions; in { - config = mkIf (cfg.enable) { - vim.startPlugins = [ - "nvim-surround" - ]; + config = mkIf cfg.enable { + vim = { + startPlugins = [ + "nvim-surround" + ]; - vim.luaConfigRC.surround = nvim.dag.entryAnywhere '' - require('nvim-surround').setup() - ''; + luaConfigRC.surround = nvim.dag.entryAnywhere '' + require('nvim-surround').setup() + ''; + + maps = { + insert = mkMerge [ + (mkSetBinding mappings.insert "(nvim-surround-insert)") + (mkSetBinding mappings.insertLine "(nvim-surround-insert-line)") + ]; + normal = mkMerge [ + (mkSetBinding mappings.normal "(nvim-surround-normal)") + (mkSetBinding mappings.normalCur "(nvim-surround-normal-cur)") + (mkSetBinding mappings.normalLine "(nvim-surround-normal-line)") + (mkSetBinding mappings.normalCurLine "(nvim-surround-normal-cur-line)") + (mkSetBinding mappings.delete "(nvim-surround-delete)") + (mkSetBinding mappings.change "(nvim-surround-change)") + ]; + visualOnly = mkMerge [ + (mkSetBinding mappings.visual "(nvim-surround-visual)") + (mkSetBinding mappings.visualLine "(nvim-surround-visual-line)") + ]; + }; + }; }; } diff --git a/modules/utility/surround/surround.nix b/modules/utility/surround/surround.nix index a29b19b..3d085e3 100644 --- a/modules/utility/surround/surround.nix +++ b/modules/utility/surround/surround.nix @@ -3,5 +3,58 @@ with lib; with builtins; { options.vim.utility.surround = { enable = mkEnableOption "nvim-surround: add/change/delete surrounding delimiter pairs with ease"; + mappings = { + insert = mkOption { + type = types.nullOr types.str; + default = "s"; + description = "Add surround character around the cursor"; + }; + insertLine = mkOption { + type = types.nullOr types.str; + default = "S"; + description = "Add surround character around the cursor on new lines"; + }; + normal = mkOption { + type = types.nullOr types.str; + default = "ys"; + description = "Surround motion with character"; + }; + normalCur = mkOption { + type = types.nullOr types.str; + default = "yss"; + description = "Surround motion with character on new lines"; + }; + normalLine = mkOption { + type = types.nullOr types.str; + default = "yS"; + description = "Surround line with character"; + }; + normalCurLine = mkOption { + type = types.nullOr types.str; + default = "ySS"; + description = "Surround line with character on new lines"; + }; + visual = mkOption { + type = types.nullOr types.str; + default = "S"; + description = "Surround selection with character"; + }; + visualLine = mkOption { + type = types.nullOr types.str; + default = "gS"; + description = "Surround selection with character on new lines"; + }; + delete = mkOption { + type = types.nullOr types.str; + default = "ds"; + description = "Delete surrounding character"; + }; + change = mkOption { + type = types.nullOr types.str; + default = "cs"; + description = "Change surrounding character"; + }; + + }; }; } From 6cec86a97fea5ca3e91f36d0976355515262d0c2 Mon Sep 17 00:00:00 2001 From: Kalle Jepsen Date: Mon, 16 Oct 2023 12:24:33 +0200 Subject: [PATCH 2/4] docs/release-notes: add new mappings for surround --- docs/release-notes/rl-0.5.adoc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/release-notes/rl-0.5.adoc b/docs/release-notes/rl-0.5.adoc index 2d02973..530e9ee 100644 --- a/docs/release-notes/rl-0.5.adoc +++ b/docs/release-notes/rl-0.5.adoc @@ -69,3 +69,7 @@ https://github.com/jacekpoz[jacekpoz]: * Updated clangd to 16 * Disabled `useSystemClipboard` by default + +https://github.com/ksonj[ksonj]: + +* Add support to change mappings to utility/surround From 570eb32a5ced49830fd5e37dbfd8dc3310b65f0c Mon Sep 17 00:00:00 2001 From: Kalle Jepsen Date: Mon, 16 Oct 2023 13:02:37 +0200 Subject: [PATCH 3/4] utility/surround: run format --- modules/utility/surround/surround.nix | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/utility/surround/surround.nix b/modules/utility/surround/surround.nix index 3d085e3..409b75d 100644 --- a/modules/utility/surround/surround.nix +++ b/modules/utility/surround/surround.nix @@ -54,7 +54,6 @@ with builtins; { default = "cs"; description = "Change surrounding character"; }; - }; }; } From 997a345cdaddda4095a4e8f98e42b3af6ccb0b23 Mon Sep 17 00:00:00 2001 From: Kalle Jepsen Date: Tue, 17 Oct 2023 12:47:44 +0200 Subject: [PATCH 4/4] 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. --- modules/utility/surround/config.nix | 22 +++++------ modules/utility/surround/surround.nix | 53 +++++++++++++++++++++------ 2 files changed, 52 insertions(+), 23 deletions(-) diff --git a/modules/utility/surround/config.nix b/modules/utility/surround/config.nix index 8a76c1f..aa7f7bd 100644 --- a/modules/utility/surround/config.nix +++ b/modules/utility/surround/config.nix @@ -6,7 +6,7 @@ with lib; with builtins; let 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; mappings = addDescriptionsToMappings cfg.mappings mappingDefinitions; in { @@ -22,20 +22,20 @@ in { maps = { insert = mkMerge [ - (mkSetBinding mappings.insert "(nvim-surround-insert)") - (mkSetBinding mappings.insertLine "(nvim-surround-insert-line)") + (mkIf (mappings.insert != null) (mkSetBinding mappings.insert "(nvim-surround-insert)")) + (mkIf (mappings.insertLine != null) (mkSetBinding mappings.insertLine "(nvim-surround-insert-line)")) ]; normal = mkMerge [ - (mkSetBinding mappings.normal "(nvim-surround-normal)") - (mkSetBinding mappings.normalCur "(nvim-surround-normal-cur)") - (mkSetBinding mappings.normalLine "(nvim-surround-normal-line)") - (mkSetBinding mappings.normalCurLine "(nvim-surround-normal-cur-line)") - (mkSetBinding mappings.delete "(nvim-surround-delete)") - (mkSetBinding mappings.change "(nvim-surround-change)") + (mkIf (mappings.normal != null) (mkSetBinding mappings.normal "(nvim-surround-normal)")) + (mkIf (mappings.normalCur != null) (mkSetBinding mappings.normalCur "(nvim-surround-normal-cur)")) + (mkIf (mappings.normalLine != null) (mkSetBinding mappings.normalLine "(nvim-surround-normal-line)")) + (mkIf (mappings.normalCurLine != null) (mkSetBinding mappings.normalCurLine "(nvim-surround-normal-cur-line)")) + (mkIf (mappings.delete != null) (mkSetBinding mappings.delete "(nvim-surround-delete)")) + (mkIf (mappings.change != null) (mkSetBinding mappings.change "(nvim-surround-change)")) ]; visualOnly = mkMerge [ - (mkSetBinding mappings.visual "(nvim-surround-visual)") - (mkSetBinding mappings.visualLine "(nvim-surround-visual-line)") + (mkIf (mappings.visual != null) (mkSetBinding mappings.visual "(nvim-surround-visual)")) + (mkIf (mappings.visualLine != null) (mkSetBinding mappings.visualLine "(nvim-surround-visual-line)")) ]; }; }; diff --git a/modules/utility/surround/surround.nix b/modules/utility/surround/surround.nix index 409b75d..405e3f2 100644 --- a/modules/utility/surround/surround.nix +++ b/modules/utility/surround/surround.nix @@ -1,59 +1,88 @@ -{lib, ...}: +{ + lib, + config, + ... +}: with lib; with builtins; { 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 = { insert = mkOption { type = types.nullOr types.str; - default = "s"; + default = "z"; description = "Add surround character around the cursor"; }; insertLine = mkOption { type = types.nullOr types.str; - default = "S"; + default = "Z"; description = "Add surround character around the cursor on new lines"; }; normal = mkOption { type = types.nullOr types.str; - default = "ys"; + default = "gz"; description = "Surround motion with character"; }; normalCur = mkOption { type = types.nullOr types.str; - default = "yss"; + default = "gZ"; description = "Surround motion with character on new lines"; }; normalLine = mkOption { type = types.nullOr types.str; - default = "yS"; + default = "gzz"; description = "Surround line with character"; }; normalCurLine = mkOption { type = types.nullOr types.str; - default = "ySS"; + default = "gZZ"; description = "Surround line with character on new lines"; }; visual = mkOption { type = types.nullOr types.str; - default = "S"; + default = "gz"; description = "Surround selection with character"; }; visualLine = mkOption { type = types.nullOr types.str; - default = "gS"; + default = "gZ"; description = "Surround selection with character on new lines"; }; delete = mkOption { type = types.nullOr types.str; - default = "ds"; + default = "gzd"; description = "Delete surrounding character"; }; change = mkOption { type = types.nullOr types.str; - default = "cs"; + default = "gzr"; 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; + }); + }; }