plugins/colorizer: rename, switch to mkNeovimPlugin

This commit is contained in:
Gaetan Lepage 2024-12-08 14:05:53 +01:00
parent 4b5364a66b
commit f2a991ae8c
4 changed files with 313 additions and 204 deletions

View file

@ -0,0 +1,251 @@
{
lib,
...
}:
let
inherit (lib.nixvim) defaultNullOpts;
inherit (lib) types;
in
lib.nixvim.neovim-plugin.mkNeovimPlugin {
name = "colorizer";
originalName = "nvim-colorizer.lua";
package = "nvim-colorizer-lua";
maintainers = [ lib.maintainers.GaetanLepage ];
# TODO introduced 2024-07-06: remove after 25.05
imports =
let
oldPluginPath = [
"plugins"
"nvim-colorizer"
];
newPluginPath = [
"plugins"
"colorizer"
];
optionsToRename = [
"RGB"
"RRGGBB"
"names"
"RRGGBBAA"
"AARRGGBB"
"rgb_fn"
"hsl_fn"
"tailwind"
"sass"
"virtualtext"
"virtualtext_inline"
"virtualtext_mode"
"always_update"
];
renameOption =
optionName:
lib.mkRenamedOptionModule
(
oldPluginPath
++ [
"userDefaultOptions"
optionName
]
)
(
newPluginPath
++ [
"settings"
"user_default_options"
optionName
]
);
in
map renameOption optionsToRename
++
lib.mapAttrsToList
(
oldOptionName: newPath:
lib.mkRenamedOptionModule (oldPluginPath ++ [ oldOptionName ]) (newPluginPath ++ newPath)
)
{
enable = [ "enable" ];
package = [ "package" ];
fileTypes = [
"settings"
"filetypes"
];
bufTypes = [
"settings"
"bufTypes"
];
};
settingsOptions =
let
colorizerOptions = {
names = defaultNullOpts.mkBool true ''
Whether to highlight name codes like `Blue` or `blue`.
'';
RGB = defaultNullOpts.mkBool true ''
Whether to highlight `#RGB` hex codes.
'';
RRGGBB = defaultNullOpts.mkBool true ''
Whether to highlight `#RRGGBB` hex codes.
'';
RRGGBBAA = defaultNullOpts.mkBool false ''
Whether to highlight `#RRGGBBAA` hex codes.
'';
AARRGGBB = defaultNullOpts.mkBool false ''
Whether to highlight `0xAARRGGBB` hex codes.
'';
rgb_fn = defaultNullOpts.mkBool false ''
Whether to highlight CSS `rgb()` and `rgba()` functions.
'';
hsl_fn = defaultNullOpts.mkBool false ''
Whether to highlight CSS `hsl()` and `hsla()` functions.
'';
css = defaultNullOpts.mkBool false ''
Enable all CSS features: `rgb_fn`, `hsl_fn`, `names`, `RGB`, `RRGGBB`.
'';
css_fn = defaultNullOpts.mkBool false ''
Enable all CSS *functions*: `rgb_fn`, `hsl_fn`.
'';
mode =
defaultNullOpts.mkEnum
[
"foreground"
"background"
"virtualtext"
]
"background"
''
Set the display mode.
'';
tailwind =
defaultNullOpts.mkNullable
(
with types;
either bool (enum [
"normal"
"lsp"
"both"
])
)
false
''
Enable tailwind colors.
It can be a boolean, `"normal"`, `"lsp"` or `"both"`.
`true` is same as `"normal"`.
'';
sass = {
enable = defaultNullOpts.mkBool false ''
Enable sass colors.
'';
parsers = defaultNullOpts.mkListOf types.str [ "css" ] ''
Parsers can contain values used in `user_default_options`.
'';
};
virtualtext = defaultNullOpts.mkStr "" ''
Virtualtext character to use.
'';
virtualtext_inline = defaultNullOpts.mkBool false ''
Display virtualtext inline with color.
'';
virtualtext_mode = defaultNullOpts.mkEnum [ "background" "foreground" ] "foreground" ''
Virtualtext highlight mode.
'';
always_update = defaultNullOpts.mkBool false ''
Update color values even if buffer is not focused.
Example use: `cmp_menu`, `cmp_docs`.
'';
};
in
{
filetypes = defaultNullOpts.mkNullable' {
type =
with types;
either (attrsOf (
either str (submodule {
freeformType = attrsOf anything;
options = colorizerOptions;
})
)) (listOf str);
pluginDefault = [ ];
description = ''
Per-filetype options.
'';
example = {
__unkeyed-1 = "css";
__unkeyed-2 = "javascript";
html = {
mode = "background";
};
};
};
user_default_options = colorizerOptions;
buftypes = defaultNullOpts.mkNullable' {
type =
with types;
either (attrsOf (
either str (submodule {
freeformType = attrsOf anything;
options = colorizerOptions;
})
)) (listOf str);
pluginDefault = [ ];
description = ''
Per-buftype options.
Buftype value is fetched by `vim.bo.buftype`.
'';
example = [
"*"
"!prompt"
"!popup"
];
};
user_commands = defaultNullOpts.mkNullable' {
type = with types; either bool (listOf str);
pluginDefault = true;
description = ''
Enable all or some usercommands.
'';
};
};
settingsExample = {
filetypes = {
__unkeyed-1 = "*";
__unkeyed-2 = "!vim";
css.rgb_fn = true;
html.names = false;
};
user_default_options = {
mode = "virtualtext";
names = false;
virtualtext = " ";
};
user_commands = [
"ColorizerToggle"
"ColorizerReloadAllBuffers"
];
};
}

