From 5f7680e7e7ccad68bd9962bcbb40dfde1c3669a1 Mon Sep 17 00:00:00 2001 From: Akianonymus Date: Sun, 4 Sep 2022 18:32:02 +0530 Subject: Fix https://github.com/NvChad/nvim-colorizer.lua/issues/12 | Add support for tailwind colors Three methods normal: Use the hardcoded colors lsp: Use lsp to fetch colors both: Use the hardcoded colors and use lsp too if available although this is hardcoding, but working with what we got, has all the colors tailwind provides, atleast the prebuilt ones for new defined colors, have to enable the lsp method Generated using https://github.com/tailwindlabs/tailwindcss/raw/master/src/public/colors.js and https://github.com/tailwindlabs/tailwindcss/raw/master/src/corePlugins.js use a different namespace than default for tailwind lsp method only try to use lsp method if attached Misc changes export the user_default_settings to doc clear name space after the lines are processed for a smooth transition cleanup some code --- README.md | 4 +- doc/colorizer.txt | 105 +++++++++---- doc/index.html | 2 +- doc/ldoc_vim.ltp | 5 +- doc/modules/colorizer.buffer_utils.html | 26 +++- doc/modules/colorizer.color_utils.html | 9 +- doc/modules/colorizer.html | 109 +++++++++++--- doc/modules/colorizer.matcher_utils.html | 2 +- doc/modules/colorizer.trie.html | 2 +- doc/modules/utils.html | 2 +- doc/tags | 1 + lua/colorizer.lua | 117 ++++++++++----- lua/colorizer/buffer_utils.lua | 122 +++++++++++++-- lua/colorizer/color_utils.lua | 23 ++- lua/colorizer/matcher_utils.lua | 12 +- lua/colorizer/tailwind_colors.lua | 250 +++++++++++++++++++++++++++++++ lua/colorizer/trie.lua | 17 ++- lua/colorizer/utils.lua | 20 ++- 18 files changed, 688 insertions(+), 140 deletions(-) create mode 100644 lua/colorizer/tailwind_colors.lua diff --git a/README.md b/README.md index ffcc616..e5d46cc 100644 --- a/README.md +++ b/README.md @@ -94,11 +94,13 @@ library to do custom highlighting themselves. 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 = "■", }, -- all the sub-options of filetypes apply to buftypes buftypes = {}, - } + } ``` MODES: diff --git a/doc/colorizer.txt b/doc/colorizer.txt index 1480756..5ac2abc 100644 --- a/doc/colorizer.txt +++ b/doc/colorizer.txt @@ -85,6 +85,9 @@ Functions: ~ |clear_highlight_cache| - Clear the highlight cache and reload all buffers. +Tables: ~ + |user_default_options| - defaults options. + Fields: ~ |DEFAULT_NAMESPACE| - Default namespace used in `colorizer.buffer_utils.highlight_buffer` and `attach_to_buffer`. @@ -105,7 +108,7 @@ is_buffer_attached({buf}) *colorizer.is_buffer_attached* {buf} - number|nil: A value of 0 implies the current buffer. returns:~ - number|nil: if attached to the buffer, false otherwise. + number or nil: if attached to the buffer, false otherwise. See also:~ |highlight_buffer| @@ -149,25 +152,12 @@ setup({config}) *colorizer.setup* > 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 = {}, } < + For all user_default_options, see |user_default_options| Parameters: ~ @@ -196,6 +186,46 @@ clear_highlight_cache() *colorizer.clear_highlight_cache* +user_default_options *colorizer.user_default_options* + defaults options. + +> + 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 = "■", + } +< + + + Fields: ~ + {RGB} - boolean + {RRGGBB} - boolean + {names} - boolean + {RRGGBBAA} - boolean + {AARRGGBB} - boolean + {rgb_fn} - boolean + {hsl_fn} - boolean + {css} - boolean + {css_fn} - boolean + {mode} - string + {tailwind} - boolean|string + {virtualtext} - string + + + DEFAULT_NAMESPACE *colorizer.DEFAULT_NAMESPACE* Default namespace used in `colorizer.buffer_utils.highlight_buffer` and `attach_to_buffer`. @@ -231,7 +261,7 @@ Fields: ~ *colorizer.buffer_utils.highlight_buffer* -highlight_buffer({buf}, {ns}, {lines}, {line_start}, {options}) +highlight_buffer({buf}, {ns}, {lines}, {line_start}, {options}, {options_local}) Highlight the buffer region. Highlight starting from `line_start` (0-indexed) for each line described by @@ -246,15 +276,25 @@ highlight_buffer({buf}, {ns}, {lines}, {line_start}, {options}) {lines} - table: the lines to highlight from the buffer. {line_start} - number: line_start should be 0-indexed {options} - table: Configuration options as described in `setup` + {options_local} - table: Buffer local variables + + returns:~ + nil or boolean or number,function or nil + -rehighlight_buffer({buf}, {options}) *colorizer.buffer_utils.rehighlight_buffer* + *colorizer.buffer_utils.rehighlight_buffer* +rehighlight_buffer({buf}, {options}, {options_local}) Rehighlight the buffer if colorizer is active Parameters: ~ {buf} - number: Buffer number {options} - table: Buffer options + {options_local} - table|nil: Buffer local variables + + returns:~ + nil or boolean or number,function or nil @@ -321,7 +361,7 @@ color_is_bright({r}, {g}, {b}) *colorizer.color_utils.color_is_bright* -color_name_parser({line}, {i}) *colorizer.color_utils.color_name_parser* +color_name_parser({line}, {i}, {opts}) *colorizer.color_utils.color_name_parser* Grab all the colour values from `vim.api.nvim_get_color_map` and create a lookup table. @@ -331,6 +371,7 @@ color_name_parser({line}, {i}) *colorizer.color_utils.color_name_parser* Parameters: ~ {line} - string: Line to parse {i} - number: Index of line from where to start parsing + {opts} - table: Currently contains whether tailwind is enabled or not @@ -342,8 +383,8 @@ rgb_function_parser({line}, {i}) *colorizer.color_utils.rgb_function_parser* {i} - number: Index of line from where to start parsing returns:~ - number|nil: Index of line where the rgb function ended - string|nil: rgb hex value + number or nil: Index of line where the rgb function ended + string or nil: rgb hex value @@ -360,8 +401,8 @@ rgba_function_parser({line}, {i}) *colorizer.color_utils.rgba_function_parser* {i} - number: Index of line from where to start parsing returns:~ - number|nil: Index of line where the rgba function ended - string|nil: rgb hex value + number or nil: Index of line where the rgba function ended + string or nil: rgb hex value @@ -373,8 +414,8 @@ hsl_function_parser({line}, {i}) *colorizer.color_utils.hsl_function_parser* {i} - number: Index of line from where to start parsing returns:~ - number|nil: Index of line where the hsl function ended - string|nil: rgb hex value + number or nil: Index of line where the hsl function ended + string or nil: rgb hex value @@ -386,8 +427,8 @@ hsla_function_parser({line}, {i}) *colorizer.color_utils.hsla_function_parser* {i} - number: Index of line from where to start parsing returns:~ - number|nil: Index of line where the hsla function ended - string|nil: rgb hex value + number or nil: Index of line where the hsla function ended + string or nil: rgb hex value @@ -402,8 +443,8 @@ argb_hex_parser({line}, {i}) *colorizer.color_utils.argb_hex_parser* {i} - number: index of line from where to start parsing returns:~ - number|nil: index of line where the hex value ended - string|nil: rgb hex value + number or nil: index of line where the hex value ended + string or nil: rgb hex value @@ -419,8 +460,8 @@ rgba_hex_parser({line}, {i}, {opts}) *colorizer.color_utils.rgba_hex_parser* {opts} - table: Containing minlen, maxlen, valid_lengths returns:~ - number|nil: index of line where the hex value ended - string|nil: rgb hex value + number or nil: index of line where the hex value ended + string or nil: rgb hex value @@ -449,7 +490,7 @@ make_matcher({options}) *colorizer.matcher_utils.make_matcher* {options} - table: options created in `colorizer.setup` returns:~ - function|boolean: function which will just parse the line for enabled + function or boolean: function which will just parse the line for enabled parsers diff --git a/doc/index.html b/doc/index.html index ae0a8d2..427033a 100644 --- a/doc/index.html +++ b/doc/index.html @@ -77,7 +77,7 @@
generated by LDoc 1.4.6 -Last updated 2022-09-03 17:24:13 +Last updated 2022-09-06 15:06:04
diff --git a/doc/ldoc_vim.ltp b/doc/ldoc_vim.ltp index baf388e..a664ffd 100644 --- a/doc/ldoc_vim.ltp +++ b/doc/ldoc_vim.ltp @@ -174,11 +174,12 @@ $(indent("", 2)){$(name)} - $(M(item.params.map[p],item)) $(indentstr)returns:~ # for i,group in ldoc.ipairs(groups) do local li,il = use_li(group) -# for r in group:iter() do local type, ctypes = item:return_type(r); local rt = ldoc.typename(type) +# for r in group:iter() do +# local type, ctypes = item:return_type(r); local rt = ldoc.typename(type) # if rt ~= '' then $(rt) # end -$(indent("", 2))$(r.text) +$(indent("", 2))$(r.text:gsub("|", " or ")) # if ctypes then # for c in ctypes:iter() do $(c.name) diff --git a/doc/modules/colorizer.buffer_utils.html b/doc/modules/colorizer.buffer_utils.html index f2f75fe..142164c 100644 --- a/doc/modules/colorizer.buffer_utils.html +++ b/doc/modules/colorizer.buffer_utils.html @@ -62,11 +62,11 @@

