From 546e6ea68a6ef9c39a97ebfaaa9c6029a2bd5569 Mon Sep 17 00:00:00 2001 From: Gaetan Lepage Date: Sun, 24 Mar 2024 12:07:29 +0100 Subject: [PATCH] plugins/comment: swith to mkNeovimPlugin + rename --- plugins/default.nix | 2 +- plugins/utils/comment-nvim.nix | 107 ------------------------ plugins/utils/comment.nix | 145 +++++++++++++++++++++++++++++++++ 3 files changed, 146 insertions(+), 108 deletions(-) delete mode 100644 plugins/utils/comment-nvim.nix create mode 100644 plugins/utils/comment.nix diff --git a/plugins/default.nix b/plugins/default.nix index ea41a5f8..6c685f0f 100644 --- a/plugins/default.nix +++ b/plugins/default.nix @@ -123,7 +123,7 @@ ./utils/baleia.nix ./utils/better-escape.nix ./utils/clipboard-image.nix - ./utils/comment-nvim.nix + ./utils/comment.nix ./utils/commentary.nix ./utils/conjure.nix ./utils/coverage.nix diff --git a/plugins/utils/comment-nvim.nix b/plugins/utils/comment-nvim.nix deleted file mode 100644 index f14fd5f6..00000000 --- a/plugins/utils/comment-nvim.nix +++ /dev/null @@ -1,107 +0,0 @@ -{ - lib, - helpers, - config, - pkgs, - ... -}: -with lib; let - cfg = config.plugins.comment-nvim; -in { - options = { - plugins.comment-nvim = { - enable = mkEnableOption "comment-nvim"; - - package = helpers.mkPackageOption "comment-nvim" pkgs.vimPlugins.comment-nvim; - - padding = mkOption { - type = types.nullOr types.bool; - description = "Add a space b/w comment and the line"; - default = null; - }; - sticky = mkOption { - type = types.nullOr types.bool; - description = "Whether the cursor should stay at its position"; - default = null; - }; - ignore = mkOption { - type = types.nullOr types.str; - description = "Lines to be ignored while comment/uncomment"; - default = null; - }; - preHook = helpers.mkNullOrLuaFn "Lua function called before (un)comment."; - postHook = helpers.mkNullOrLuaFn "Lua function called after (un)comment."; - toggler = mkOption { - type = types.nullOr (types.submodule (_: { - options = { - line = mkOption { - type = types.str; - description = "line-comment keymap"; - default = "gcc"; - }; - block = mkOption { - type = types.str; - description = "block-comment keymap"; - default = "gbc"; - }; - }; - })); - description = "LHS of toggle mappings in NORMAL + VISUAL mode"; - default = null; - }; - opleader = mkOption { - type = types.nullOr (types.submodule (_: { - options = { - line = mkOption { - type = types.str; - description = "line-comment keymap"; - default = "gc"; - }; - block = mkOption { - type = types.str; - description = "block-comment keymap"; - default = "gb"; - }; - }; - })); - description = "LHS of operator-pending mappings in NORMAL + VISUAL mode"; - default = null; - }; - mappings = mkOption { - type = types.nullOr (types.submodule (_: { - options = { - basic = mkOption { - type = types.bool; - description = "operator-pending mapping. Includes 'gcc', 'gcb', 'gc[count]{motion}' and 'gb[count]{motion}'"; - default = true; - }; - extra = mkOption { - type = types.bool; - description = "extra mapping. Includes 'gco', 'gc0', 'gcA'"; - default = true; - }; - extended = mkOption { - type = types.bool; - description = "extended mapping. Includes 'g>', 'g<', 'g>[count]{motion}' and 'g<[count]{motion}'"; - default = false; - }; - }; - })); - description = "Create basic (operator-pending) and extended mappings for NORMAL + VISUAL mode"; - default = null; - }; - }; - }; - - config = let - setupOptions = { - inherit (cfg) padding sticky ignore toggler opleader mappings; - pre_hook = cfg.preHook; - post_hook = cfg.postHook; - }; - in - mkIf cfg.enable { - extraPlugins = [cfg.package]; - extraConfigLua = ''require("Comment").setup${helpers.toLuaObject setupOptions}''; - }; -} diff --git a/plugins/utils/comment.nix b/plugins/utils/comment.nix new file mode 100644 index 00000000..245f88ce --- /dev/null +++ b/plugins/utils/comment.nix @@ -0,0 +1,145 @@ +{ + lib, + helpers, + config, + pkgs, + ... +}: +with lib; + helpers.neovim-plugin.mkNeovimPlugin config { + name = "comment"; + originalName = "Comment.nvim"; + luaName = "Comment"; + defaultPackage = pkgs.vimPlugins.comment-nvim; + + maintainers = [maintainers.GaetanLepage]; + + # TODO introduced 2024-03-24: remove 2024-05-24 + imports = let + oldPluginPath = ["plugins" "comment-nvim"]; + newPluginPath = ["plugins" "comment"]; + settingsPath = newPluginPath ++ ["settings"]; + in [ + (mkRenamedOptionModule (oldPluginPath ++ ["enable"]) (newPluginPath ++ ["enable"])) + (mkRenamedOptionModule (oldPluginPath ++ ["package"]) (newPluginPath ++ ["package"])) + (mkRenamedOptionModule (oldPluginPath ++ ["padding"]) (settingsPath ++ ["padding"])) + (mkRenamedOptionModule (oldPluginPath ++ ["sticky"]) (settingsPath ++ ["sticky"])) + (mkRenamedOptionModule (oldPluginPath ++ ["ignore"]) (settingsPath ++ ["ignore"])) + (mkRenamedOptionModule (oldPluginPath ++ ["toggler" "line"]) (settingsPath ++ ["toggler" "line"])) + (mkRenamedOptionModule (oldPluginPath ++ ["toggler" "block"]) (settingsPath ++ ["toggler" "block"])) + (mkRenamedOptionModule (oldPluginPath ++ ["opleader" "line"]) (settingsPath ++ ["opleader" "line"])) + (mkRenamedOptionModule (oldPluginPath ++ ["opleader" "block"]) (settingsPath ++ ["opleader" "block"])) + (mkRenamedOptionModule (oldPluginPath ++ ["mappings" "basic"]) (settingsPath ++ ["mappings" "basic"])) + (mkRenamedOptionModule (oldPluginPath ++ ["mappings" "extra"]) (settingsPath ++ ["mappings" "extra"])) + (mkRemovedOptionModule (oldPluginPath ++ ["mappings" "extended"]) "This option has been removed upstream.") + (mkRenamedOptionModule (oldPluginPath ++ ["preHook"]) (settingsPath ++ ["pre_hook"])) + (mkRenamedOptionModule (oldPluginPath ++ ["postHook"]) (settingsPath ++ ["post_hook"])) + ]; + + settingsOptions = { + padding = helpers.defaultNullOpts.mkBool true '' + Add a space b/w comment and the line. + ''; + + sticky = helpers.defaultNullOpts.mkBool true '' + Whether the cursor should stay at its position. + ''; + + ignore = helpers.mkNullOrStr '' + Lines to be ignored while (un)comment. + ''; + + toggler = { + line = helpers.defaultNullOpts.mkStr "gcc" '' + Line-comment toggle keymap in NORMAL mode. + ''; + + block = helpers.defaultNullOpts.mkStr "gbc" '' + Block-comment toggle keymap in NORMAL mode. + ''; + }; + + opleader = { + line = helpers.defaultNullOpts.mkStr "gc" '' + Line-comment operator-pending keymap in NORMAL and VISUAL mode. + ''; + + block = helpers.defaultNullOpts.mkStr "gb" '' + Block-comment operator-pending keymap in NORMAL and VISUAL mode. + ''; + }; + + extra = { + above = helpers.defaultNullOpts.mkStr "gcO" '' + Add comment on the line above. + ''; + + below = helpers.defaultNullOpts.mkStr "gco" '' + Add comment on the line below. + ''; + + eol = helpers.defaultNullOpts.mkStr "gcA" '' + Add comment at the end of line. + ''; + }; + + mappings = + helpers.defaultNullOpts.mkNullable + ( + with types; + either + (enum [false]) + (submodule { + options = { + basic = helpers.defaultNullOpts.mkBool true '' + Enable operator-pending mappings (`gcc`, `gbc`, `gc[count]{motion}`, `gb[count]{motion}`). + ''; + + extra = helpers.defaultNullOpts.mkBool true '' + Enable extra mappings (`gco`, `gcO`, `gcA`). + ''; + }; + }) + ) + '' + { + basic = true; + extra = true; + } + '' + '' + Enables keybindings. + NOTE: If given 'false', then the plugin won't create any mappings. + ''; + + pre_hook = helpers.mkNullOrLuaFn '' + Lua function called before (un)comment. + ''; + + post_hook = helpers.mkNullOrLuaFn '' + Lua function called after (un)comment. + ''; + }; + + settingsExample = { + ignore = "^const(.*)=(%s?)%((.*)%)(%s?)=>"; + toggler = { + line = "gcc"; + block = "gbc"; + }; + opleader = { + line = "gc"; + block = "gb"; + }; + pre_hook = "require('ts_context_commentstring.integrations.comment_nvim').create_pre_hook()"; + post_hook = '' + function(ctx) + if ctx.range.srow == ctx.range.erow then + -- do something with the current line + else + -- do something with lines range + end + end + ''; + }; + }