View file

@ -1,199 +0,0 @@
{
lib,
helpers,
config,
pkgs,
...
}:
with lib;
let
cfg = config.plugins.nvim-colorizer;
colorizer-options = {
RGB = mkOption {
description = "#RGB hex codes";
type = types.nullOr types.bool;
default = null;
};
RRGGBB = mkOption {
description = "#RRGGBB hex codes";
type = types.nullOr types.bool;
default = null;
};
names = mkOption {
description = "\"Name\" codes like Blue or blue";
type = types.nullOr types.bool;
default = null;
};
RRGGBBAA = mkOption {
description = "#RRGGBBAA hex codes";
type = types.nullOr types.bool;
default = null;
};
AARRGGBB = mkOption {
description = "0xAARRGGBB hex codes";
type = types.nullOr types.bool;
default = null;
};
rgb_fn = mkOption {
description = "CSS rgb() and rgba() functions";
type = types.nullOr types.bool;
default = null;
};
hsl_fn = mkOption {
description = "CSS hsl() and hsla() functions";
type = types.nullOr types.bool;
default = null;
};
css = mkOption {
description = "Enable all CSS features: rgb_fn, hsl_fn, names, RGB, RRGGBB";
type = types.nullOr types.bool;
default = null;
};
css_fn = mkOption {
description = "Enable all CSS *functions*: rgb_fn, hsl_fn";
type = types.nullOr types.bool;
default = null;
};
mode = mkOption {
description = "Set the display mode";
type = types.nullOr (
types.enum [
"foreground"
"background"
"virtualtext"
]
);
default = null;
};
tailwind = mkOption {
description = "Enable tailwind colors";
type = types.nullOr (
types.oneOf [
types.bool
(types.enum [
"normal"
"lsp"
"both"
])
]
);
default = null;
};
sass = {
enable = mkOption {
description = "Enable sass colors";
type = types.nullOr types.bool;
default = null;
};
parsers = mkOption {
description = "sass parsers settings";
type = types.nullOr types.attrs;
default = null;
};
};
virtualtext = mkOption {
description = "Set the virtualtext character (only used when mode is set to 'virtualtext')";
type = types.nullOr types.str;
default = null;
};
virtualtext_inline = mkOption {
description = "Display virtualtext inline with color";
type = types.nullOr types.bool;
default = null;
};
virtualtext_mode = mkOption {
description = "virtualtext highlighting mode";
type = types.nullOr (
types.enum [
"foreground"
"background"
]
);
default = null;
};
always_update = mkOption {
description = "update color values even if buffer is not focused";
type = types.nullOr types.bool;
default = null;
};
};
in
{
options = {
plugins.nvim-colorizer = {
enable = mkEnableOption "nvim-colorizer";
package = lib.mkPackageOption pkgs "nvim-colorizer" {
default = [
"vimPlugins"
"nvim-colorizer-lua"
];
};
fileTypes = mkOption {
description = "Enable and/or configure highlighting for certain filetypes";
type =
with types;
nullOr (
listOf (
either str (
types.submodule {
options = {
language = mkOption {
type = types.str;
description = "The language this configuration should apply to.";
};
} // colorizer-options;
}
)
)
);
default = null;
};
userDefaultOptions = mkOption {
description = "Default options";
type = types.nullOr (types.submodule { options = colorizer-options; });
default = null;
};
bufTypes = mkOption {
description = "Buftype value is fetched by vim.bo.buftype";
type = types.nullOr (types.listOf types.str);
default = null;
};
};
};
config = mkIf cfg.enable {
extraPlugins = [ cfg.package ];
extraConfigLua =
let
filetypes =
if (cfg.fileTypes != null) then
(
let
list = map (
v:
if builtins.isAttrs v then
v.language + " = " + helpers.toLuaObject (builtins.removeAttrs v [ "language" ])
else
"'${v}'"
) cfg.fileTypes;
in
"{" + (concatStringsSep "," list) + "}"
)
else
"nil";
in
''
require("colorizer").setup({
filetypes = ${filetypes},
user_default_options = ${helpers.toLuaObject cfg.userDefaultOptions},
buftypes = ${helpers.toLuaObject cfg.bufTypes},
})
'';
};
}

View file

@ -0,0 +1,62 @@
{
empty = {
plugins.colorizer.enable = true;
};
defaults = {
plugins.colorizer = {
enable = true;
settings = {
filetypes = { };
user_default_options = {
RGB = true;
RRGGBB = true;
names = true;
RRGGBBAA = false;
AARRGGBB = false;
rgb_fn = false;
hsl_fn = false;
css = false;
css_fn = false;
mode = "background";
tailwind = false;
sass = {
enable = false;
parsers = [ "css" ];
};
virtualtext = "";
virtualtext_inline = false;
virtualtext_mode = "foreground";
always_update = false;
};
buftypes = { };
user_commands = true;
};
};
};
example = {
plugins.colorizer = {
enable = true;
settings = {
filetypes = {
__unkeyed-1 = "*";
__unkeyed-2 = "!vim";
css.rgb_fn = true;
html.names = false;
};
user_default_options = {
mode = "virtualtext";
names = false;
virtualtext = " ";
};
user_commands = [
"ColorizerToggle"
"ColorizerReloadAllBuffers"
];
};
};
};
}

View file

@ -1,5 +0,0 @@
{
empty = {
plugins.nvim-colorizer.enable = true;
};
}