From bab56daddbe70141fb5fec85adb4cc0d61180b8d Mon Sep 17 00:00:00 2001 From: Haseeb Majid Date: Fri, 13 Oct 2023 22:47:44 +0100 Subject: [PATCH] plugins/better-escape: init + tests (#626) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * adding better escape * fixing formatting * fixing tests * fixing comments from pr * fixing comments from pr * Update tests/test-sources/plugins/utils/better-escape.nix Co-authored-by: Gaétan Lepage <33058747+GaetanLepage@users.noreply.github.com> * Update plugins/utils/better-escape.nix Co-authored-by: Gaétan Lepage <33058747+GaetanLepage@users.noreply.github.com> * Update plugins/utils/better-escape.nix Co-authored-by: Gaétan Lepage <33058747+GaetanLepage@users.noreply.github.com> * Update plugins/utils/better-escape.nix Co-authored-by: Gaétan Lepage <33058747+GaetanLepage@users.noreply.github.com> * Update plugins/utils/better-escape.nix Co-authored-by: Gaétan Lepage <33058747+GaetanLepage@users.noreply.github.com> * Update plugins/utils/better-escape.nix Co-authored-by: Gaétan Lepage <33058747+GaetanLepage@users.noreply.github.com> * fixing comments from pr --------- Co-authored-by: Haseeb Majid Co-authored-by: Gaétan Lepage <33058747+GaetanLepage@users.noreply.github.com> --- plugins/default.nix | 1 + plugins/utils/better-escape.nix | 78 +++++++++++++++++++ .../plugins/utils/better-escape.nix | 16 ++++ 3 files changed, 95 insertions(+) create mode 100644 plugins/utils/better-escape.nix create mode 100644 tests/test-sources/plugins/utils/better-escape.nix diff --git a/plugins/default.nix b/plugins/default.nix index 5ba06232..43f5f593 100644 --- a/plugins/default.nix +++ b/plugins/default.nix @@ -91,6 +91,7 @@ ./utils/alpha.nix ./utils/auto-save.nix ./utils/auto-session.nix + ./utils/better-escape.nix ./utils/comment-nvim.nix ./utils/commentary.nix ./utils/conjure.nix diff --git a/plugins/utils/better-escape.nix b/plugins/utils/better-escape.nix new file mode 100644 index 00000000..bd100ee9 --- /dev/null +++ b/plugins/utils/better-escape.nix @@ -0,0 +1,78 @@ +{ + pkgs, + config, + lib, + ... +}: +with lib; let + cfg = config.plugins.better-escape; + helpers = import ../helpers.nix {inherit lib;}; +in { + options.plugins.better-escape = + helpers.extraOptionsOptions + // { + enable = mkEnableOption "better-escape.nvim"; + + package = helpers.mkPackageOption "better-escape.nvim" pkgs.vimPlugins.better-escape-nvim; + + mapping = helpers.mkNullOrOption (with types; listOf str) '' + List of mappings to use to enter escape mode. + ''; + + timeout = + helpers.defaultNullOpts.mkNullable + ( + with types; + either ints.unsigned str + ) + "vim.o.timeoutlen" + '' + The time in which the keys must be hit in ms. + Uses the value of `vim.o.timeoutlen` (`options.timeoutlen` in nixvim) by default. + ''; + + clearEmptyLines = helpers.defaultNullOpts.mkBool false '' + Clear line after escaping if there is only whitespace. + ''; + + keys = + helpers.defaultNullOpts.mkNullable + ( + with types; + either str helpers.rawType + ) + "" + '' + Keys used for escaping, if it is a function will use the result everytime. + + Example (recommended): + + keys.__raw = \'\' + function() + return vim.api.nvim_win_get_cursor(0)[2] > 1 and 'l' or '' + end + \'\'; + ''; + }; + + config = let + setupOptions = with cfg; + { + inherit mapping; + timeout = + if isInt timeout + then helpers.mkRaw "timeout" + else timeout; + clear_empty_lines = clearEmptyLines; + inherit keys; + } + // cfg.extraOptions; + in + mkIf cfg.enable { + extraPlugins = [cfg.package]; + + extraConfigLua = '' + require('better_escape').setup(${helpers.toLuaObject setupOptions}) + ''; + }; +} diff --git a/tests/test-sources/plugins/utils/better-escape.nix b/tests/test-sources/plugins/utils/better-escape.nix new file mode 100644 index 00000000..3d861267 --- /dev/null +++ b/tests/test-sources/plugins/utils/better-escape.nix @@ -0,0 +1,16 @@ +{ + empty = { + plugins.better-escape.enable = true; + }; + + example = { + plugins.better-escape = { + enable = true; + + mapping = ["jj" "jk"]; + timeout = 150; + clearEmptyLines = false; + keys = ""; + }; + }; +}