@@ -34,6 +34,22 @@ local function add_color(name, value)
3434 names_cache .color_trie :insert (name )
3535end
3636
37+ --- Extract non-alphanumeric characters to add as a valid index in the Trie
38+ -- @param tbl table The table to extract non-alphanumeric characters from.
39+ local function extract_non_alphanum_keys (tbl )
40+ local non_alphanum_chars = {}
41+ for key , _ in pairs (tbl ) do
42+ for char in key :gmatch (" [^%w]" ) do
43+ non_alphanum_chars [char ] = true
44+ end
45+ end
46+ local result = " "
47+ for char in pairs (non_alphanum_chars ) do
48+ result = result .. char
49+ end
50+ return result
51+ end
52+
3753--- Handles additional color names provided as a table or function.
3854-- @param names_custom table|function|nil Additional color names to add.
3955local function handle_names_custom (names_custom )
@@ -42,7 +58,6 @@ local function handle_names_custom(names_custom)
4258 end
4359
4460 local extra_data = {}
45-
4661 if type (names_custom ) == " table" then
4762 extra_data = names_custom
4863 elseif type (names_custom ) == " function" then
@@ -57,6 +72,10 @@ local function handle_names_custom(names_custom)
5772 end
5873 end
5974
75+ -- Add additional characters found in names_custom keys
76+ local additonal_chars = extract_non_alphanum_keys (names_custom )
77+ names_cache .color_trie :additional_chars (additonal_chars )
78+
6079 for name , hex in pairs (extra_data ) do
6180 if type (hex ) == " string" then
6281 local normalized_hex = hex :gsub (" ^#" , " " ):gsub (" %s" , " " )
@@ -73,6 +92,30 @@ local function handle_names_custom(names_custom)
7392 end
7493end
7594
95+ --- Handles Tailwind CSS colors and adds them to the Trie and map.
96+ local function handle_tailwind ()
97+ names_cache .color_trie :additional_chars (" -" )
98+ local tailwind = require (" colorizer.tailwind_colors" )
99+ for name , hex in pairs (tailwind .colors ) do
100+ for _ , prefix in ipairs (tailwind .prefixes ) do
101+ add_color (prefix .. " -" .. name , hex )
102+ end
103+ end
104+ end
105+
106+ --- Handles Vim's color map and adds colors to the Trie and map.
107+ local function handle_names ()
108+ for name , value in pairs (vim .api .nvim_get_color_map ()) do
109+ if not (names_cache .color_name_settings .strip_digits and name :match (" %d+$" )) then
110+ local rgb_hex = tohex (value , 6 )
111+ add_color (name , rgb_hex )
112+ if names_cache .color_name_settings .lowercase then
113+ add_color (name :lower (), rgb_hex )
114+ end
115+ end
116+ end
117+ end
118+
76119--- Populates the Trie and map with colors based on options.
77120-- @param opts table Configuration options for color names and Tailwind CSS.
78121local function populate_colors (opts )
@@ -82,25 +125,12 @@ local function populate_colors(opts)
82125
83126 -- Add Vim's color map
84127 if opts .color_names then
85- for name , value in pairs (vim .api .nvim_get_color_map ()) do
86- if not (names_cache .color_name_settings .strip_digits and name :match (" %d+$" )) then
87- local rgb_hex = tohex (value , 6 )
88- add_color (name , rgb_hex )
89- if names_cache .color_name_settings .lowercase then
90- add_color (name :lower (), rgb_hex )
91- end
92- end
93- end
128+ handle_names ()
94129 end
95130
96131 -- Add Tailwind colors
97132 if opts .tailwind then
98- local tailwind = require (" colorizer.tailwind_colors" )
99- for name , hex in pairs (tailwind .colors ) do
100- for _ , prefix in ipairs (tailwind .prefixes ) do
101- add_color (prefix .. " -" .. name , hex )
102- end
103- end
133+ handle_tailwind ()
104134 end
105135 names_cache .tailwind_enabled = opts .tailwind
106136
117147-- @return number|nil, string|nil Length of match and hex value if found.
118148function M .parser (line , i , opts )
119149 if not names_cache .color_trie or opts .tailwind ~= names_cache .tailwind_enabled then
150+ -- TODO: 2024-12-21 - Ensure that this is not being called too many times
120151 populate_colors (opts )
121152 end
122153
0 commit comments