Skip to content

Commit

Permalink
Fix/110 add names custom nonalpha chars to trie (#113)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
catgoose authored Dec 22, 2024
1 parent 29ee3fa commit dc7216a
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 42 deletions.
4 changes: 2 additions & 2 deletions doc/colorizer.txt
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ LUA API *colorizer.sass-lua-api*
Functions: ~
|cleanup| - Cleanup sass variables and watch handlers

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

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



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

Expand Down
6 changes: 3 additions & 3 deletions doc/modules/colorizer.sass.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ <h2><a href="#Functions">Functions</a></h2>
<td class="summary">Cleanup sass variables and watch handlers</td>
</tr>
<tr>
<td class="name" nowrap><a href="#name_parser">name_parser (line, i, bufnr)</a></td>
<td class="name" nowrap><a href="#parser">parser (line, i, bufnr)</a></td>
<td class="summary">Parse the given line for sass color names
check for value in state[buf].definitions_all</td>
</tr>
Expand Down Expand Up @@ -113,8 +113,8 @@ <h3>Parameters:</h3>

</dd>
<dt>
<a name = "name_parser"></a>
<strong>name_parser (line, i, bufnr)</strong>
<a name = "parser"></a>
<strong>parser (line, i, bufnr)</strong>
</dt>
<dd>
Parse the given line for sass color names
Expand Down
2 changes: 1 addition & 1 deletion lua/colorizer/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ function M.parse_buffer_options(options)
end
end

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

Expand Down
1 change: 1 addition & 0 deletions lua/colorizer/matcher.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ local parsers = {
hsl_function = require("colorizer.parser.hsl").parser,
rgb_function = require("colorizer.parser.rgb").parser,
rgba_hex = require("colorizer.parser.rgba_hex").parser,
-- TODO: 2024-12-21 - Should this be moved into parsers module?
sass_name = require("colorizer.sass").parser,
}

Expand Down
63 changes: 47 additions & 16 deletions lua/colorizer/parser/names.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ local function add_color(name, value)
names_cache.color_trie:insert(name)
end

--- Extract non-alphanumeric characters to add as a valid index in the Trie
-- @param tbl table The table to extract non-alphanumeric characters from.
local function extract_non_alphanum_keys(tbl)
local non_alphanum_chars = {}
for key, _ in pairs(tbl) do
for char in key:gmatch("[^%w]") do
non_alphanum_chars[char] = true
end
end
local result = ""
for char in pairs(non_alphanum_chars) do
result = result .. char
end
return result
end

--- Handles additional color names provided as a table or function.
-- @param names_custom table|function|nil Additional color names to add.
local function handle_names_custom(names_custom)
Expand All @@ -42,7 +58,6 @@ local function handle_names_custom(names_custom)
end

local extra_data = {}

if type(names_custom) == "table" then
extra_data = names_custom
elseif type(names_custom) == "function" then
Expand All @@ -57,6 +72,10 @@ local function handle_names_custom(names_custom)
end
end

-- Add additional characters found in names_custom keys
local additonal_chars = extract_non_alphanum_keys(names_custom)
names_cache.color_trie:additional_chars(additonal_chars)

for name, hex in pairs(extra_data) do
if type(hex) == "string" then
local normalized_hex = hex:gsub("^#", ""):gsub("%s", "")
Expand All @@ -73,6 +92,30 @@ local function handle_names_custom(names_custom)
end
end

--- Handles Tailwind CSS colors and adds them to the Trie and map.
local function handle_tailwind()
names_cache.color_trie:additional_chars("-")
local tailwind = require("colorizer.tailwind_colors")
for name, hex in pairs(tailwind.colors) do
for _, prefix in ipairs(tailwind.prefixes) do
add_color(prefix .. "-" .. name, hex)
end
end
end

--- Handles Vim's color map and adds colors to the Trie and map.
local function handle_names()
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
local rgb_hex = tohex(value, 6)
add_color(name, rgb_hex)
if names_cache.color_name_settings.lowercase then
add_color(name:lower(), rgb_hex)
end
end
end
end

--- Populates the Trie and map with colors based on options.
-- @param opts table Configuration options for color names and Tailwind CSS.
local function populate_colors(opts)
Expand All @@ -82,25 +125,12 @@ local function populate_colors(opts)

-- Add Vim's color map
if opts.color_names then
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
local rgb_hex = tohex(value, 6)
add_color(name, rgb_hex)
if names_cache.color_name_settings.lowercase then
add_color(name:lower(), rgb_hex)
end
end
end
handle_names()
end

-- Add Tailwind colors
if opts.tailwind then
local tailwind = require("colorizer.tailwind_colors")
for name, hex in pairs(tailwind.colors) do
for _, prefix in ipairs(tailwind.prefixes) do
add_color(prefix .. "-" .. name, hex)
end
end
handle_tailwind()
end
names_cache.tailwind_enabled = opts.tailwind

Expand All @@ -117,6 +147,7 @@ end
-- @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
populate_colors(opts)
end

Expand Down
2 changes: 1 addition & 1 deletion lua/colorizer/sass.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ end
---@param i number: Index of line from where to start parsing
---@param bufnr number: Buffer number
---@return number|nil, string|nil
function M.name_parser(line, i, bufnr)
function M.parser(line, i, bufnr)
local variable_name = line:match("^%$([%w_-]+)", i)
if variable_name then
local rgb_hex = state[bufnr].definitions_all[variable_name]
Expand Down
21 changes: 16 additions & 5 deletions lua/colorizer/trie.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,9 @@ end

local total_char = 255
local index_lookup = ffi.new("uint8_t[?]", total_char)
local char_lookup = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-"
local char_lookup = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
do
local b = string.byte
local extra_char = {
[b("-")] = true,
}
local byte = {
["0"] = b("0"),
["9"] = b("9"),
Expand All @@ -75,7 +72,6 @@ do
index_lookup[i] = i - byte["A"] + 10
elseif i >= byte["a"] and i <= byte["z"] then
index_lookup[i] = i - byte["a"] + 10 + 26
elseif extra_char[i] then
else
index_lookup[i] = total_char
end
Expand Down Expand Up @@ -247,6 +243,20 @@ local function trie_to_string(trie)
return table.concat(print_trie_table(as_table), "\n")
end

local function trie_additional_chars(trie, chars)
if trie == nil or type(chars) ~= "string" then
return
end
for i = 1, #chars do
local char = chars:sub(i, i)
local char_byte = string.byte(char)
if index_lookup[char_byte] == total_char then
char_lookup = char_lookup .. char
index_lookup[char_byte] = total_char + 1
end
end
end

local Trie_mt = {
__new = function(_, init)
local trie = trie_create()
Expand All @@ -261,6 +271,7 @@ local Trie_mt = {
longest_prefix = trie_longest_prefix,
extend = trie_extend,
destroy = trie_destroy,
additional_chars = trie_additional_chars,
},
__tostring = trie_to_string,
__gc = trie_destroy,
Expand Down
1 change: 1 addition & 0 deletions lua/colorizer/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ end
---@param byte number The byte to check.
---@return boolean `true` if the byte is valid, otherwise `false`.
function M.byte_is_valid_colorchar(byte)
-- TODO: 2024-12-21 - Is this check required?
return M.byte_is_alphanumeric(byte) or byte == ("-"):byte()
end

Expand Down
22 changes: 8 additions & 14 deletions test/expect.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@ local opts = {
lua = {
names = true,
names_custom = {
-- names = "#1F4770",
names = "#791497",
cool = "#3F3347",
lua = "#107d3c",
["notcool"] = "#ee9240",
redgreen = "#970000",
asdf = "#234311",
eeee = "#112238",
-- lua
red_purple = "#017dac",
["red=green"] = "#3700c2",
["green@blue"] = "#e9e240",
["green!blue"] = "#a9e042",
["green!!blue"] = "#09e392",
},
-- names_custom = function()
-- local colors = require("kanagawa.colors").setup()
Expand All @@ -25,11 +21,6 @@ local opts = {
buftypes = { "*", "!prompt", "!popup" },
user_commands = true,
user_default_options = {
names_custom = {
-- names = "#1F4770",
names = "#1740F7",
lua = "#7407F1",
},
names = false,
RGB = true,
RRGGBB = true,
Expand Down Expand Up @@ -69,6 +60,9 @@ Extra names:
oniViolet oniViolet2 crystalBlue springViolet1 springViolet2 springBlue
lightBlue waveAqua2
Additional names with non-alphanumeric characters
red_purple red=green green@blue green!blue green!!blue
Hexadecimal:
#RGB:
#F0F
Expand Down

0 comments on commit dc7216a

Please sign in to comment.