From 24f305fb00c6aea5cdebfc40a0f0d574dd403f88 Mon Sep 17 00:00:00 2001
From: Meow Honk <21010072+catgoose@users.noreply.github.com>
Date: Fri, 27 Dec 2024 17:30:00 -0600
Subject: [PATCH] feat(names): Configure lower/camel/upper case of names
highlights (#123)
* ref: initialize caches in do block upon module load
* feat: adds casing, strip_digit customization to names config
* doc: updates documentation for names_opts
---
README.md | 29 +++++--
doc/colorizer.txt | 75 ++++++++++-------
doc/modules/colorizer.config.html | 45 ++++++++--
doc/modules/colorizer.matcher.html | 44 +++++-----
doc/modules/colorizer.parser.names.html | 46 +++++------
lua/colorizer/config.lua | 36 +++++---
lua/colorizer/matcher.lua | 104 ++++++++++++++----------
lua/colorizer/parser/names.lua | 59 +++++++-------
lua/colorizer/tailwind_colors.lua | 2 -
test/expect.lua | 14 ++++
10 files changed, 275 insertions(+), 179 deletions(-)
diff --git a/README.md b/README.md
index 764c0a3..88c0258 100644
--- a/README.md
+++ b/README.md
@@ -123,10 +123,17 @@ library to do custom highlighting themselves.
require("colorizer").setup({
filetypes = { "*" },
user_default_options = {
- names = true, -- "Name" codes like Blue or blue
- -- Expects a table of color name to rgb value pairs. # is optional
+ names = true, -- "Name" codes like Blue or red. Added from `vim.api.nvim_get_color_map()`
+ names_opts = { -- options for mutating/filtering names.
+ lowercase = true, -- name:lower(), highlight `blue` and `red`
+ camelcase = true, -- name, highlight `Blue` and `Red`
+ uppercase = false, -- name:upper(), highlight `BLUE` and `RED`
+ strip_digits = false, -- ignore names with digits,
+ -- highlight `blue` and `red`, but not `blue3` and `red4`
+ },
+ -- Expects a table of color name to #RRGGBB value pairs. # is optional
-- Example: { cool = "#107dac", ["notcool"] = "ee9240" }
- -- Set to false|nil to disable
+ -- Set to false|nil to disable, for example when setting filetype options
names_custom = false, -- Custom names to be highlighted: table|function|false|nil
RGB = true, -- #RGB hex codes
RGBA = true, -- #RGBA hex codes
@@ -135,7 +142,8 @@ library to do custom highlighting themselves.
AARRGGBB = false, -- 0xAARRGGBB hex codes
rgb_fn = false, -- CSS rgb() and rgba() functions
hsl_fn = false, -- CSS hsl() and hsla() functions
- css = false, -- Enable all CSS features: rgb_fn, hsl_fn, names, RGB, RRGGBB
+ css = false, -- Enable all CSS *features*:
+ -- names, RGB, RGBA, RRGGBB, RRGGBBAA, AARRGGBB, rgb_fn, hsl_fn
css_fn = false, -- Enable all CSS *functions*: rgb_fn, hsl_fn
-- Highlighting mode. 'background'|'foreground'|'virtualtext'
mode = "background", -- Set the display mode
@@ -246,14 +254,15 @@ require("colorizer").setup({
In `user_default_options`, there are 2 types of options
-1. Individual options - `names`, `RGB`, `RRGGBB`, `RRGGBBAA`, `hsl_fn`, `rgb_fn`,
- `RRGGBBAA`, `AARRGGBB`, `tailwind`, `sass`
+1. Individual options - `names`, `names_opts`, `names_custom`, `RGB`, `RGBA`,
+ `RRGGBB`, `RRGGBBAA`, `hsl_fn`, `rgb_fn`, `RRGGBBAA`, `AARRGGBB`, `tailwind`,
+ `sass`
1. Alias options - `css`, `css_fn`
If `css_fn` is true, then `hsl_fn`, `rgb_fn` becomes `true`
-If `css` is true, then `names`, `RGB`, `RRGGBB`, `RRGGBBAA`, `hsl_fn`, `rgb_fn`
+If `css` is true, then `names`, `RGB`, `RGBA`, `RRGGBB`, `RRGGBBAA`, `hsl_fn`, `rgb_fn`
becomes `true`
These options have a priority, Individual options have the highest priority,
@@ -261,7 +270,8 @@ then alias options
For alias, `css_fn` has more priority over `css`
-e.g: Here `RGB`, `RRGGBB`, `RRGGBBAA`, `hsl_fn`, `rgb_fn` is enabled but not `names`
+e.g: Here `RGB`, `RGBA`, `RRGGBB`, `RRGGBBAA`, `AARRGGBB`, `hsl_fn`, `rgb_fn` is
+enabled but not `names`
```lua
require("colorizer").setup({
@@ -272,7 +282,8 @@ require("colorizer").setup({
})
```
-e.g: Here `names`, `RGB`, `RRGGBB`, `RRGGBBAA` is enabled but not `rgb_fn` and `hsl_fn`
+e.g: Here `names`, `RGB`, `RGBA`, `RRGGBB`, `RRGGBBAA`, `AARRGGBB` is enabled but
+not `rgb_fn` and `hsl_fn`
```lua
require("colorizer").setup({
diff --git a/doc/colorizer.txt b/doc/colorizer.txt
index cc609c6..92419cb 100644
--- a/doc/colorizer.txt
+++ b/doc/colorizer.txt
@@ -453,6 +453,8 @@ Provides configuration options and utilities for setting up colorizer.
LUA API *colorizer.config-lua-api*
Functions: ~
+ |reset_cache| - Reset the cache for buffer options.
+
|set_bo_value| - Set options for a specific buffer or file type.
|apply_alias_options| - Parse and apply alias options to the user options.
@@ -472,6 +474,14 @@ Tables: ~
|opts| - Configuration options for the `setup` function.
+reset_cache() *colorizer.config.reset_cache*
+ Reset the cache for buffer options.
+
+ Called from colorizer.setup
+
+
+
+
set_bo_value({bo_type}, {value}, {options}) *colorizer.config.set_bo_value*
Set options for a specific buffer or file type.
@@ -559,12 +569,15 @@ user_default_options *colorizer.config.user_default_options*
Fields: ~
- {RGB} - boolean: Enables `#RGB` hex codes.
- {RRGGBB} - boolean: Enables `#RRGGBB` hex codes.
{names} - boolean: Enables named colors (e.g., "Blue").
- {names_custom} - table|function|nil: Extra color name to RGB value
- mappings
+ {names_opts} - table: Names options for customizing casing, digit
+ stripping, etc
+ {names_custom} - table|function|false|nil: Custom color name to RGB
+ value mappings
should return a table of color names to RGB value pairs
+ {RGB} - boolean: Enables `#RGB` hex codes.
+ {RGBA} - boolean: Enables `#RGBA` hex codes.
+ {RRGGBB} - boolean: Enables `#RRGGBB` hex codes.
{RRGGBBAA} - boolean: Enables `#RRGGBBAA` hex codes.
{AARRGGBB} - boolean: Enables `0xAARRGGBB` hex codes.
{rgb_fn} - boolean: Enables CSS `rgb()` and `rgba()` functions.
@@ -606,10 +619,14 @@ opts *colorizer.config.opts*
{filetypes} - table A list of file types where colorizer should be
enabled. Use `"*"` for all file types.
{user_default_options} - table Default options for color handling.
+ - `names` (boolean): Enables named color codes like `"Blue"`.
+ - `names_opts` (table): Names options for customizing casing, digit
+ stripping, etc
+ - `names_custom` (table|function|false|nil): Custom color name to RGB value
+ mappings
- `RGB` (boolean): Enables support for `#RGB` hex codes.
+ - `RGBA` (boolean): Enables support for `#RGBA` hex codes.
- `RRGGBB` (boolean): Enables support for `#RRGGBB` hex codes.
- - `names` (boolean): Enables named color codes like `"Blue"`.
- - `names_custom` (table|function|nil): Extra color name to RGB value mappings
- `RRGGBBAA` (boolean): Enables support for `#RRGGBBAA` hex codes.
- `AARRGGBB` (boolean): Enables support for `0xAARRGGBB` hex codes.
- `rgb_fn` (boolean): Enables CSS `rgb()` and `rgba()` functions.
@@ -658,13 +675,19 @@ Manages matching and parsing of color patterns in buffers.
LUA API *colorizer.matcher-lua-api*
Functions: ~
+ |reset_cache| - Reset matcher cache
+ Called from colorizer.setup
+
|make| - Parse the given options and return a function with enabled parsers.
- |reset_cache| - Reset the cache of matchers
-Called from colorizer.setup
+
+reset_cache() *colorizer.matcher.reset_cache*
+ Reset matcher cache
+ Called from colorizer.setup
+
-make({options}) *colorizer.matcher.make*
+make({opts}) *colorizer.matcher.make*
Parse the given options and return a function with enabled parsers.
if no parsers enabled then return false
@@ -672,7 +695,7 @@ make({options}) *colorizer.matcher.make*
Parameters: ~
- {options} - table: options created in `colorizer.setup`
+ {opts} - table: options created in `colorizer.setup`
returns:~
function or boolean: function which will just parse the line for enabled
@@ -680,12 +703,6 @@ make({options}) *colorizer.matcher.make*
-reset_cache() *colorizer.matcher.reset_cache*
- Reset the cache of matchers
- Called from colorizer.setup
-
-
-
==============================================================================
HSL *colorizer.parser.hsl-introduction*
@@ -743,29 +760,29 @@ text.
LUA API *colorizer.parser.names-lua-api*
Functions: ~
- |parser| - Parses a line to identify color names.
+ |reset_cache| - Reset the color names cache.
- |reset_cache| - Resets the color names cache.
+ |parser| - Parses a line to identify color names.
-parser({line}, {i}, {opts}) *colorizer.parser.names.parser*
- Parses a line to identify color names.
+reset_cache() *colorizer.parser.names.reset_cache*
+ Reset the color names cache.
- Parameters: ~
- {line} - string The text line to parse.
- {i} - number The index to start parsing from.
- {opts} - table Parsing options.
+ Called from colorizer.setup
- returns:~
- number or nil, string or nil Length of match and hex value if found.
-reset_cache() *colorizer.parser.names.reset_cache*
- Resets the color names cache.
+parser({line}, {i}, {opts}) *colorizer.parser.names.parser*
+ Parses a line to identify color names.
- Called from colorizer.setup
+ Parameters: ~
+ {line} - string: The text line to parse.
+ {i} - number: The index to start parsing from.
+ {opts} - table: Parsing options.
+ returns:~
+ number or nil, string or nil: Length of match and hex value if found.
diff --git a/doc/modules/colorizer.config.html b/doc/modules/colorizer.config.html
index f8250d6..c4b8fa3 100644
--- a/doc/modules/colorizer.config.html
+++ b/doc/modules/colorizer.config.html
@@ -68,6 +68,10 @@
Module colorizer.config
+
+ reset_cache () |
+ Reset the cache for buffer options. |
+
set_bo_value (bo_type, value, options) |
Set options for a specific buffer or file type. |
@@ -112,6 +116,21 @@
+ -
+
+ reset_cache ()
+
+ -
+ Reset the cache for buffer options.
+ Called from colorizer.setup
+
+
+
+
+
+
+
+
-
set_bo_value (bo_type, value, options)
@@ -267,19 +286,25 @@
Fields:
- - RGB
- boolean: Enables `#RGB` hex codes.
-
- - RRGGBB
- boolean: Enables `#RRGGBB` hex codes.
-
- names
boolean: Enables named colors (e.g., "Blue").
+ - names_opts
+ table: Names options for customizing casing, digit stripping, etc
+
- names_custom
- table|function|nil: Extra color name to RGB value mappings
+ table|function|false|nil: Custom color name to RGB value mappings
should return a table of color names to RGB value pairs
+ - RGB
+ boolean: Enables `#RGB` hex codes.
+
+ - RGBA
+ boolean: Enables `#RGBA` hex codes.
+
+ - RRGGBB
+ boolean: Enables `#RRGGBB` hex codes.
+
- RRGGBBAA
boolean: Enables `#RRGGBBAA` hex codes.
@@ -379,10 +404,12 @@ Fields:
- user_default_options
table Default options for color handling.
+ - `names` (boolean): Enables named color codes like `"Blue"`.
+ - `names_opts` (table): Names options for customizing casing, digit stripping, etc
+ - `names_custom` (table|function|false|nil): Custom color name to RGB value mappings
- `RGB` (boolean): Enables support for `#RGB` hex codes.
+ - `RGBA` (boolean): Enables support for `#RGBA` hex codes.
- `RRGGBB` (boolean): Enables support for `#RRGGBB` hex codes.
- - `names` (boolean): Enables named color codes like `"Blue"`.
- - `names_custom` (table|function|nil): Extra color name to RGB value mappings
- `RRGGBBAA` (boolean): Enables support for `#RRGGBBAA` hex codes.
- `AARRGGBB` (boolean): Enables support for `0xAARRGGBB` hex codes.
- `rgb_fn` (boolean): Enables CSS `rgb()` and `rgba()` functions.
diff --git a/doc/modules/colorizer.matcher.html b/doc/modules/colorizer.matcher.html
index b0799c9..bf0285a 100644
--- a/doc/modules/colorizer.matcher.html
+++ b/doc/modules/colorizer.matcher.html
@@ -71,13 +71,13 @@
Module colorizer.matcher
- make (options) |
- Parse the given options and return a function with enabled parsers. |
+ reset_cache () |
+ Reset matcher cache
+ Called from colorizer.setup |
- reset_cache () |
- Reset the cache of matchers
-Called from colorizer.setup |
+ make (opts) |
+ Parse the given options and return a function with enabled parsers. |
@@ -88,9 +88,24 @@
+ -
+
+ reset_cache ()
+
+ -
+ Reset matcher cache
+ Called from colorizer.setup
+
+
+
+
+
+
+
+
-
- make (options)
+ make (opts)
-
Parse the given options and return a function with enabled parsers.
@@ -100,7 +115,7 @@
Parameters:
- - options
+
- opts
table: options created in `colorizer.setup`
@@ -114,21 +129,6 @@ Returns:
-
- -
-
- reset_cache ()
-
- -
- Reset the cache of matchers
-Called from colorizer.setup
-
-
-
-
-
-
-
diff --git a/doc/modules/colorizer.parser.names.html b/doc/modules/colorizer.parser.names.html
index a4b3ec4..5b54e25 100644
--- a/doc/modules/colorizer.parser.names.html
+++ b/doc/modules/colorizer.parser.names.html
@@ -70,12 +70,12 @@ Module colorizer.parser.names
@@ -86,6 +86,21 @@
+ -
+
+ reset_cache ()
+
+ -
+ Reset the color names cache.
+ Called from colorizer.setup
+
+
+
+
+
+
+
+
-
parser (line, i, opts)
@@ -97,40 +112,25 @@
Parameters:
- line
- string The text line to parse.
+ string: The text line to parse.
- i
- number The index to start parsing from.
+ number: The index to start parsing from.
- opts
- table Parsing options.
+ table: Parsing options.
Returns:
- number|nil, string|nil Length of match and hex value if found.
+ number|nil, string|nil: Length of match and hex value if found.
-
- -
-
- reset_cache ()
-
- -
- Resets the color names cache.
-Called from colorizer.setup
-
-
-
-
-
-
-
diff --git a/lua/colorizer/config.lua b/lua/colorizer/config.lua
index 4931719..f7b32fc 100644
--- a/lua/colorizer/config.lua
+++ b/lua/colorizer/config.lua
@@ -5,6 +5,12 @@ local M = {}
--- Defaults for colorizer options
local plugin_user_default_options = {
names = true,
+ names_opts = {
+ lowercase = true,
+ camelcase = true,
+ uppercase = false,
+ strip_digits = false,
+ },
names_custom = false,
RGB = true,
RGBA = true,
@@ -38,11 +44,13 @@ local plugin_user_default_options = {
-- **Option Priority**: Individual options have higher priority than aliases.
-- If both `css` and `css_fn` are true, `css_fn` has more priority over `css`.
-- @table user_default_options
--- @field RGB boolean: Enables `#RGB` hex codes.
--- @field RRGGBB boolean: Enables `#RRGGBB` hex codes.
-- @field names boolean: Enables named colors (e.g., "Blue").
--- @field names_custom table|function|nil: Extra color name to RGB value mappings
+-- @field names_opts table: Names options for customizing casing, digit stripping, etc
+-- @field names_custom table|function|false|nil: Custom color name to RGB value mappings
-- should return a table of color names to RGB value pairs
+-- @field RGB boolean: Enables `#RGB` hex codes.
+-- @field RGBA boolean: Enables `#RGBA` hex codes.
+-- @field RRGGBB boolean: Enables `#RRGGBB` hex codes.
-- @field RRGGBBAA boolean: Enables `#RRGGBBAA` hex codes.
-- @field AARRGGBB boolean: Enables `0xAARRGGBB` hex codes.
-- @field rgb_fn boolean: Enables CSS `rgb()` and `rgba()` functions.
@@ -66,7 +74,6 @@ local plugin_user_default_options = {
--@field filetypes
--@field buftypes
M.options = {}
-
local function init_options()
M.options = {
-- setup options
@@ -80,8 +87,15 @@ local function init_options()
}
end
--- State for managing buffer and filetype-specific options
-local options_cache = { buftype = {}, filetype = {} }
+local options_cache
+--- Reset the cache for buffer options.
+-- Called from colorizer.setup
+function M.reset_cache()
+ options_cache = { buftype = {}, filetype = {} }
+end
+do
+ M.reset_cache()
+end
--- Set options for a specific buffer or file type.
---@param bo_type 'buftype'|'filetype': The type of buffer option
@@ -128,10 +142,12 @@ end
-- @table opts
-- @field filetypes table A list of file types where colorizer should be enabled. Use `"*"` for all file types.
-- @field user_default_options table Default options for color handling.
+-- - `names` (boolean): Enables named color codes like `"Blue"`.
+-- - `names_opts` (table): Names options for customizing casing, digit stripping, etc
+-- - `names_custom` (table|function|false|nil): Custom color name to RGB value mappings
-- - `RGB` (boolean): Enables support for `#RGB` hex codes.
+-- - `RGBA` (boolean): Enables support for `#RGBA` hex codes.
-- - `RRGGBB` (boolean): Enables support for `#RRGGBB` hex codes.
--- - `names` (boolean): Enables named color codes like `"Blue"`.
--- - `names_custom` (table|function|nil): Extra color name to RGB value mappings
-- - `RRGGBBAA` (boolean): Enables support for `#RRGGBBAA` hex codes.
-- - `AARRGGBB` (boolean): Enables support for `0xAARRGGBB` hex codes.
-- - `rgb_fn` (boolean): Enables CSS `rgb()` and `rgba()` functions.
@@ -186,8 +202,4 @@ function M.get_bo_options(bo_type, buftype, filetype)
return fo or bo
end
-function M.reset_cache()
- options_cache = { buftype = {}, filetype = {} }
-end
-
return M
diff --git a/lua/colorizer/matcher.lua b/lua/colorizer/matcher.lua
index 0e84818..be6e25a 100644
--- a/lua/colorizer/matcher.lua
+++ b/lua/colorizer/matcher.lua
@@ -67,45 +67,62 @@ local function compile(matchers, matchers_trie)
return parse_fn
end
-local matcher_cache = {}
+local matcher_cache
+---Reset matcher cache
+-- Called from colorizer.setup
+function M.reset_cache()
+ matcher_cache = {}
+end
+do
+ M.reset_cache()
+end
+
---Parse the given options and return a function with enabled parsers.
--if no parsers enabled then return false
--Do not try make the function again if it is present in the cache
----@param options table: options created in `colorizer.setup`
+---@param opts table: options created in `colorizer.setup`
---@return function|boolean: function which will just parse the line for enabled parsers
-function M.make(options)
- if not options then
+function M.make(opts)
+ if not opts then
return false
end
- local enable_names = options.names
- local enable_names_custom = options.names_custom
- local enable_sass = options.sass and options.sass.enable
- local enable_tailwind = options.tailwind
- local enable_RGB = options.RGB
- local enable_RGBA = options.RGBA
- local enable_RRGGBB = options.RRGGBB
- local enable_RRGGBBAA = options.RRGGBBAA
- local enable_AARRGGBB = options.AARRGGBB
- local enable_rgb = options.rgb_fn
- local enable_hsl = options.hsl_fn
+ local enable_names = opts.names
+ local enable_names_lowercase = opts.names_opts and opts.names_opts.lowercase
+ local enable_names_camelcase = opts.names_opts and opts.names_opts.camelcase
+ local enable_names_uppercase = opts.names_opts and opts.names_opts.uppercase
+ local enable_names_strip_digits = opts.names_opts and opts.names_opts.strip_digits
+ local enable_names_custom = opts.names_custom
+ local enable_sass = opts.sass and opts.sass.enable
+ local enable_tailwind = opts.tailwind
+ local enable_RGB = opts.RGB
+ local enable_RGBA = opts.RGBA
+ local enable_RRGGBB = opts.RRGGBB
+ local enable_RRGGBBAA = opts.RRGGBBAA
+ local enable_AARRGGBB = opts.AARRGGBB
+ local enable_rgb = opts.rgb_fn
+ local enable_hsl = opts.hsl_fn
-- Rather than use bit.lshift or calculate 2^x, use precalculated values to
-- create unique bitmask
local matcher_key = 0
+ (enable_names and 1 or 0)
- + (enable_names_custom and 2 or 0)
- + (enable_RGB and 4 or 0)
- + (enable_RGBA and 8 or 0)
- + (enable_RRGGBB and 16 or 0)
- + (enable_RRGGBBAA and 32 or 0)
- + (enable_AARRGGBB and 64 or 0)
- + (enable_rgb and 128 or 0)
- + (enable_hsl and 256 or 0)
- + ((enable_tailwind == true or enable_tailwind == "normal") and 512 or 0)
- + (enable_tailwind == "lsp" and 1024 or 0)
- + (enable_tailwind == "both" and 2048 or 0)
- + (enable_sass and 4096 or 0)
+ + (enable_names_lowercase and 2 or 0)
+ + (enable_names_camelcase and 4 or 0)
+ + (enable_names_uppercase and 8 or 0)
+ + (enable_names_strip_digits and 16 or 0)
+ + (enable_names_custom and 32 or 0)
+ + (enable_RGB and 64 or 0)
+ + (enable_RGBA and 128 or 0)
+ + (enable_RRGGBB and 256 or 0)
+ + (enable_RRGGBBAA and 512 or 0)
+ + (enable_AARRGGBB and 1024 or 0)
+ + (enable_rgb and 2048 or 0)
+ + (enable_hsl and 4096 or 0)
+ + ((enable_tailwind == true or enable_tailwind == "normal") and 8192 or 0)
+ + (enable_tailwind == "lsp" and 16384 or 0)
+ + (enable_tailwind == "both" and 32768 or 0)
+ + (enable_sass and 65536 or 0)
if matcher_key == 0 then
return false
@@ -119,18 +136,25 @@ function M.make(options)
local matchers = {}
local matchers_prefix = {}
- if enable_names then
- matchers.color_name_parser = matchers.color_name_parser or {}
- matchers.color_name_parser.color_names = enable_names
- end
- if enable_names_custom then
- matchers.color_name_parser = matchers.color_name_parser or {}
- matchers.color_name_parser.names_custom = enable_names_custom
- end
- if enable_tailwind then
+ if enable_names or enable_names_custom or enable_tailwind then
matchers.color_name_parser = matchers.color_name_parser or {}
- matchers.color_name_parser.tailwind = enable_tailwind
+ if enable_names then
+ matchers.color_name_parser.color_names = enable_names
+ matchers.color_name_parser.color_names_opts = {
+ lowercase = enable_names_lowercase,
+ camelcase = enable_names_camelcase,
+ uppercase = enable_names_uppercase,
+ strip_digits = enable_names_strip_digits,
+ }
+ end
+ if enable_names_custom then
+ matchers.color_name_parser.names_custom = enable_names_custom
+ end
+ if enable_tailwind then
+ matchers.color_name_parser.tailwind = enable_tailwind
+ end
end
+
if enable_sass then
matchers.sass_name_parser = true
end
@@ -181,10 +205,4 @@ function M.make(options)
return loop_parse_fn
end
----Reset the cache of matchers
----Called from colorizer.setup
-function M.reset_cache()
- matcher_cache = {}
-end
-
return M
diff --git a/lua/colorizer/parser/names.lua b/lua/colorizer/parser/names.lua
index 632db33..e565114 100644
--- a/lua/colorizer/parser/names.lua
+++ b/lua/colorizer/parser/names.lua
@@ -9,14 +9,21 @@ local utils = require("colorizer.utils")
local tohex = require("bit").tohex
local min, max = math.min, math.max
-local names_cache = {
- color_map = {},
- color_trie = nil,
- color_name_minlen = nil,
- color_name_maxlen = nil,
- color_name_settings = { lowercase = true, strip_digits = false },
- tailwind_enabled = false,
-}
+local names_cache
+---Reset the color names cache.
+-- Called from colorizer.setup
+function M.reset_cache()
+ names_cache = {
+ color_map = {},
+ color_trie = nil,
+ color_name_minlen = nil,
+ color_name_maxlen = nil,
+ tailwind_enabled = false,
+ }
+end
+do
+ M.reset_cache()
+end
--- Internal function to add a color to the Trie and map.
-- @param name string The color name.
@@ -102,14 +109,19 @@ local function handle_tailwind()
end
--- Handles Vim's color map and adds colors to the Trie and map.
-local function handle_names()
+local function handle_names(opts)
for name, value in pairs(vim.api.nvim_get_color_map()) do
- if not (names_cache.color_name_settings.strip_digits and name:match("%d+$")) then
+ if not (opts.strip_digits and name:match("%d+$")) then
local rgb_hex = tohex(value, 6)
- add_color(name, rgb_hex)
- if names_cache.color_name_settings.lowercase then
+ if opts.lowercase then
add_color(name:lower(), rgb_hex)
end
+ if opts.camelcase then
+ add_color(name, rgb_hex)
+ end
+ if opts.uppercase then
+ add_color(name:upper(), rgb_hex)
+ end
end
end
end
@@ -123,7 +135,7 @@ local function populate_colors(opts)
-- Add Vim's color map
if opts.color_names then
- handle_names()
+ handle_names(opts.color_names_opts)
end
-- Add Tailwind colors
@@ -139,10 +151,10 @@ local function populate_colors(opts)
end
--- Parses a line to identify color names.
--- @param line string The text line to parse.
--- @param i number The index to start parsing from.
--- @param opts table Parsing options.
--- @return number|nil, string|nil Length of match and hex value if found.
+-- @param line string: The text line to parse.
+-- @param i number: The index to start parsing from.
+-- @param opts table: Parsing options.
+-- @return number|nil, string|nil: Length of match and hex value if found.
function M.parser(line, i, opts)
if not names_cache.color_trie or opts.tailwind ~= names_cache.tailwind_enabled then
-- TODO: 2024-12-21 - Ensure that this is not being called too many times
@@ -166,17 +178,4 @@ function M.parser(line, i, opts)
end
end
----Resets the color names cache.
----Called from colorizer.setup
-function M.reset_cache()
- names_cache = {
- color_map = {},
- color_trie = nil,
- color_name_minlen = nil,
- color_name_maxlen = nil,
- color_name_settings = { lowercase = true, strip_digits = false },
- tailwind_enabled = false,
- }
-end
-
return M
diff --git a/lua/colorizer/tailwind_colors.lua b/lua/colorizer/tailwind_colors.lua
index 9c9f470..f1e5e4e 100644
--- a/lua/colorizer/tailwind_colors.lua
+++ b/lua/colorizer/tailwind_colors.lua
@@ -1,6 +1,5 @@
-- Tailwind colors
return {
- -- https://github.com/tailwindlabs/tailwindcss/raw/master/src/corePlugins.js
prefixes = {
"accent",
"bg",
@@ -24,7 +23,6 @@ return {
"ring",
"ring-offset",
},
- -- Generated using https://github.com/tailwindlabs/tailwindcss/raw/master/src/public/colors.js
colors = {
["black"] = "000",
["white"] = "fff",
diff --git a/test/expect.lua b/test/expect.lua
index da01088..b354c21 100644
--- a/test/expect.lua
+++ b/test/expect.lua
@@ -5,6 +5,9 @@ local opts = {
"!dashboard",
lua = {
names = true,
+ names_opts = {
+ -- strip_digits = false,
+ },
names_custom = {
one_two = "#017dac",
["three=four"] = "#3700c2",
@@ -18,6 +21,12 @@ local opts = {
user_commands = true,
user_default_options = {
names = false,
+ names_opts = {
+ lowercase = true,
+ camelcase = true,
+ uppercase = false,
+ strip_digits = false,
+ },
names_custom = function()
local colors = require("kanagawa.colors").setup()
return colors.palette
@@ -56,6 +65,11 @@ blue gray lightblue gray100 white gold blue
Blue LightBlue Gray100 White
White
+Names options: casing, strip digits
+deepskyblue deepskyblue1
+DeepSkyBlue DeepSkyBlue2
+DEEPSKYBLUE DEEPSKYBLUE3
+
Extra names:
oniViolet oniViolet2 crystalBlue springViolet1 springViolet2 springBlue
lightBlue waveAqua2