diff options
Diffstat (limited to 'lua')
-rw-r--r-- | lua/colorizer.lua | 117 | ||||
-rw-r--r-- | lua/colorizer/buffer_utils.lua | 122 | ||||
-rw-r--r-- | lua/colorizer/color_utils.lua | 23 | ||||
-rw-r--r-- | lua/colorizer/matcher_utils.lua | 12 | ||||
-rw-r--r-- | lua/colorizer/tailwind_colors.lua | 250 | ||||
-rw-r--r-- | lua/colorizer/trie.lua | 17 | ||||
-rw-r--r-- | lua/colorizer/utils.lua | 20 |
7 files changed, 486 insertions, 75 deletions
diff --git a/lua/colorizer.lua b/lua/colorizer.lua index f3726c4..f54f689 100644 --- a/lua/colorizer.lua +++ b/lua/colorizer.lua @@ -87,23 +87,53 @@ local AUGROUP_ID local AUGROUP_NAME = "ColorizerSetup" -- buffer specific options given in setup local BUFFER_OPTIONS = {} --- store boolean for buffer if it is initialzed -local BUFFER_INIT = {} --- store buffer local autocmd(s) id -local BUFFER_AUTOCMDS = {} +-- buffer local options created after setup +local BUFFER_LOCAL = {} +---defaults options. +--<pre> +-- user_default_options = { +-- RGB = true, -- #RGB hex codes +-- RRGGBB = true, -- #RRGGBB hex codes +-- names = true, -- "Name" codes like Blue or blue +-- RRGGBBAA = false, -- #RRGGBBAA hex codes +-- AARRGGBB = false, -- 0xAARRGGBB hex codes +-- rgb_fn = false, -- CSS rgb() and rgba() functions +-- hsl_fn = false, -- CSS hsl() and hsla() functions +-- css = false, -- Enable all CSS features: rgb_fn, hsl_fn, names, RGB, RRGGBB +-- css_fn = false, -- Enable all CSS *functions*: rgb_fn, hsl_fn +-- -- Available modes for `mode`: foreground, background, virtualtext +-- mode = "background", -- Set the display mode. +-- -- Available methods are false / "normal" / "lsp" / "both" +-- tailwind = false -- Enable tailwind colors +-- virtualtext = "■", +-- } +--</pre> +---@table user_default_options +--@field RGB boolean +--@field RRGGBB boolean +--@field names boolean +--@field RRGGBBAA boolean +--@field AARRGGBB boolean +--@field rgb_fn boolean +--@field hsl_fn boolean +--@field css boolean +--@field css_fn boolean +--@field mode string +--@field tailwind boolean|string +--@field virtualtext string local USER_DEFAULT_OPTIONS = { - RGB = true, -- #RGB hex codes - RRGGBB = true, -- #RRGGBB hex codes - names = true, -- "Name" codes like Blue or blue - RRGGBBAA = false, -- #RRGGBBAA hex codes - AARRGGBB = false, -- 0xAARRGGBB hex codes - rgb_fn = false, -- CSS rgb() and rgba() functions - hsl_fn = false, -- CSS hsl() and hsla() functions - css = false, -- Enable all CSS features: rgb_fn, hsl_fn, names, RGB, RRGGBB - css_fn = false, -- Enable all CSS *functions*: rgb_fn, hsl_fn - -- Available modes: foreground, background, virtualtext - mode = "background", -- Set the display mode. + RGB = true, + RRGGBB = true, + names = true, + RRGGBBAA = false, + AARRGGBB = false, + rgb_fn = false, + hsl_fn = false, + css = false, + css_fn = false, + mode = "background", + tailwind = "lsp", virtualtext = "■", } @@ -158,12 +188,22 @@ local function detach_from_buffer(buf, ns) end clear_namespace(buf, ns or DEFAULT_NAMESPACE, 0, -1) - for _, id in ipairs(BUFFER_AUTOCMDS[buf] or {}) do - pcall(api.nvim_del_autocmd, id) + if BUFFER_LOCAL[buf] then + if BUFFER_LOCAL[buf].__tailwind_ns then + clear_namespace(buf, BUFFER_LOCAL[buf].__tailwind_ns, 0, -1) + if type(BUFFER_LOCAL[buf].__tailwind_detach) == "function" then + BUFFER_LOCAL[buf].__tailwind_detach() + end + end + + for _, id in ipairs(BUFFER_LOCAL[buf].__autocmds or {}) do + pcall(api.nvim_del_autocmd, id) + end + + BUFFER_LOCAL[buf].__autocmds = nil end -- because now the buffer is not visible, so delete its information BUFFER_OPTIONS[buf] = nil - BUFFER_AUTOCMDS[buf] = nil end ---Attach to a buffer and continuously highlight changes. @@ -191,11 +231,15 @@ local function attach_to_buffer(buf, options, typ) end BUFFER_OPTIONS[buf] = options - rehighlight_buffer(buf, options) - BUFFER_INIT[buf] = true + BUFFER_LOCAL[buf] = BUFFER_LOCAL[buf] or {} + local tailwind_ns, tailwind_detach = rehighlight_buffer(buf, options) + BUFFER_LOCAL[buf].__tailwind_ns = tailwind_ns + BUFFER_LOCAL[buf].__tailwind_detach = tailwind_detach - if BUFFER_AUTOCMDS[buf] then + BUFFER_LOCAL[buf].__init = true + + if BUFFER_LOCAL[buf].__autocmds then return end @@ -208,7 +252,7 @@ local function attach_to_buffer(buf, options, typ) callback = function() -- only reload if it was not disabled using detach_from_buffer if BUFFER_OPTIONS[buf] then - rehighlight_buffer(buf, options) + rehighlight_buffer(buf, options, BUFFER_LOCAL[buf]) end end, }) @@ -219,7 +263,7 @@ local function attach_to_buffer(buf, options, typ) callback = function() -- only reload if it was not disabled using detach_from_buffer if BUFFER_OPTIONS[buf] then - rehighlight_buffer(buf, options) + rehighlight_buffer(buf, options, BUFFER_LOCAL[buf]) end end, }) @@ -231,11 +275,12 @@ local function attach_to_buffer(buf, options, typ) if BUFFER_OPTIONS[buf] then detach_from_buffer(buf) end - BUFFER_INIT[buf] = nil + BUFFER_LOCAL[buf].__init = nil end, }) - BUFFER_AUTOCMDS[buf] = autocmds + BUFFER_LOCAL[buf].__autocmds = autocmds + BUFFER_LOCAL[buf].__augroup_id = au_group_id end ---Easy to use function if you want the full setup without fine grained control. @@ -251,24 +296,12 @@ end --<pre> -- require("colorizer").setup { -- filetypes = { "*" }, --- user_default_options = { --- RGB = true, -- #RGB hex codes --- RRGGBB = true, -- #RRGGBB hex codes --- names = true, -- "Name" codes like Blue or blue --- RRGGBBAA = false, -- #RRGGBBAA hex codes --- AARRGGBB = false, -- 0xAARRGGBB hex codes --- rgb_fn = false, -- CSS rgb() and rgba() functions --- hsl_fn = false, -- CSS hsl() and hsla() functions --- css = false, -- Enable all CSS features: rgb_fn, hsl_fn, names, RGB, RRGGBB --- css_fn = false, -- Enable all CSS *functions*: rgb_fn, hsl_fn --- -- Available modes for `mode`: foreground, background, virtualtext --- mode = "background", -- Set the display mode. --- virtualtext = "■", --- }, +-- user_default_options, -- -- all the sub-options of filetypes apply to buftypes -- buftypes = {}, -- } --</pre> +--For all user_default_options, see |user_default_options| ---@param config table: Config containing above parameters. ---@usage `require'colorizer'.setup()` local function setup(config) @@ -297,13 +330,15 @@ local function setup(config) local filetype = vim.bo.filetype local buftype = vim.bo.buftype local buf = current_buf() + BUFFER_LOCAL[buf] = BUFFER_LOCAL[buf] or {} + if SETUP_SETTINGS.exclusions.file[filetype] or SETUP_SETTINGS.exclusions.buf[buftype] then -- when a filetype is disabled but buftype is enabled, it can Attach in -- some cases, so manually detach if BUFFER_OPTIONS[buf] then detach_from_buffer(buf) end - BUFFER_INIT[buf] = nil + BUFFER_LOCAL[buf].__init = nil return end @@ -326,7 +361,7 @@ local function setup(config) -- this should ideally be triggered one time per buffer -- but BufWinEnter also triggers for split formation -- but we don't want that so add a check using local buffer variable - if not BUFFER_INIT[buf] then + if not BUFFER_LOCAL[buf].__init then attach_to_buffer(buf, options, typ) end end diff --git a/lua/colorizer/buffer_utils.lua b/lua/colorizer/buffer_utils.lua index 0f8517f..d0b0faa 100644 --- a/lua/colorizer/buffer_utils.lua +++ b/lua/colorizer/buffer_utils.lua @@ -13,10 +13,14 @@ local color_is_bright = color_utils.color_is_bright local matcher_utils = require "colorizer.matcher_utils" local make_matcher = matcher_utils.make_matcher +local highlight_buffer, rehighlight_buffer +local BUFFER_LINES = {} --- Default namespace used in `highlight_buffer` and `colorizer.attach_to_buffer`. -- @see highlight_buffer -- @see colorizer.attach_to_buffer local DEFAULT_NAMESPACE = create_namespace "colorizer" +-- use a different namespace for tailwind as will be cleared if kept in Default namespace +local DEFAULT_NAMESPACE_TAILWIND = create_namespace "colorizer_tailwind" local HIGHLIGHT_NAME_PREFIX = "colorizer" --- Highlight mode which will be use to render the colour local HIGHLIGHT_MODE_NAMES = { @@ -72,6 +76,13 @@ local function create_highlight(rgb_hex, options) end local function add_highlight(options, buf, ns, data) + clear_namespace( + buf, + ns, + BUFFER_LINES[buf]["old_min"] or BUFFER_LINES[buf]["min"], + BUFFER_LINES[buf]["old_max"] or BUFFER_LINES[buf]["max"] + ) + if vim.tbl_contains({ "foreground", "background" }, options.mode) then for linenr, hls in pairs(data) do for _, hl in ipairs(hls) do @@ -90,6 +101,37 @@ local function add_highlight(options, buf, ns, data) end end +local function highlight_buffer_tailwind(buf, ns, mode, options) + -- it can take some time to actually fetch the results + -- on top of that, tailwindcss is quite slow in neovim + vim.defer_fn(function() + local opts = { textDocument = vim.lsp.util.make_text_document_params() } + --@local + ---@diagnostic disable-next-line: param-type-mismatch + vim.lsp.buf_request(buf, "textDocument/documentColor", opts, function(err, results, _, _) + if err == nil and results ~= nil then + local datas = {} + for _, color in pairs(results) do + local cur_line = color.range.start.line + local r, g, b, a = color.color.red or 0, color.color.green or 0, color.color.blue or 0, color.color.alpha or 0 + local rgb_hex = string.format("%02x%02x%02x", r * a * 255, g * a * 255, b * a * 255) + local name = create_highlight(rgb_hex, mode) + local first_col = color.range.start.character + local end_col = color.range["end"].character + + local d = datas[cur_line] or {} + table.insert(d, { name = name, range = { first_col, end_col } }) + datas[cur_line] = d + end + add_highlight(options, buf, ns, datas) + end + end) + end, 10) +end + +local TW_LSP_ATTACHED = {} +local TW_LSP_AU_CREATED = {} +local TW_LSP_AU_ID = {} --- Highlight the buffer region. -- Highlight starting from `line_start` (0-indexed) for each line described by `lines` in the -- buffer `buf` and attach it to the namespace `ns`. @@ -98,7 +140,9 @@ end ---@param lines table: the lines to highlight from the buffer. ---@param line_start number: line_start should be 0-indexed ---@param options table: Configuration options as described in `setup` -local function highlight_buffer(buf, ns, lines, line_start, options) +---@param options_local table: Buffer local variables +---@return nil|boolean|number,function|nil +function highlight_buffer(buf, ns, lines, line_start, options, options_local) if buf == 0 or buf == nil then buf = api.nvim_get_current_buf() end @@ -129,19 +173,53 @@ local function highlight_buffer(buf, ns, lines, line_start, options) end end add_highlight(options, buf, ns, data) -end -local BUFFER_LINES = {} ---- Rehighlight the buffer if colorizer is active ----@param buf number: Buffer number ----@param options table: Buffer options -local function rehighlight_buffer(buf, options) - if buf == 0 or buf == nil then - buf = api.nvim_get_current_buf() + if not options.tailwind or (options.tailwind ~= "lsp" and options.tailwind ~= "both") then + return end - local ns = DEFAULT_NAMESPACE + -- create the autocmds so tailwind colours only activate when tailwindcss lsp is active + if not TW_LSP_AU_CREATED[buf] then + TW_LSP_AU_ID[buf] = {} + TW_LSP_AU_ID[buf][1] = api.nvim_create_autocmd("LspAttach", { + group = options_local.__augroup_id, + buffer = buf, + callback = function(args) + local ok, client = pcall(vim.lsp.get_client_by_id, args.data.client_id) + if ok then + if client.name == "tailwindcss" and client.supports_method "textDocument/documentColor" then + highlight_buffer_tailwind(buf, DEFAULT_NAMESPACE_TAILWIND, mode, options) + TW_LSP_ATTACHED[buf] = true + end + end + end, + }) + local function del_tailwind_stuff() + pcall(api.nvim_del_autocmd, TW_LSP_AU_ID[buf][1]) + pcall(api.nvim_del_autocmd, TW_LSP_AU_ID[buf][2]) + TW_LSP_ATTACHED[buf], TW_LSP_AU_CREATED[buf], TW_LSP_AU_ID[buf] = nil, nil, nil + end + -- make sure the autocmds are deleted after lsp server is closed + TW_LSP_AU_ID[buf][2] = api.nvim_create_autocmd("LspDetach", { + group = options_local.__augroup_id, + buffer = buf, + callback = function() + del_tailwind_stuff() + clear_namespace(buf, DEFAULT_NAMESPACE_TAILWIND, 0, -1) + end, + }) + TW_LSP_AU_CREATED[buf] = true + return DEFAULT_NAMESPACE_TAILWIND, del_tailwind_stuff + end + -- only try to do tailwindcss highlight if lsp is attached + if TW_LSP_ATTACHED[buf] then + highlight_buffer_tailwind(buf, DEFAULT_NAMESPACE_TAILWIND, mode, options) + end +end + +-- get the amount lines to highlight +local function getrow(buf) if not BUFFER_LINES[buf] then BUFFER_LINES[buf] = {} end @@ -176,15 +254,31 @@ local function rehighlight_buffer(buf, options) max = new_max end end - min = min or new_min max = max or new_max - clear_namespace(buf, ns, min, max) - local lines = buf_get_lines(buf, min, max, false) - highlight_buffer(buf, ns, lines, min, options) -- store current window position to be used later to incremently highlight BUFFER_LINES[buf]["max"] = new_max BUFFER_LINES[buf]["min"] = new_min + BUFFER_LINES[buf]["old_max"] = max + BUFFER_LINES[buf]["old_min"] = min + return min, max +end + +--- Rehighlight the buffer if colorizer is active +---@param buf number: Buffer number +---@param options table: Buffer options +---@param options_local table|nil: Buffer local variables +---@return nil|boolean|number,function|nil +function rehighlight_buffer(buf, options, options_local) + if buf == 0 or buf == nil then + buf = api.nvim_get_current_buf() + end + + local ns = DEFAULT_NAMESPACE + + local min, max = getrow(buf) + local lines = buf_get_lines(buf, min, max, false) + return highlight_buffer(buf, ns, lines, min, options, options_local or {}) end --- @export diff --git a/lua/colorizer/color_utils.lua b/lua/colorizer/color_utils.lua index 433cea7..b90c108 100644 --- a/lua/colorizer/color_utils.lua +++ b/lua/colorizer/color_utils.lua @@ -60,13 +60,15 @@ local COLOR_MAP local COLOR_TRIE local COLOR_NAME_MINLEN, COLOR_NAME_MAXLEN local COLOR_NAME_SETTINGS = { lowercase = true, strip_digits = false } +local TAILWIND_ENABLED = false --- Grab all the colour values from `vim.api.nvim_get_color_map` and create a lookup table. -- COLOR_MAP is used to store the colour values ---@param line string: Line to parse ---@param i number: Index of line from where to start parsing -local function color_name_parser(line, i) +---@param opts table: Currently contains whether tailwind is enabled or not +local function color_name_parser(line, i, opts) --- Setup the COLOR_MAP and COLOR_TRIE - if not COLOR_TRIE then + if not COLOR_TRIE or opts.tailwind ~= TAILWIND_ENABLED then COLOR_MAP = {} COLOR_TRIE = Trie() for k, v in pairs(api.nvim_get_color_map()) do @@ -83,8 +85,23 @@ local function color_name_parser(line, i) end end end + if opts and opts.tailwind then + if opts.tailwind == "normal" or opts.tailwind == "both" then + local tailwind = require "colorizer.tailwind_colors" + -- setup tailwind colors + for k, v in pairs(tailwind.colors) do + for _, pre in ipairs(tailwind.prefixes) do + local name = pre .. "-" .. k + COLOR_NAME_MINLEN = COLOR_NAME_MINLEN and min(#name, COLOR_NAME_MINLEN) or #name + COLOR_NAME_MAXLEN = COLOR_NAME_MAXLEN and max(#name, COLOR_NAME_MAXLEN) or #name + COLOR_MAP[name] = v + COLOR_TRIE:insert(name) + end + end + end + end + TAILWIND_ENABLED = opts.tailwind end - if #line < i + COLOR_NAME_MINLEN - 1 then return end diff --git a/lua/colorizer/matcher_utils.lua b/lua/colorizer/matcher_utils.lua index 84ddece..11e56f6 100644 --- a/lua/colorizer/matcher_utils.lua +++ b/lua/colorizer/matcher_utils.lua @@ -34,12 +34,14 @@ local function compile_matcher(matchers, matchers_trie) local prefix = trie:longest_prefix(line, i) if prefix then local fn = "_" .. prefix - return parser[fn](line, i, matchers[fn]) + if parser[fn] then + return parser[fn](line, i, matchers[fn]) + end end -- Colour names if matchers.color_name_parser then - return color_name_parser(line, i) + return color_name_parser(line, i, matchers.color_name_parser) end end return parse_fn @@ -53,6 +55,7 @@ local MATCHER_CACHE = {} ---@return function|boolean: function which will just parse the line for enabled parsers local function make_matcher(options) local enable_names = options.css or options.names + local enable_tailwind = options.tailwind local enable_RGB = options.css or options.RGB local enable_RRGGBB = options.css or options.RRGGBB local enable_RRGGBBAA = options.css or options.RRGGBBAA @@ -68,6 +71,9 @@ local function make_matcher(options) + (enable_AARRGGBB and 1 or 4) + (enable_rgb and 1 or 5) + (enable_hsl and 1 or 6) + + (enable_tailwind == "normal" and 1 or 7) + + (enable_tailwind == "lsp" and 1 or 8) + + (enable_tailwind == "both" and 1 or 9) if matcher_key == 0 then return false @@ -83,7 +89,7 @@ local function make_matcher(options) matchers.max_prefix_length = 0 if enable_names then - matchers.color_name_parser = true + matchers.color_name_parser = { tailwind = options.tailwind } end local valid_lengths = { [3] = enable_RGB, [6] = enable_RRGGBB, [8] = enable_RRGGBBAA } diff --git a/lua/colorizer/tailwind_colors.lua b/lua/colorizer/tailwind_colors.lua new file mode 100644 index 0000000..aa99173 --- /dev/null +++ b/lua/colorizer/tailwind_colors.lua @@ -0,0 +1,250 @@ +-- Tailwind colors +return { + -- https://github.com/tailwindlabs/tailwindcss/raw/master/src/corePlugins.js + prefixes = { + "accent", + "bg", + "border", + "border-x", + "border-y", + "border-t", + "border-r", + "border-b", + "border-l", + "caret", + "decoration", + "divide", + "fill", + "from", + "shadow", + "stroke", + "text", + "to", + "via", + }, + -- Generated using https://github.com/tailwindlabs/tailwindcss/raw/master/src/public/colors.js + colors = { + ["black"] = "000", + ["white"] = "fff", + ["slate-50"] = "f8fafc", + ["slate-100"] = "f1f5f9", + ["slate-200"] = "e2e8f0", + ["slate-300"] = "cbd5e1", + ["slate-400"] = "94a3b8", + ["slate-500"] = "64748b", + ["slate-600"] = "475569", + ["slate-700"] = "334155", + ["slate-800"] = "1e293b", + ["slate-900"] = "0f172a", + ["gray-50"] = "f9fafb", + ["gray-100"] = "f3f4f6", + ["gray-200"] = "e5e7eb", + ["gray-300"] = "d1d5db", + ["gray-400"] = "9ca3af", + ["gray-500"] = "6b7280", + ["gray-600"] = "4b5563", + ["gray-700"] = "374151", + ["gray-800"] = "1f2937", + ["gray-900"] = "111827", + ["zinc-50"] = "fafafa", + ["zinc-100"] = "f4f4f5", + ["zinc-200"] = "e4e4e7", + ["zinc-300"] = "d4d4d8", + ["zinc-400"] = "a1a1aa", + ["zinc-500"] = "71717a", + ["zinc-600"] = "52525b", + ["zinc-700"] = "3f3f46", + ["zinc-800"] = "27272a", + ["zinc-900"] = "18181b", + ["neutral-50"] = "fafafa", + ["neutral-100"] = "f5f5f5", + ["neutral-200"] = "e5e5e5", + ["neutral-300"] = "d4d4d4", + ["neutral-400"] = "a3a3a3", + ["neutral-500"] = "737373", + ["neutral-600"] = "525252", + ["neutral-700"] = "404040", + ["neutral-800"] = "262626", + ["neutral-900"] = "171717", + ["stone-50"] = "fafaf9", + ["stone-100"] = "f5f5f4", + ["stone-200"] = "e7e5e4", + ["stone-300"] = "d6d3d1", + ["stone-400"] = "a8a29e", + ["stone-500"] = "78716c", + ["stone-600"] = "57534e", + ["stone-700"] = "44403c", + ["stone-800"] = "292524", + ["stone-900"] = "1c1917", + ["red-50"] = "fef2f2", + ["red-100"] = "fee2e2", + ["red-200"] = "fecaca", + ["red-300"] = "fca5a5", + ["red-400"] = "f87171", + ["red-500"] = "ef4444", + ["red-600"] = "dc2626", + ["red-700"] = "b91c1c", + ["red-800"] = "991b1b", + ["red-900"] = "7f1d1d", + ["orange-50"] = "fff7ed", + ["orange-100"] = "ffedd5", + ["orange-200"] = "fed7aa", + ["orange-300"] = "fdba74", + ["orange-400"] = "fb923c", + ["orange-500"] = "f97316", + ["orange-600"] = "ea580c", + ["orange-700"] = "c2410c", + ["orange-800"] = "9a3412", + ["orange-900"] = "7c2d12", + ["amber-50"] = "fffbeb", + ["amber-100"] = "fef3c7", + ["amber-200"] = "fde68a", + ["amber-300"] = "fcd34d", + ["amber-400"] = "fbbf24", + ["amber-500"] = "f59e0b", + ["amber-600"] = "d97706", + ["amber-700"] = "b45309", + ["amber-800"] = "92400e", + ["amber-900"] = "78350f", + ["yellow-50"] = "fefce8", + ["yellow-100"] = "fef9c3", + ["yellow-200"] = "fef08a", + ["yellow-300"] = "fde047", + ["yellow-400"] = "facc15", + ["yellow-500"] = "eab308", + ["yellow-600"] = "ca8a04", + ["yellow-700"] = "a16207", + ["yellow-800"] = "854d0e", + ["yellow-900"] = "713f12", + ["lime-50"] = "f7fee7", + ["lime-100"] = "ecfccb", + ["lime-200"] = "d9f99d", + ["lime-300"] = "bef264", + ["lime-400"] = "a3e635", + ["lime-500"] = "84cc16", + ["lime-600"] = "65a30d", + ["lime-700"] = "4d7c0f", + ["lime-800"] = "3f6212", + ["lime-900"] = "365314", + ["green-50"] = "f0fdf4", + ["green-100"] = "dcfce7", + ["green-200"] = "bbf7d0", + ["green-300"] = "86efac", + ["green-400"] = "4ade80", + ["green-500"] = "22c55e", + ["green-600"] = "16a34a", + ["green-700"] = "15803d", + ["green-800"] = "166534", + ["green-900"] = "14532d", + ["emerald-50"] = "ecfdf5", + ["emerald-100"] = "d1fae5", + ["emerald-200"] = "a7f3d0", + ["emerald-300"] = "6ee7b7", + ["emerald-400"] = "34d399", + ["emerald-500"] = "10b981", + ["emerald-600"] = "059669", + ["emerald-700"] = "047857", + ["emerald-800"] = "065f46", + ["emerald-900"] = "064e3b", + ["teal-50"] = "f0fdfa", + ["teal-100"] = "ccfbf1", + ["teal-200"] = "99f6e4", + ["teal-300"] = "5eead4", + ["teal-400"] = "2dd4bf", + ["teal-500"] = "14b8a6", + ["teal-600"] = "0d9488", + ["teal-700"] = "0f766e", + ["teal-800"] = "115e59", + ["teal-900"] = "134e4a", + ["cyan-50"] = "ecfeff", + ["cyan-100"] = "cffafe", + ["cyan-200"] = "a5f3fc", + ["cyan-300"] = "67e8f9", + ["cyan-400"] = "22d3ee", + ["cyan-500"] = "06b6d4", + ["cyan-600"] = "0891b2", + ["cyan-700"] = "0e7490", + ["cyan-800"] = "155e75", + ["cyan-900"] = "164e63", + ["sky-50"] = "f0f9ff", + ["sky-100"] = "e0f2fe", + ["sky-200"] = "bae6fd", + ["sky-300"] = "7dd3fc", + ["sky-400"] = "38bdf8", + ["sky-500"] = "0ea5e9", + ["sky-600"] = "0284c7", + ["sky-700"] = "0369a1", + ["sky-800"] = "075985", + ["sky-900"] = "0c4a6e", + ["blue-50"] = "eff6ff", + ["blue-100"] = "dbeafe", + ["blue-200"] = "bfdbfe", + ["blue-300"] = "93c5fd", + ["blue-400"] = "60a5fa", + ["blue-500"] = "3b82f6", + ["blue-600"] = "2563eb", + ["blue-700"] = "1d4ed8", + ["blue-800"] = "1e40af", + ["blue-900"] = "1e3a8a", + ["indigo-50"] = "eef2ff", + ["indigo-100"] = "e0e7ff", + ["indigo-200"] = "c7d2fe", + ["indigo-300"] = "a5b4fc", + ["indigo-400"] = "818cf8", + ["indigo-500"] = "6366f1", + ["indigo-600"] = "4f46e5", + ["indigo-700"] = "4338ca", + ["indigo-800"] = "3730a3", + ["indigo-900"] = "312e81", + ["violet-50"] = "f5f3ff", + ["violet-100"] = "ede9fe", + ["violet-200"] = "ddd6fe", + ["violet-300"] = "c4b5fd", + ["violet-400"] = "a78bfa", + ["violet-500"] = "8b5cf6", + ["violet-600"] = "7c3aed", + ["violet-700"] = "6d28d9", + ["violet-800"] = "5b21b6", + ["violet-900"] = "4c1d95", + ["purple-50"] = "faf5ff", + ["purple-100"] = "f3e8ff", + ["purple-200"] = "e9d5ff", + ["purple-300"] = "d8b4fe", + ["purple-400"] = "c084fc", + ["purple-500"] = "a855f7", + ["purple-600"] = "9333ea", + ["purple-700"] = "7e22ce", + ["purple-800"] = "6b21a8", + ["purple-900"] = "581c87", + ["fuchsia-50"] = "fdf4ff", + ["fuchsia-100"] = "fae8ff", + ["fuchsia-200"] = "f5d0fe", + ["fuchsia-300"] = "f0abfc", + ["fuchsia-400"] = "e879f9", + ["fuchsia-500"] = "d946ef", + ["fuchsia-600"] = "c026d3", + ["fuchsia-700"] = "a21caf", + ["fuchsia-800"] = "86198f", + ["fuchsia-900"] = "701a75", + ["pink-50"] = "fdf2f8", + ["pink-100"] = "fce7f3", + ["pink-200"] = "fbcfe8", + ["pink-300"] = "f9a8d4", + ["pink-400"] = "f472b6", + ["pink-500"] = "ec4899", + ["pink-600"] = "db2777", + ["pink-700"] = "be185d", + ["pink-800"] = "9d174d", + ["pink-900"] = "831843", + ["rose-50"] = "fff1f2", + ["rose-100"] = "ffe4e6", + ["rose-200"] = "fecdd3", + ["rose-300"] = "fda4af", + ["rose-400"] = "fb7185", + ["rose-500"] = "f43f5e", + ["rose-600"] = "e11d48", + ["rose-700"] = "be123c", + ["rose-800"] = "9f1239", + ["rose-900"] = "881337", + }, +} diff --git a/lua/colorizer/trie.lua b/lua/colorizer/trie.lua index 82a0d2d..1f7a0de 100644 --- a/lua/colorizer/trie.lua +++ b/lua/colorizer/trie.lua @@ -55,13 +55,18 @@ local INDEX_LOOKUP_TABLE = ffi.new("uint8_t[?]", total_char) local CHAR_LOOKUP_TABLE = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" do local b = string.byte + local extra_char = { + [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 - if i >= b "0" and i <= b "9" then - INDEX_LOOKUP_TABLE[i] = i - b "0" - elseif i >= b "A" and i <= b "Z" then - INDEX_LOOKUP_TABLE[i] = i - b "A" + 10 - elseif i >= b "a" and i <= b "z" then - INDEX_LOOKUP_TABLE[i] = i - b "a" + 10 + 26 + if i >= byte["0"] and i <= byte["9"] then + INDEX_LOOKUP_TABLE[i] = i - byte["0"] + elseif i >= byte["A"] and i <= byte["Z"] then + INDEX_LOOKUP_TABLE[i] = i - byte["A"] + 10 + elseif i >= byte["a"] and i <= byte["z"] then + INDEX_LOOKUP_TABLE[i] = i - byte["a"] + 10 + 26 + elseif extra_char[i] then else INDEX_LOOKUP_TABLE[i] = total_char end diff --git a/lua/colorizer/utils.lua b/lua/colorizer/utils.lua index 0cb09ee..3b501ac 100644 --- a/lua/colorizer/utils.lua +++ b/lua/colorizer/utils.lua @@ -22,22 +22,26 @@ local CATEGORY_ALPHANUM = bor(CATEGORY_ALPHA, CATEGORY_DIGIT) -- do not run the loop multiple times local b = string.byte +local byte_values = { ["0"] = b "0", ["9"] = b "9", ["a"] = b "a", ["f"] = b "f", ["z"] = b "z" } +local extra_char = { [b "-"] = true } + for i = 0, 255 do local v = 0 + local lowercase = bor(i, 0x20) -- Digit is bit 1 - if i >= b "0" and i <= b "9" then + if i >= byte_values["0"] and i <= byte_values["9"] then v = bor(v, lshift(1, 0)) v = bor(v, lshift(1, 2)) - v = bor(v, lshift(i - b "0", 4)) - end - local lowercase = bor(i, 0x20) - -- Alpha is bit 2 - if lowercase >= b "a" and lowercase <= b "z" then + v = bor(v, lshift(i - byte_values["0"], 4)) + elseif lowercase >= byte_values["a"] and lowercase <= byte_values["z"] then + -- Alpha is bit 2 v = bor(v, lshift(1, 1)) - if lowercase <= b "f" then + if lowercase <= byte_values["f"] then v = bor(v, lshift(1, 2)) - v = bor(v, lshift(lowercase - b "a" + 10, 4)) + v = bor(v, lshift(lowercase - byte_values["a"] + 10, 4)) end + elseif extra_char[i] then + v = i end BYTE_CATEGORY[i] = v end |