diff --git a/plugins/by-name/rainbow-delimiters/default.nix b/plugins/by-name/rainbow-delimiters/default.nix index 7db7a0f6..2809c9ba 100644 --- a/plugins/by-name/rainbow-delimiters/default.nix +++ b/plugins/by-name/rainbow-delimiters/default.nix @@ -1,24 +1,30 @@ { lib, - helpers, config, - pkgs, + options, ... }: -with lib; -{ - options.plugins.rainbow-delimiters = lib.nixvim.plugins.neovim.extraOptionsOptions // { - enable = mkEnableOption "rainbow-delimiters.nvim"; +let + opts = options.plugins.rainbow-delimiters; + inherit (builtins) any isNull; + inherit (lib) mapAttrs' nameValuePair isString; + inherit (lib.nixvim) mkRaw toLuaObject nestedLiteralLua; +in +lib.nixvim.plugins.mkNeovimPlugin { + name = "rainbow-delimiters"; + package = "rainbow-delimiters-nvim"; + description = "Rainbow delimiters for Neovim with Tree-sitter"; + maintainers = [ ]; - package = lib.mkPackageOption pkgs "rainbow-delimiters.nvim" { - default = [ - "vimPlugins" - "rainbow-delimiters-nvim" - ]; - }; + # This plugin uses globals for configuration. + callSetup = false; - strategy = helpers.defaultNullOpts.mkAttrsOf' { - type = types.enum [ + # TODO: introduced 2025-10-1: remove after 26.05 + inherit (import ./deprecations.nix lib) deprecateExtraOptions optionsRenamedToSettings imports; + + extraOptions = { + strategy = lib.nixvim.defaultNullOpts.mkAttrsOf' { + type = lib.types.enum [ "global" "local" "noop" @@ -29,12 +35,13 @@ with lib; description = '' Attrs mapping Tree-sitter language names to strategies. See `|rb-delimiters-strategy|` for more information about strategies. + This option adds definitions to `${opts.settings}.strategy` wrapped with required Lua code. ''; - example = literalMD '' + example = lib.literalMD '' ```nix { # Use global strategy by default - default = "global"; + "" = "global"; # Use local for HTML html = "local"; @@ -56,102 +63,66 @@ with lib; ``` ''; }; - - query = - helpers.defaultNullOpts.mkAttrsOf types.str - { - default = "rainbow-delimiters"; - lua = "rainbow-blocks"; - } - '' - Attrs mapping Tree-sitter language names to queries. - See `|rb-delimiters-query|` for more information about queries. - ''; - - highlight = - helpers.defaultNullOpts.mkListOf types.str - [ - "RainbowDelimiterRed" - "RainbowDelimiterYellow" - "RainbowDelimiterBlue" - "RainbowDelimiterOrange" - "RainbowDelimiterGreen" - "RainbowDelimiterViolet" - "RainbowDelimiterCyan" - ] - '' - List of names of the highlight groups to use for highlighting, for more information see - `|rb-delimiters-colors|`. - ''; - - whitelist = helpers.mkNullOrOption (with types; listOf str) '' - List of Tree-sitter languages for which to enable rainbow delimiters. - Rainbow delimiters will be disabled for all other languages. - ''; - - blacklist = helpers.mkNullOrOption (with types; listOf str) '' - List of Tree-sitter languages for which to disable rainbow delimiters. - Rainbow delimiters will be enabled for all other languages. - ''; - - log = { - file = - helpers.defaultNullOpts.mkStr { __raw = "vim.fn.stdpath('log') .. '/rainbow-delimiters.log'"; } - '' - Path to the log file, default is `rainbow-delimiters.log` in your standard log path - (see `|standard-path|`). - ''; - - level = helpers.defaultNullOpts.mkLogLevel "warn" '' - Only messages equal to or above this value will be logged. - The default is to log warnings or above. - See `|log_levels|` for possible values. - ''; - }; }; - config = - let - cfg = config.plugins.rainbow-delimiters; - in - mkIf cfg.enable { - warnings = lib.nixvim.mkWarnings "plugins.rainbow-delimiters" { + settingsExample = { + blacklist = [ "json" ]; + strategy = { + "" = nestedLiteralLua "require 'rainbow-delimiters'.strategy['global']"; + "nix" = nestedLiteralLua "require 'rainbow-delimiters'.strategy['local']"; + }; + highlight = [ + "RainbowDelimiterViolet" + "RainbowDelimiterBlue" + "RainbowDelimiterGreen" + ]; + }; + + extraConfig = cfg: { + warnings = lib.nixvim.mkWarnings "plugins.rainbow-delimiters" [ + { when = !config.plugins.treesitter.enable; message = "This plugin needs treesitter to function as intended."; - }; - assertions = lib.nixvim.mkAssertions "plugins.rainbow-delimiters" { - assertion = (cfg.whitelist == null) || (cfg.blacklist == null); + } + # TODO: introduced 2025-10-1: remove after 26.05 + { + when = cfg.strategy ? default; message = '' - Both `rainbow-delimiters.whitelist` and `rainbow-delimiters.blacklist` should not be - set simultaneously. - Please remove one of them. + Setting `${opts.strategy}` keys to the string `"default"` is deprecated. + You can set them to an empty string `""` instead. ''; - }; + } + { + when = (cfg.query ? default) && (cfg.query.default != "_mkMergedOptionModule"); + message = '' + Setting `${opts.query}` keys to the string `"default"` is deprecated. + You can set them to an empty string `""` instead. + ''; + } + ]; - extraPlugins = [ cfg.package ]; - - globals.rainbow_delimiters = - with cfg; - { - strategy = helpers.ifNonNull' strategy ( - mapAttrs' (name: value: { - name = if name == "default" then "__emptyString" else name; - value = - if isString value then helpers.mkRaw "require 'rainbow-delimiters'.strategy['${value}']" else value; - }) strategy - ); - query = helpers.ifNonNull' query ( - mapAttrs' (name: value: { - name = if name == "default" then "__emptyString" else name; - inherit value; - }) query - ); - inherit highlight whitelist blacklist; - log = with log; { - inherit file level; - }; - } - // cfg.extraOptions; + assertions = lib.nixvim.mkAssertions "plugins.rainbow-delimiters" { + assertion = any isNull [ + (cfg.settings.whitelist or null) + (cfg.settings.blacklist or null) + ]; + message = '' + Both `${opts.settings}.whitelist` and `${opts.settings}.blacklist` should not be set simultaneously. + Please remove one of them. + ''; }; + + globals.rainbow_delimiters = lib.mkMerge [ + cfg.settings + (lib.mkIf (cfg.strategy != null) { + strategy = mapAttrs' ( + n: v: + nameValuePair (if n == "default" then "" else n) ( + if isString v then mkRaw "require('rainbow-delimiters').strategy[${toLuaObject v}]" else v + ) + ) cfg.strategy; + }) + ]; + }; } diff --git a/plugins/by-name/rainbow-delimiters/deprecations.nix b/plugins/by-name/rainbow-delimiters/deprecations.nix new file mode 100644 index 00000000..a5f06009 --- /dev/null +++ b/plugins/by-name/rainbow-delimiters/deprecations.nix @@ -0,0 +1,40 @@ +lib: +let + inherit (lib) mapAttrs' nameValuePair; + inherit (lib.nixvim) ifNonNull'; + basePathAnd = lib.concat [ + "plugins" + "rainbow-delimiters" + ]; +in +{ + deprecateExtraOptions = true; + + optionsRenamedToSettings = map (lib.splitString ".") [ + "highlight" + "whitelist" + "blacklist" + "log.file" + "log.level" + ]; + + imports = [ + ( + let + oldOptPath = basePathAnd [ "query" ]; + in + lib.mkChangedOptionModule oldOptPath + (basePathAnd [ + "settings" + "query" + ]) + ( + config: + let + old = lib.getAttrFromPath oldOptPath config; + in + ifNonNull' old (mapAttrs' (n: nameValuePair (if n == "default" then "" else n)) old) + ) + ) + ]; +} diff --git a/tests/test-sources/plugins/by-name/rainbow-delimiters/default.nix b/tests/test-sources/plugins/by-name/rainbow-delimiters/default.nix index e402d069..8b21fc70 100644 --- a/tests/test-sources/plugins/by-name/rainbow-delimiters/default.nix +++ b/tests/test-sources/plugins/by-name/rainbow-delimiters/default.nix @@ -11,58 +11,56 @@ treesitter.enable = true; rainbow-delimiters = { enable = true; - - strategy = { - default = "global"; - html = "local"; - latex.__raw = '' - function() - -- Disabled for very large files, global strategy for large files, - -- local strategy otherwise - if vim.fn.line('$') > 10000 then - return nil - elseif vim.fn.line('$') > 1000 then - return rainbow.strategy['global'] - end - return rainbow.strategy['local'] - end - ''; - }; - query = { - default = "rainbow-delimiters"; - lua = "rainbow-blocks"; - }; - highlight = [ - "RainbowDelimiterRed" - "RainbowDelimiterYellow" - "RainbowDelimiterBlue" - "RainbowDelimiterOrange" - "RainbowDelimiterGreen" - "RainbowDelimiterViolet" - "RainbowDelimiterCyan" - ]; - blacklist = [ - "c" - "cpp" - ]; - log = { - file.__raw = "vim.fn.stdpath('log') .. '/rainbow-delimiters.log'"; - level = "warn"; + settings = { + settingsExample = { + blacklist = [ "json" ]; + strategy = { + "".__raw = "require 'rainbow-delimiters'.strategy['global']"; + "nix".__raw = "require 'rainbow-delimiters'.strategy['local']"; + }; + highlight = [ + "RainbowDelimiterViolet" + "RainbowDelimiterBlue" + "RainbowDelimiterGreen" + ]; + }; }; }; }; }; - example-whitelist = { + defaults = { plugins = { treesitter.enable = true; rainbow-delimiters = { enable = true; + settings = { + query = { + "" = "rainbow-delimiters"; + javascript = "rainbow-delimiters-react"; + }; + strategy = { + "".__raw = "require 'rainbow-delimiters'.strategy['global']"; + }; + priority = { + "".__raw = + "math.floor(((vim.hl or vim.highlight).priorities.semantic_tokens + (vim.hl or vim.highlight).priorities.treesitter) / 2)"; + }; + log = { + level.__raw = "vim.log.levels.WARN"; + file.__raw = "vim.fn.stdpath('log') .. '/rainbow-delimiters.log'"; + }; + highlight = [ + "RainbowDelimiterRed" + "RainbowDelimiterYellow" + "RainbowDelimiterBlue" + "RainbowDelimiterOrange" + "RainbowDelimiterGreen" + "RainbowDelimiterViolet" + "RainbowDelimiterCyan" + ]; - whitelist = [ - "c" - "cpp" - ]; + }; }; }; };