Functions

- + - +
highlight_buffer (buf, ns, lines, line_start, options)highlight_buffer (buf, ns, lines, line_start, options, options_local) Highlight the buffer region.
rehighlight_buffer (buf, options)rehighlight_buffer (buf, options, options_local) Rehighlight the buffer if colorizer is active
@@ -94,7 +94,7 @@
- highlight_buffer (buf, ns, lines, line_start, options) + highlight_buffer (buf, ns, lines, line_start, options, options_local)
Highlight the buffer region. @@ -119,8 +119,16 @@
  • options table: Configuration options as described in setup
  • +
  • options_local + table: Buffer local variables +
  • +

    Returns:

    +
      + + nil|boolean|number,function|nil +
    @@ -128,7 +136,7 @@
    - rehighlight_buffer (buf, options) + rehighlight_buffer (buf, options, options_local)
    Rehighlight the buffer if colorizer is active @@ -142,8 +150,16 @@
  • options table: Buffer options
  • +
  • options_local + table|nil: Buffer local variables +
  • +

    Returns:

    +
      + + nil|boolean|number,function|nil +
    @@ -215,7 +231,7 @@
    generated by LDoc 1.4.6 -Last updated 2022-09-03 17:24:13 +Last updated 2022-09-06 15:06:04
    diff --git a/doc/modules/colorizer.color_utils.html b/doc/modules/colorizer.color_utils.html index 613185c..8cd68e2 100644 --- a/doc/modules/colorizer.color_utils.html +++ b/doc/modules/colorizer.color_utils.html @@ -64,7 +64,7 @@ Determine whether to use black or white text. - color_name_parser (line, i) + color_name_parser (line, i, opts) Grab all the colour values from vim.api.nvim_get_color_map and create a lookup table. @@ -131,7 +131,7 @@
    - color_name_parser (line, i) + color_name_parser (line, i, opts)
    Grab all the colour values from vim.api.nvim_get_color_map and create a lookup table. @@ -146,6 +146,9 @@
  • i number: Index of line from where to start parsing
  • +
  • opts + table: Currently contains whether tailwind is enabled or not +
  • @@ -348,7 +351,7 @@
    generated by LDoc 1.4.6 -Last updated 2022-09-03 17:24:13 +Last updated 2022-09-06 15:06:04
    diff --git a/doc/modules/colorizer.html b/doc/modules/colorizer.html index 3db15a7..e02633c 100644 --- a/doc/modules/colorizer.html +++ b/doc/modules/colorizer.html @@ -33,6 +33,7 @@

    Contents

    @@ -155,6 +156,13 @@ USE WITH LUA Clear the highlight cache and reload all buffers. +

    Tables

    + + + + + +
    user_default_optionsdefaults options.

    Fields

    @@ -271,8 +279,7 @@ USE WITH LUA setup (config)
    - -

    Easy to use function if you want the full setup without fine grained control. + Easy to use function if you want the full setup without fine grained control. Setup an autocmd which enables colorizing for the filetypes and options specified.

    By default highlights all FileTypes.

    @@ -288,26 +295,13 @@ Setup an autocmd which enables colorizing for the filetypes and options specifie
         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 = {},
         }
     
    - +

    For all userdefaultoptions, see |userdefaultoptions|

    Parameters:

    @@ -373,6 +367,85 @@ Setup an autocmd which enables colorizing for the filetypes and options specifie +
    + +

    Tables

    + +
    +
    + + user_default_options +
    +
    + +

    defaults options.

    + +
    +  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 = "■",
    +  }
    +
    + + + + +

    Fields:

    +
      +
    • RGB + boolean +
    • +
    • RRGGBB + boolean +
    • +
    • names + boolean +
    • +
    • RRGGBBAA + boolean +
    • +
    • AARRGGBB + boolean +
    • +
    • rgb_fn + boolean +
    • +
    • hsl_fn + boolean +
    • +
    • css + boolean +
    • +
    • css_fn + boolean +
    • +
    • mode + string +
    • +
    • tailwind + boolean|string +
    • +
    • virtualtext + string +
    • +
    + + + + +

    Fields

    @@ -404,7 +477,7 @@ Setup an autocmd which enables colorizing for the filetypes and options specifie
    generated by LDoc 1.4.6 -Last updated 2022-09-03 17:24:13 +Last updated 2022-09-06 15:06:04
    diff --git a/doc/modules/colorizer.matcher_utils.html b/doc/modules/colorizer.matcher_utils.html index e709932..e4cccee 100644 --- a/doc/modules/colorizer.matcher_utils.html +++ b/doc/modules/colorizer.matcher_utils.html @@ -106,7 +106,7 @@ Do not try make the function again if it is present in the cache
    generated by LDoc 1.4.6 -Last updated 2022-09-03 17:24:13 +Last updated 2022-09-06 15:06:04
    diff --git a/doc/modules/colorizer.trie.html b/doc/modules/colorizer.trie.html index 0261786..e2c7192 100644 --- a/doc/modules/colorizer.trie.html +++ b/doc/modules/colorizer.trie.html @@ -62,7 +62,7 @@
    generated by LDoc 1.4.6 -Last updated 2022-09-03 17:24:13 +Last updated 2022-09-06 15:06:04
    diff --git a/doc/modules/utils.html b/doc/modules/utils.html index 3e3ff4d..ffe7615 100644 --- a/doc/modules/utils.html +++ b/doc/modules/utils.html @@ -224,7 +224,7 @@
    generated by LDoc 1.4.6 -Last updated 2022-09-03 17:24:13 +Last updated 2022-09-06 15:06:04
    diff --git a/doc/tags b/doc/tags index bb5229d..1041494 100644 --- a/doc/tags +++ b/doc/tags @@ -35,3 +35,4 @@ colorizer.matcher_utils.make_matcher colorizer.txt /*colorizer.matcher_utils.mak colorizer.reload_all_buffers colorizer.txt /*colorizer.reload_all_buffers* colorizer.setup colorizer.txt /*colorizer.setup* colorizer.trie-introduction colorizer.txt /*colorizer.trie-introduction* +colorizer.user_default_options colorizer.txt /*colorizer.user_default_options* 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. +--
    +--  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 = "■",
    +--  }
    +--
    +---@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 --
     --    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 = {},
     --    }
     --
    +--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 -- cgit v1.2.3-70-g09d2