From 2b03933101012cc5830d5dd7167d4544902a51b1 Mon Sep 17 00:00:00 2001 From: Austin Horstman Date: Sat, 1 Feb 2025 13:37:29 -0600 Subject: [PATCH] plugins/clipboard-image: migrate to mkneovimplugin --- plugins/by-name/clipboard-image/default.nix | 217 +++++++----------- .../by-name/clipboard-image/deprecations.nix | 42 ++++ .../by-name/clipboard-image/default.nix | 22 +- 3 files changed, 140 insertions(+), 141 deletions(-) create mode 100644 plugins/by-name/clipboard-image/deprecations.nix diff --git a/plugins/by-name/clipboard-image/default.nix b/plugins/by-name/clipboard-image/default.nix index 0bebde6a..6f3c990c 100644 --- a/plugins/by-name/clipboard-image/default.nix +++ b/plugins/by-name/clipboard-image/default.nix @@ -1,160 +1,117 @@ { lib, - helpers, - config, pkgs, ... }: -with lib; let - cfg = config.plugins.clipboard-image; - - pluginOptions = { - imgDir = - helpers.defaultNullOpts.mkNullable - ( - with lib.types; - oneOf [ - str - (listOf str) - rawLua - ] - ) - "img" - '' - Dir name where the image will be pasted to. - - Note: If you want to create nested dir, it is better to use table since windows and unix - have different path separator. - ''; - - imgDirTxt = helpers.defaultNullOpts.mkNullable ( - with lib.types; - oneOf [ - str - (listOf str) - rawLua - ] - ) "img" "Dir that will be inserted into text/buffer."; - - imgName = helpers.defaultNullOpts.mkStr { - __raw = "function() return os.date('%Y-%m-%d-%H-%M-%S') end"; - } "Image's name."; - - imgHandler = helpers.defaultNullOpts.mkLuaFn "function(img) end" '' - Function that will handle image after pasted. - - Note: `img` is a table that contain pasted image's `{name}` and `{path}`. - ''; - - affix = helpers.mkNullOrStr '' - String that sandwiched the image's path. - - Default: - - `default`: `"{img_path}"` - - `markdown`: `"![]({img_path})"` - - Note: - Affix can be multi lines, like this: - - ```nix - # You can use line break escape sequence - affix = "<\n %s\n>"; - ``` - - ```nix - # Or lua's double square brackets - affix.__raw = \'\' - [[< - %s - >]] - \'\' - ``` - ''; - }; - - processPluginOptions = - opts: with opts; { - img_dir = imgDir; - img_dir_txt = imgDirTxt; - img_name = imgName; - img_handler = imgHandler; - inherit affix; - }; + inherit (lib.nixvim) defaultNullOpts; + inherit (lib) types; in -{ - meta.maintainers = [ maintainers.GaetanLepage ]; +lib.nixvim.plugins.mkNeovimPlugin { + name = "clipboard-image"; + packPathName = "clipboard-image.nvim"; + package = "clipboard-image-nvim"; - options.plugins.clipboard-image = lib.nixvim.plugins.neovim.extraOptionsOptions // { - enable = mkEnableOption "clipboard-image.nvim"; + maintainers = [ lib.maintainers.GaetanLepage ]; - package = lib.mkPackageOption pkgs "clipboard-image.nvim" { - default = [ - "vimPlugins" - "clipboard-image-nvim" - ]; - }; + description = '' + Plugin to paste images from clipboard into Neovim. + ''; - clipboardPackage = lib.mkPackageOption pkgs "clipboard privider" { + extraOptions = { + clipboardPackage = lib.mkPackageOption pkgs "clipboard provider" { nullable = true; default = null; example = [ "wl-clipboard" ]; extraDescription = '' ${"\n\n"} - Recommended: - X11: `pkgs.xclip` - Wayland: `pkgs.wl-clipboard` - MacOS: `pkgs.pngpaste` ''; }; + }; - default = pluginOptions; + settingsOptions = { + default = { + img_dir = + defaultNullOpts.mkNullable + (types.oneOf [ + types.str + (types.listOf types.str) + types.rawLua + ]) + "img" + '' + Directory name where the image will be pasted to. - filetypes = mkOption { - type = - with types; - attrsOf (submodule { - options = pluginOptions; - }); - apply = mapAttrs (_: processPluginOptions); - default = { }; - description = "Override certain options for specific filetypes."; - example = { - markdown = { - imgDir = [ - "src" - "assets" - "img" - ]; - imgDirTxt = "/assets/img"; - imgHandler = '' - function(img) - local script = string.format('./image_compressor.sh "%s"', img.path) - os.execute(script) - end + > [!Note] + > If you want to create nested dir, it is better to use table since windows and unix have different path separator. ''; - }; + + img_dir_txt = defaultNullOpts.mkNullable (types.oneOf [ + types.str + (types.listOf types.str) + types.rawLua + ]) "img" "Directory that will be inserted into text/buffer."; + + img_name = defaultNullOpts.mkStr { + __raw = "function() return os.date('%Y-%m-%d-%H-%M-%S') end"; + } "Image's name."; + + img_handler = defaultNullOpts.mkLuaFn "function(img) end" '' + Function that will handle image after pasted. + + > [!Note] + > `img` is a table that contain pasted image's `{name}` and `{path}`. + ''; + + affix = defaultNullOpts.mkStr' { + pluginDefault = lib.literalMD '' + `default`: `"{img_path}"` + `markdown`: `"![]({img_path})"` + ''; + description = "String that sandwiches the image's path."; + example = lib.literalExpression '' + > [!Note] + + > ```nix + > # You can use line break escape sequence + > affix = "<\n %s\n>"; + > ``` + + > ```nix + > # Or lua's double square brackets + > affix.__raw = \'\' + > [[< + > %s + > >]] + > \'\' + > ``` + ''; }; }; }; - config = mkIf cfg.enable { - extraPlugins = [ cfg.package ]; - - extraPackages = [ cfg.clipboardPackage ]; - - extraConfigLua = - let - setupOptions = - { - default = processPluginOptions cfg.default; - } - // cfg.filetypes - // cfg.extraOptions; - in - '' - require('clipboard-image').setup(${lib.nixvim.toLuaObject setupOptions}) - ''; + settingsExample = { + settings = { + img_dir = "img"; + img_dir_txt = "img"; + img_name.__raw = "function() return os.date('%Y-%m-%d-%H-%M-%S') end"; + img_handler.__raw = "function(img) end"; + affix = "![]({img_path})"; + }; }; + + extraConfig = cfg: { + extraPackages = [ cfg.clipboardPackage ]; + }; + + # TODO: Deprecated in 2025-02-01 + inherit (import ./deprecations.nix { inherit lib; }) + imports + deprecateExtraOptions + optionsRenamedToSettings + ; } diff --git a/plugins/by-name/clipboard-image/deprecations.nix b/plugins/by-name/clipboard-image/deprecations.nix new file mode 100644 index 00000000..63f56517 --- /dev/null +++ b/plugins/by-name/clipboard-image/deprecations.nix @@ -0,0 +1,42 @@ +{ lib }: +{ + imports = [ + (lib.mkRemovedOptionModule + [ + "plugins" + "clipboard-image" + "filetypes" + ] + '' + Please use `plugins.clipboard-image.settings` for each filetype configuration instead. + The attribute key is the name of the filetype and contains the same options as the default key. + + Note that nested options will now be snake_case, as well, to match upstream plugin configuration. + '' + ) + ]; + + deprecateExtraOptions = true; + optionsRenamedToSettings = [ + [ + "default" + "imgDir" + ] + [ + "default" + "imgDirTxt" + ] + [ + "default" + "imgName" + ] + [ + "default" + "imgHandler" + ] + [ + "default" + "affix" + ] + ]; +} diff --git a/tests/test-sources/plugins/by-name/clipboard-image/default.nix b/tests/test-sources/plugins/by-name/clipboard-image/default.nix index 4feb24cd..159a19b1 100644 --- a/tests/test-sources/plugins/by-name/clipboard-image/default.nix +++ b/tests/test-sources/plugins/by-name/clipboard-image/default.nix @@ -12,22 +12,22 @@ enable = true; clipboardPackage = pkgs.wl-clipboard; - default = { - imgDir = "img"; - imgDirTxt = "img"; - imgName.__raw = "function() return os.date('%Y-%m-%d-%H-%M-%S') end"; - imgHandler = "function(img) end"; - affix = "{img_path}"; - }; - filetypes = { + settings = { + default = { + img_dir = "img"; + img_dir_txt = "img"; + img_name.__raw = "function() return os.date('%Y-%m-%d-%H-%M-%S') end"; + img_handler.__raw = "function(img) end"; + affix = "![]({img_path})"; + }; markdown = { - imgDir = [ + img_dir = [ "src" "assets" "img" ]; - imgDirTxt = "/assets/img"; - imgHandler = '' + img_dir_txt = "/assets/img"; + img_handler.__raw = '' function(img) -- New feature from PR #22 local script = string.format('./image_compressor.sh "%s"', img.path) os.execute(script)