diff --git a/lua/colorizer.lua b/lua/colorizer.lua index f61e480..143b9c3 100644 --- a/lua/colorizer.lua +++ b/lua/colorizer.lua @@ -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 @@ -173,6 +174,7 @@ local USER_DEFAULT_OPTIONS = { sass = { enable = false, parsers = { css = true } }, virtualtext = "■", always_update = false, + custom = nil, } local OPTIONS = { buf = {}, file = {} } @@ -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 @@ -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. diff --git a/lua/colorizer/matcher.lua b/lua/colorizer/matcher.lua index 1f3eede..f9efd9c 100644 --- a/lua/colorizer/matcher.lua +++ b/lua/colorizer/matcher.lua @@ -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 @@ -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 diff --git a/lua/colorizer/parser/names.lua b/lua/colorizer/parser/names.lua index 6fc7043..55bac5e 100644 --- a/lua/colorizer/parser/names.lua +++ b/lua/colorizer/parser/names.lua @@ -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 diff --git a/lua/colorizer/trie.lua b/lua/colorizer/trie.lua index a7d9994..64c71dc 100644 --- a/lua/colorizer/trie.lua +++ b/lua/colorizer/trie.lua @@ -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); @@ -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) @@ -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 @@ -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) @@ -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) diff --git a/lua/colorizer/utils.lua b/lua/colorizer/utils.lua index ae590cd..29c8046 100644 --- a/lua/colorizer/utils.lua +++ b/lua/colorizer/utils.lua @@ -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