Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support custom name parser #67

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion lua/colorizer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
local buffer_utils = require "colorizer.buffer"
local clear_hl_cache = buffer_utils.clear_hl_cache
local rehighlight_buffer = buffer_utils.rehighlight
local clear_matcher_cache = require("colorizer.matcher").clear_cache

local utils = require "colorizer.utils"
local merge = utils.merge
Expand Down Expand Up @@ -173,6 +174,7 @@ local USER_DEFAULT_OPTIONS = {
sass = { enable = false, parsers = { css = true } },
virtualtext = "■",
always_update = false,
custom = nil,
}

local OPTIONS = { buf = {}, file = {} }
Expand Down Expand Up @@ -480,7 +482,11 @@ function colorizer.setup(config)
all = { file = false, buf = false },
default_options = user_default_options,
}
BUFFER_OPTIONS, BUFFER_LOCAL = {}, {}

-- clear autocmds
pcall(api.nvim_del_augroup_by_name, AUGROUP_NAME)

BUFFER_LOCAL = {}

local function COLORIZER_SETUP_HOOK(typ)
local filetype = vim.bo.filetype
Expand Down Expand Up @@ -575,6 +581,19 @@ function colorizer.setup(config)
require("colorizer").clear_highlight_cache()
end,
})

-- if old buffers, reattach them with new settings
local old_buffers = vim.deepcopy(BUFFER_OPTIONS)
BUFFER_OPTIONS = {}
if old_buffers then
clear_matcher_cache()
clear_hl_cache()
for buf, _ in pairs(old_buffers) do
colorizer.detach_from_buffer(buf)
colorizer.attach_to_buffer(buf)
end
end

end

--- Return the currently active buffer options.
Expand Down
7 changes: 6 additions & 1 deletion lua/colorizer/matcher.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ function matcher.compile(matchers, matchers_trie)
end

local MATCHER_CACHE = {}

function matcher.clear_cache()
MATCHER_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
Expand Down Expand Up @@ -112,7 +117,7 @@ function matcher.make(options)
matchers.max_prefix_length = 0

if enable_names then
matchers.color_name_parser = { tailwind = options.tailwind }
matchers.color_name_parser = { tailwind = options.tailwind, custom = options.custom }
end

if enable_sass then
Expand Down
10 changes: 10 additions & 0 deletions lua/colorizer/parser/names.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,19 @@ function parser.name_parser(line, i, opts)
end
end
end

TAILWIND_ENABLED = opts.tailwind
end

if opts and opts.custom then
for k, v in pairs(opts.custom) do
COLOR_NAME_MINLEN = COLOR_NAME_MINLEN and min(#k, COLOR_NAME_MINLEN) or #k
COLOR_NAME_MAXLEN = COLOR_NAME_MAXLEN and max(#k, COLOR_NAME_MAXLEN) or #k
COLOR_MAP[k] = string.sub(v, 2)
COLOR_TRIE:insert(k)
end
end

if #line < i + COLOR_NAME_MINLEN - 1 then
return
end
Expand Down
11 changes: 6 additions & 5 deletions lua/colorizer/trie.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ local ffi = require "ffi"
ffi.cdef [[
struct Trie {
bool is_leaf;
struct Trie* character[62];
struct Trie* character[63];
};
void *malloc(size_t size);
void free(void *ptr);
Expand All @@ -41,7 +41,7 @@ local function trie_destroy(trie)
if trie == nil then
return
end
for i = 0, 61 do
for i = 0, 62 do
local child = trie.character[i]
if child ~= nil then
trie_destroy(child)
Expand All @@ -52,11 +52,12 @@ end

local total_char = 255
local INDEX_LOOKUP_TABLE = ffi.new("uint8_t[?]", total_char)
local CHAR_LOOKUP_TABLE = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-"
local CHAR_LOOKUP_TABLE = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_"
do
local b = string.byte
local extra_char = {
[b "-"] = true,
[b "_"] = true,
}
local byte = { ["0"] = b "0", ["9"] = b "9", ["a"] = b "a", ["A"] = b "A", ["z"] = b "z", ["Z"] = b "Z" }
for i = 0, total_char do
Expand Down Expand Up @@ -156,7 +157,7 @@ end
--- Printing utilities

local function index_to_char(index)
if index < 0 or index > 61 then
if index < 0 or index > 62 then
return
end
return CHAR_LOOKUP_TABLE:sub(index + 1, index + 1)
Expand All @@ -167,7 +168,7 @@ local function trie_as_table(trie)
return nil
end
local children = {}
for i = 0, 61 do
for i = 0, 62 do
local child = trie.character[i]
if child ~= nil then
local child_table = trie_as_table(child)
Expand Down
2 changes: 1 addition & 1 deletion lua/colorizer/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ end
---@param byte number
---@return boolean
function utils.byte_is_valid_colorchar(byte)
return utils.byte_is_alphanumeric(byte) or byte == ("-"):byte()
return utils.byte_is_alphanumeric(byte) or byte == ("-"):byte() or byte == ("_"):byte()
end

---Count the number of character in a string
Expand Down