Skip to content

Commit dc7216a

Browse files
authored
Fix/110 add names custom nonalpha chars to trie (norcalli#113)
* fix: fixes incorrect reference to sass name parser * feat(Trie): adds method to add additional valid characters to Trie index_lookup * chore: moves TODO comment which causes luadoc to skip function
1 parent 29ee3fa commit dc7216a

File tree

9 files changed

+80
-42
lines changed

9 files changed

+80
-42
lines changed

doc/colorizer.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,7 @@ LUA API *colorizer.sass-lua-api*
910910
Functions: ~
911911
|cleanup| - Cleanup sass variables and watch handlers
912912

913-
|name_parser| - Parse the given line for sass color names
913+
|parser| - Parse the given line for sass color names
914914
check for value in state[buf].definitions_all
915915

916916
|update_variables| - Parse the given lines for sass variabled and add to
@@ -925,7 +925,7 @@ cleanup({bufnr}) *colorizer.sass.cleanup*
925925

926926

927927

928-
name_parser({line}, {i}, {bufnr}) *colorizer.sass.name_parser*
928+
parser({line}, {i}, {bufnr}) *colorizer.sass.parser*
929929
Parse the given line for sass color names
930930
check for value in state[buf].definitions_all
931931

doc/modules/colorizer.sass.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ <h2><a href="#Functions">Functions</a></h2>
7575
<td class="summary">Cleanup sass variables and watch handlers</td>
7676
</tr>
7777
<tr>
78-
<td class="name" nowrap><a href="#name_parser">name_parser (line, i, bufnr)</a></td>
78+
<td class="name" nowrap><a href="#parser">parser (line, i, bufnr)</a></td>
7979
<td class="summary">Parse the given line for sass color names
8080
check for value in state[buf].definitions_all</td>
8181
</tr>
@@ -113,8 +113,8 @@ <h3>Parameters:</h3>
113113

114114
</dd>
115115
<dt>
116-
<a name = "name_parser"></a>
117-
<strong>name_parser (line, i, bufnr)</strong>
116+
<a name = "parser"></a>
117+
<strong>parser (line, i, bufnr)</strong>
118118
</dt>
119119
<dd>
120120
Parse the given line for sass color names

lua/colorizer/config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ function M.parse_buffer_options(options)
233233
end
234234
end
235235

236-
-- https://github.com/NvChad/nvim-colorizer.lua/issues/48
236+
-- https://github.com/catgoose/nvim-colorizer.lua/issues/48
237237
handle_alias("css", options, default)
238238
handle_alias("css_fn", options, default)
239239

lua/colorizer/matcher.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ local parsers = {
1515
hsl_function = require("colorizer.parser.hsl").parser,
1616
rgb_function = require("colorizer.parser.rgb").parser,
1717
rgba_hex = require("colorizer.parser.rgba_hex").parser,
18+
-- TODO: 2024-12-21 - Should this be moved into parsers module?
1819
sass_name = require("colorizer.sass").parser,
1920
}
2021

lua/colorizer/parser/names.lua

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ local function add_color(name, value)
3434
names_cache.color_trie:insert(name)
3535
end
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.
3955
local 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
7493
end
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.
78121
local 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

@@ -117,6 +147,7 @@ end
117147
-- @return number|nil, string|nil Length of match and hex value if found.
118148
function 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

lua/colorizer/sass.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ end
3737
---@param i number: Index of line from where to start parsing
3838
---@param bufnr number: Buffer number
3939
---@return number|nil, string|nil
40-
function M.name_parser(line, i, bufnr)
40+
function M.parser(line, i, bufnr)
4141
local variable_name = line:match("^%$([%w_-]+)", i)
4242
if variable_name then
4343
local rgb_hex = state[bufnr].definitions_all[variable_name]

lua/colorizer/trie.lua

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,9 @@ end
5454

5555
local total_char = 255
5656
local index_lookup = ffi.new("uint8_t[?]", total_char)
57-
local char_lookup = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-"
57+
local char_lookup = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
5858
do
5959
local b = string.byte
60-
local extra_char = {
61-
[b("-")] = true,
62-
}
6360
local byte = {
6461
["0"] = b("0"),
6562
["9"] = b("9"),
@@ -75,7 +72,6 @@ do
7572
index_lookup[i] = i - byte["A"] + 10
7673
elseif i >= byte["a"] and i <= byte["z"] then
7774
index_lookup[i] = i - byte["a"] + 10 + 26
78-
elseif extra_char[i] then
7975
else
8076
index_lookup[i] = total_char
8177
end
@@ -247,6 +243,20 @@ local function trie_to_string(trie)
247243
return table.concat(print_trie_table(as_table), "\n")
248244
end
249245

246+
local function trie_additional_chars(trie, chars)
247+
if trie == nil or type(chars) ~= "string" then
248+
return
249+
end
250+
for i = 1, #chars do
251+
local char = chars:sub(i, i)
252+
local char_byte = string.byte(char)
253+
if index_lookup[char_byte] == total_char then
254+
char_lookup = char_lookup .. char
255+
index_lookup[char_byte] = total_char + 1
256+
end
257+
end
258+
end
259+
250260
local Trie_mt = {
251261
__new = function(_, init)
252262
local trie = trie_create()
@@ -261,6 +271,7 @@ local Trie_mt = {
261271
longest_prefix = trie_longest_prefix,
262272
extend = trie_extend,
263273
destroy = trie_destroy,
274+
additional_chars = trie_additional_chars,
264275
},
265276
__tostring = trie_to_string,
266277
__gc = trie_destroy,

lua/colorizer/utils.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ end
6868
---@param byte number The byte to check.
6969
---@return boolean `true` if the byte is valid, otherwise `false`.
7070
function M.byte_is_valid_colorchar(byte)
71+
-- TODO: 2024-12-21 - Is this check required?
7172
return M.byte_is_alphanumeric(byte) or byte == ("-"):byte()
7273
end
7374

test/expect.lua

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,11 @@ local opts = {
66
lua = {
77
names = true,
88
names_custom = {
9-
-- names = "#1F4770",
10-
names = "#791497",
11-
cool = "#3F3347",
12-
lua = "#107d3c",
13-
["notcool"] = "#ee9240",
14-
redgreen = "#970000",
15-
asdf = "#234311",
16-
eeee = "#112238",
17-
-- lua
9+
red_purple = "#017dac",
10+
["red=green"] = "#3700c2",
11+
["green@blue"] = "#e9e240",
12+
["green!blue"] = "#a9e042",
13+
["green!!blue"] = "#09e392",
1814
},
1915
-- names_custom = function()
2016
-- local colors = require("kanagawa.colors").setup()
@@ -25,11 +21,6 @@ local opts = {
2521
buftypes = { "*", "!prompt", "!popup" },
2622
user_commands = true,
2723
user_default_options = {
28-
names_custom = {
29-
-- names = "#1F4770",
30-
names = "#1740F7",
31-
lua = "#7407F1",
32-
},
3324
names = false,
3425
RGB = true,
3526
RRGGBB = true,
@@ -69,6 +60,9 @@ Extra names:
6960
oniViolet oniViolet2 crystalBlue springViolet1 springViolet2 springBlue
7061
lightBlue waveAqua2
7162
63+
Additional names with non-alphanumeric characters
64+
red_purple red=green green@blue green!blue green!!blue
65+
7266
Hexadecimal:
7367
#RGB:
7468
#F0F

0 commit comments

Comments
 (0)