diff options
-rw-r--r-- | doc/colorizer.txt | 277 | ||||
-rw-r--r-- | doc/index.html | 12 | ||||
-rw-r--r-- | doc/modules/colorizer.buffer.html (renamed from doc/modules/colorizer.buffer_utils.html) | 122 | ||||
-rw-r--r-- | doc/modules/colorizer.color.html (renamed from doc/modules/colorizer.color_utils.html) | 217 | ||||
-rw-r--r-- | doc/modules/colorizer.html | 16 | ||||
-rw-r--r-- | doc/modules/colorizer.matcher.html (renamed from doc/modules/colorizer.matcher_utils.html) | 46 | ||||
-rw-r--r-- | doc/modules/colorizer.sass.html | 32 | ||||
-rw-r--r-- | doc/modules/colorizer.tailwind.html | 22 | ||||
-rw-r--r-- | doc/modules/colorizer.trie.html | 6 | ||||
-rw-r--r-- | doc/modules/utils.html | 64 | ||||
-rw-r--r-- | lua/colorizer.lua | 78 | ||||
-rw-r--r-- | lua/colorizer/buffer.lua (renamed from lua/colorizer/buffer_utils.lua) | 146 | ||||
-rw-r--r-- | lua/colorizer/color.lua (renamed from lua/colorizer/color_utils.lua) | 352 | ||||
-rw-r--r-- | lua/colorizer/matcher.lua (renamed from lua/colorizer/matcher_utils.lua) | 44 | ||||
-rw-r--r-- | lua/colorizer/sass.lua | 27 | ||||
-rw-r--r-- | lua/colorizer/tailwind.lua | 53 | ||||
-rw-r--r-- | lua/colorizer/utils.lua | 107 | ||||
-rw-r--r-- | test/sample.scss | 15 |
18 files changed, 945 insertions, 691 deletions
diff --git a/doc/colorizer.txt b/doc/colorizer.txt index 6171fbf..57273af 100644 --- a/doc/colorizer.txt +++ b/doc/colorizer.txt @@ -89,15 +89,15 @@ Tables: ~ |user_default_options| - defaults options. Fields: ~ - |DEFAULT_NAMESPACE| - Default namespace used in - `colorizer.buffer_utils.highlight_buffer` and `attach_to_buffer`. + |DEFAULT_NAMESPACE| - Default namespace used in `colorizer.buffer.highlight` + and `attach_to_buffer`. highlight_buffer() *colorizer.highlight_buffer* Highlight the buffer region See also:~ - |colorizer.buffer_utils.highlight_buffer| + |colorizer.buffer.highlight| @@ -111,7 +111,7 @@ is_buffer_attached({buf}) *colorizer.is_buffer_attached* number or nil: if attached to the buffer, false otherwise. See also:~ - |highlight_buffer| + |colorizer.buffer.highlight| @@ -234,49 +234,69 @@ user_default_options *colorizer.user_default_options* DEFAULT_NAMESPACE *colorizer.DEFAULT_NAMESPACE* - Default namespace used in `colorizer.buffer_utils.highlight_buffer` and + Default namespace used in `colorizer.buffer.highlight` and `attach_to_buffer`. See also:~ - |colorizer.buffer_utils.highlight_buffer| + |colorizer.buffer.highlight| |attach_to_buffer| ============================================================================== -BUFFER_UTILS *colorizer.buffer_utils-introduction* +BUFFER *colorizer.buffer-introduction* Helper functions to highlight buffer smartly ============================================================================== -LUA API *colorizer.buffer_utils-lua-api* +LUA API *colorizer.buffer-lua-api* Functions: ~ |clear_hl_cache| - Clean the highlight cache - |highlight_buffer| - Highlight the buffer region. + |add_highlight| - Create highlight and set highlights - |rehighlight_buffer| - Rehighlight the buffer if colorizer is active + |highlight| - Highlight the buffer region. + + |parse_lines| - Parse the given lines for colors and return a table + containing + rgb_hex and range per line + + |rehighlight| - Rehighlight the buffer if colorizer is active Tables: ~ - |HIGHLIGHT_MODE_NAMES| - Highlight mode which will be use to render the + |highlight_mode_names| - Highlight mode which will be use to render the colour Fields: ~ - |DEFAULT_NAMESPACE| - Default namespace used in `highlight_buffer` and + |default_namespace| - Default namespace used in `highlight` and `colorizer.attach_to_buffer`. -clear_hl_cache() *colorizer.buffer_utils.clear_hl_cache* +clear_hl_cache() *colorizer.buffer.clear_hl_cache* Clean the highlight cache - *colorizer.buffer_utils.highlight_buffer* -highlight_buffer({buf}, {ns}, {lines}, {line_start}, {line_end}, {options}, -{options_local}) + *colorizer.buffer.add_highlight* +add_highlight({buf}, {ns}, {line_start}, {line_end}, {data}, {options}) + Create highlight and set highlights + + Parameters: ~ + {buf} - number + {ns} - number + {line_start} - number + {line_end} - number + {data} - table: table output of `parse_lines` + {options} - table: Passed in setup, mainly for `user_default_options` + + + + + *colorizer.buffer.highlight* +highlight({buf}, {ns}, {line_start}, {line_end}, {options}, {options_local}) Highlight the buffer region. Highlight starting from `line_start` (0-indexed) for each line described by @@ -287,8 +307,7 @@ highlight_buffer({buf}, {ns}, {lines}, {line_start}, {line_end}, {options}, Parameters: ~ {buf} - number: buffer id {ns} - number: The namespace id. Default is DEFAULT_NAMESPACE. Create - it with `vim.api.create_namespace` - {lines} - table: the lines to highlight from the buffer. + it with `vim.api.nvim_create_namespace` {line_start} - number: line_start should be 0-indexed {line_end} - number: Last line to highlight {options} - table: Configuration options as described in `setup` @@ -300,8 +319,27 @@ highlight_buffer({buf}, {ns}, {lines}, {line_start}, {line_end}, {options}, - *colorizer.buffer_utils.rehighlight_buffer* -rehighlight_buffer({buf}, {options}, {options_local}, {use_local_lines}) + *colorizer.buffer.parse_lines* +parse_lines({buf}, {lines}, {line_start}, {options}) + Parse the given lines for colors and return a table containing + rgb_hex and range per line + + Parameters: ~ + {buf} - number + {lines} - table + {line_start} - number: This is the buffer line number, from where to + start highlighting + {options} - table: Passed in `colorizer.setup`, Only uses + `user_default_options` + + returns:~ + table or nil + + + + + *colorizer.buffer.rehighlight* +rehighlight({buf}, {options}, {options_local}, {use_local_lines}) Rehighlight the buffer if colorizer is active Parameters: ~ @@ -316,7 +354,7 @@ rehighlight_buffer({buf}, {options}, {options_local}, {use_local_lines}) -HIGHLIGHT_MODE_NAMES *colorizer.buffer_utils.HIGHLIGHT_MODE_NAMES* +highlight_mode_names *colorizer.buffer.highlight_mode_names* Highlight mode which will be use to render the colour Fields: ~ @@ -326,147 +364,179 @@ HIGHLIGHT_MODE_NAMES *colorizer.buffer_utils.HIGHLIGHT_MODE_NAMES* -DEFAULT_NAMESPACE *colorizer.buffer_utils.DEFAULT_NAMESPACE* - Default namespace used in `highlight_buffer` and - `colorizer.attach_to_buffer`. +default_namespace *colorizer.buffer.default_namespace* + Default namespace used in `highlight` and `colorizer.attach_to_buffer`. See also:~ - |highlight_buffer| + |highlight| |colorizer.attach_to_buffer| ============================================================================== -COLOR_UTILS *colorizer.color_utils-introduction* +COLOR *colorizer.color-introduction* Helper functions to parse different colour formats ============================================================================== -LUA API *colorizer.color_utils-lua-api* +LUA API *colorizer.color-lua-api* Functions: ~ - |color_is_bright| - Determine whether to use black or white text. + |argb_hex_parser| - parse for 0xaarrggbb and return rgb hex. - |color_name_parser| - Grab all the colour values from - `vim.api.nvim_get_color_map` and create a lookup table. + |hsl_to_rgb| - Converts an HSL color value to RGB. + + |hsl_function_parser| - Parse for hsl() css function and return rgb hex. + + |hsla_function_parser| - Parse for hsl() css function and return rgb hex. + + |hue_to_rgb| - Convert hsl colour values to rgb. + + |is_bright| - Determine whether to use black or white text. + + |name_parser| - Grab all the colour values from `vim.api.nvim_get_color_map` + and create a lookup table. |rgb_function_parser| - Parse for rgb() css function and return rgb hex. |rgba_function_parser| - Parse for rgba() css function and return rgb hex. - |hsl_function_parser| - Parse for hsl() css function and return rgb hex. + |rgba_hex_parser| - parse for #rrggbbaa and return rgb hex. - |hsla_function_parser| - Parse for hsl() css function and return rgb hex. - |argb_hex_parser| - parse for 0xaarrggbb and return rgb hex. +argb_hex_parser({line}, {i}) *colorizer.color.argb_hex_parser* + parse for 0xaarrggbb and return rgb hex. - |rgba_hex_parser| - parse for #rrggbbaa and return rgb hex. + a format used in android apps -color_is_bright({r}, {g}, {b}) *colorizer.color_utils.color_is_bright* - Determine whether to use black or white text. + Parameters: ~ + {line} - string: line to parse + {i} - number: index of line from where to start parsing + returns:~ + number or nil: index of line where the hex value ended + string or nil: rgb hex value - ref: https://stackoverflow.com/a/1855903/837964 - https://stackoverflow.com/questions/596216/formula-to-determine-brightness-of-rgb-color +hsl_to_rgb({h}, {s}, {l}) *colorizer.color.hsl_to_rgb* + Converts an HSL color value to RGB. + Parameters: ~ - {r} - number: Red - {g} - number: Green - {b} - number: Blue + {h} - number: Hue + {s} - number: Saturation + {l} - number: Lightness + returns:~ + number or nil,number or nil,number or nil -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. - - COLOR_MAP is used to store the colour values +hsl_function_parser({line}, {i}) *colorizer.color.hsl_function_parser* + Parse for hsl() css function and return rgb hex. 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 + returns:~ + number or nil: Index of line where the hsl function ended + string or nil: rgb hex value -rgb_function_parser({line}, {i}) *colorizer.color_utils.rgb_function_parser* - Parse for rgb() css function and return rgb hex. + +hsla_function_parser({line}, {i}) *colorizer.color.hsla_function_parser* + Parse for hsl() css function and return rgb hex. Parameters: ~ {line} - string: Line to parse {i} - number: Index of line from where to start parsing returns:~ - number or nil: Index of line where the rgb function ended + number or nil: Index of line where the hsla function ended string or nil: rgb hex value -rgba_function_parser({line}, {i}) *colorizer.color_utils.rgba_function_parser* - Parse for rgba() css function and return rgb hex. +hue_to_rgb({p}, {q}, {t}) *colorizer.color.hue_to_rgb* + Convert hsl colour values to rgb. - Todo consider removing the regexes here - Todo this might not be the best approach to alpha channel. - Things like pumblend might be useful here. + Source: https://gist.github.com/mjackson/5311256 Parameters: ~ - {line} - string: Line to parse - {i} - number: Index of line from where to start parsing + {p} - number + {q} - number + {t} - number returns:~ - number or nil: Index of line where the rgba function ended - string or nil: rgb hex value + number -hsl_function_parser({line}, {i}) *colorizer.color_utils.hsl_function_parser* - Parse for hsl() css function and return rgb hex. +is_bright({r}, {g}, {b}) *colorizer.color.is_bright* + Determine whether to use black or white text. + + + ref: https://stackoverflow.com/a/1855903/837964 + https://stackoverflow.com/questions/596216/formula-to-determine-brightness-of-rgb-color + + + Parameters: ~ + {r} - number: Red + {g} - number: Green + {b} - number: Blue + + + +name_parser({line}, {i}, {opts}) *colorizer.color.name_parser* + 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 + Parameters: ~ {line} - string: Line to parse {i} - number: Index of line from where to start parsing - - returns:~ - number or nil: Index of line where the hsl function ended - string or nil: rgb hex value + {opts} - table: Currently contains whether tailwind is enabled or not -hsla_function_parser({line}, {i}) *colorizer.color_utils.hsla_function_parser* - Parse for hsl() css function and return rgb hex. +rgb_function_parser({line}, {i}) *colorizer.color.rgb_function_parser* + Parse for rgb() css function and return rgb hex. Parameters: ~ {line} - string: Line to parse {i} - number: Index of line from where to start parsing returns:~ - number or nil: Index of line where the hsla function ended + number or nil: Index of line where the rgb function ended string or nil: rgb hex value -argb_hex_parser({line}, {i}) *colorizer.color_utils.argb_hex_parser* - parse for 0xaarrggbb and return rgb hex. +rgba_function_parser({line}, {i}) *colorizer.color.rgba_function_parser* + Parse for rgba() css function and return rgb hex. - a format used in android apps + Todo consider removing the regexes here + Todo this might not be the best approach to alpha channel. + Things like pumblend might be useful here. Parameters: ~ - {line} - string: line to parse - {i} - number: index of line from where to start parsing + {line} - string: Line to parse + {i} - number: Index of line from where to start parsing returns:~ - number or nil: index of line where the hex value ended + number or nil: Index of line where the rgba function ended string or nil: rgb hex value -rgba_hex_parser({line}, {i}, {opts}) *colorizer.color_utils.rgba_hex_parser* +rgba_hex_parser({line}, {i}, {opts}) *colorizer.color.rgba_hex_parser* parse for #rrggbbaa and return rgb hex. a format used in android apps @@ -484,20 +554,34 @@ rgba_hex_parser({line}, {i}, {opts}) *colorizer.color_utils.rgba_hex_parser* ============================================================================== -MATCHER_UTILS *colorizer.matcher_utils-introduction* +MATCHER *colorizer.matcher-introduction* Helper functions for colorizer to enable required parsers ============================================================================== -LUA API *colorizer.matcher_utils-lua-api* +LUA API *colorizer.matcher-lua-api* Functions: ~ - |make_matcher| - Parse the given options and return a function with enabled - parsers. + |compile| - Form a trie stuct with the given prefixes + + |make| - Parse the given options and return a function with enabled parsers. + + +compile({matchers}, {matchers_trie}) *colorizer.matcher.compile* + Form a trie stuct with the given prefixes + + Parameters: ~ + {matchers} - table: List of prefixes, {"rgb", "hsl"} + {matchers_trie} - table: Table containing information regarding + non-trie based parsers + + returns:~ + function: function which will just parse the line for enabled parsers + -make_matcher({options}) *colorizer.matcher_utils.make_matcher* +make({options}) *colorizer.matcher.make* Parse the given options and return a function with enabled parsers. if no parsers enabled then return false @@ -523,16 +607,16 @@ Helper functions to parse sass color variables LUA API *colorizer.sass-lua-api* Functions: ~ - |sass_cleanup| - Cleanup sass variables and watch handlers + |cleanup| - Cleanup sass variables and watch handlers - |sass_name_parser| - Parse the given line for sass color names + |name_parser| - Parse the given line for sass color names check for value in SASS[buf].DEFINITIONS_ALL - |sass_update_variables| - Parse the given lines for sass variabled and add - to SASS[buf].DEFINITIONS_ALL. + |update_variables| - Parse the given lines for sass variabled and add to + `SASS[buf].DEFINITIONS_ALL`. -sass_cleanup({buf}) *colorizer.sass.sass_cleanup* +cleanup({buf}) *colorizer.sass.cleanup* Cleanup sass variables and watch handlers Parameters: ~ @@ -540,7 +624,7 @@ sass_cleanup({buf}) *colorizer.sass.sass_cleanup* -sass_name_parser({line}, {i}, {buf}) *colorizer.sass.sass_name_parser* +name_parser({line}, {i}, {buf}) *colorizer.sass.name_parser* Parse the given line for sass color names check for value in SASS[buf].DEFINITIONS_ALL @@ -555,11 +639,11 @@ sass_name_parser({line}, {i}, {buf}) *colorizer.sass.sass_name_parser* - *colorizer.sass.sass_update_variables* -sass_update_variables({buf}, {line_start}, {line_end}, {lines}, {color_parser}, + *colorizer.sass.update_variables* +update_variables({buf}, {line_start}, {line_end}, {lines}, {color_parser}, {options}, {options_local}) Parse the given lines for sass variabled and add to - SASS[buf].DEFINITIONS_ALL. + `SASS[buf].DEFINITIONS_ALL`. which is then used in |sass_name_parser| If lines are not given, then fetch the lines with line_start and line_end @@ -586,14 +670,13 @@ Helper functions to parse tailwind color variables LUA API *colorizer.tailwind-lua-api* Functions: ~ - |tailwind_cleanup| - Cleanup tailwind variables and autocmd + |cleanup| - Cleanup tailwind variables and autocmd - |tailwind_setup_lsp_colors| - highlight buffer using values returned by - tailwindcss - To see these table information, see |colorizer.buffer_utils| + |setup_lsp_colors| - highlight buffer using values returned by tailwindcss + To see these table information, see |colorizer.buffer| -tailwind_cleanup({buf}) *colorizer.tailwind.tailwind_cleanup* +cleanup({buf}) *colorizer.tailwind.cleanup* Cleanup tailwind variables and autocmd Parameters: ~ @@ -602,10 +685,10 @@ tailwind_cleanup({buf}) *colorizer.tailwind.tailwind_cleanup* - *colorizer.tailwind.tailwind_setup_lsp_colors* -tailwind_setup_lsp_colors({buf}, {options}, {options_local}, {add_highlight}) + *colorizer.tailwind.setup_lsp_colors* +setup_lsp_colors({buf}, {options}, {options_local}, {add_highlight}) highlight buffer using values returned by tailwindcss - To see these table information, see |colorizer.buffer_utils| + To see these table information, see |colorizer.buffer| Parameters: ~ {buf} - number diff --git a/doc/index.html b/doc/index.html index 5e9f21d..827380b 100644 --- a/doc/index.html +++ b/doc/index.html @@ -32,9 +32,9 @@ <h2>Modules</h2> <ul class="nowrap"> <li><a href="modules/colorizer.html">colorizer</a></li> - <li><a href="modules/colorizer.buffer_utils.html">buffer_utils</a></li> - <li><a href="modules/colorizer.color_utils.html">color_utils</a></li> - <li><a href="modules/colorizer.matcher_utils.html">matcher_utils</a></li> + <li><a href="modules/colorizer.buffer.html">buffer</a></li> + <li><a href="modules/colorizer.color.html">color</a></li> + <li><a href="modules/colorizer.matcher.html">matcher</a></li> <li><a href="modules/colorizer.sass.html">sass</a></li> <li><a href="modules/colorizer.tailwind.html">tailwind</a></li> <li><a href="modules/colorizer.trie.html">trie</a></li> @@ -54,15 +54,15 @@ <td class="summary">Requires Neovim >= 0.6.0 and <code>set termguicolors</code></td> </tr> <tr> - <td class="name" nowrap><a href="modules/colorizer.buffer_utils.html">colorizer.buffer_utils</a></td> + <td class="name" nowrap><a href="modules/colorizer.buffer.html">colorizer.buffer</a></td> <td class="summary">Helper functions to highlight buffer smartly</td> </tr> <tr> - <td class="name" nowrap><a href="modules/colorizer.color_utils.html">colorizer.color_utils</a></td> + <td class="name" nowrap><a href="modules/colorizer.color.html">colorizer.color</a></td> <td class="summary">Helper functions to parse different colour formats</td> </tr> <tr> - <td class="name" nowrap><a href="modules/colorizer.matcher_utils.html">colorizer.matcher_utils</a></td> + <td class="name" nowrap><a href="modules/colorizer.matcher.html">colorizer.matcher</a></td> <td class="summary">Helper functions for colorizer to enable required parsers</td> </tr> <tr> diff --git a/doc/modules/colorizer.buffer_utils.html b/doc/modules/colorizer.buffer.html index 783afcc..a120996 100644 --- a/doc/modules/colorizer.buffer_utils.html +++ b/doc/modules/colorizer.buffer.html @@ -41,9 +41,9 @@ <h2>Modules</h2> <ul class="nowrap"> <li><a href="../modules/colorizer.html">colorizer</a></li> - <li><strong>buffer_utils</strong></li> - <li><a href="../modules/colorizer.color_utils.html">color_utils</a></li> - <li><a href="../modules/colorizer.matcher_utils.html">matcher_utils</a></li> + <li><strong>buffer</strong></li> + <li><a href="../modules/colorizer.color.html">color</a></li> + <li><a href="../modules/colorizer.matcher.html">matcher</a></li> <li><a href="../modules/colorizer.sass.html">sass</a></li> <li><a href="../modules/colorizer.tailwind.html">tailwind</a></li> <li><a href="../modules/colorizer.trie.html">trie</a></li> @@ -54,7 +54,7 @@ <div id="content"> -<h1>Module <code>colorizer.buffer_utils</code></h1> +<h1>Module <code>colorizer.buffer</code></h1> <p>Helper functions to highlight buffer smartly</p> <p> @@ -68,26 +68,35 @@ <td class="summary">Clean the highlight cache</td> </tr> <tr> - <td class="name" nowrap><a href="#highlight_buffer">highlight_buffer (buf, ns, lines, line_start, line_end, options, options_local)</a></td> + <td class="name" nowrap><a href="#add_highlight">add_highlight (buf, ns, line_start, line_end, data, options)</a></td> + <td class="summary">Create highlight and set highlights</td> + </tr> + <tr> + <td class="name" nowrap><a href="#highlight">highlight (buf, ns, line_start, line_end, options, options_local)</a></td> <td class="summary">Highlight the buffer region.</td> </tr> <tr> - <td class="name" nowrap><a href="#rehighlight_buffer">rehighlight_buffer (buf, options, options_local, use_local_lines)</a></td> + <td class="name" nowrap><a href="#parse_lines">parse_lines (buf, lines, line_start, options)</a></td> + <td class="summary">Parse the given lines for colors and return a table containing + rgb_hex and range per line</td> + </tr> + <tr> + <td class="name" nowrap><a href="#rehighlight">rehighlight (buf, options, options_local, use_local_lines)</a></td> <td class="summary">Rehighlight the buffer if colorizer is active</td> </tr> </table> <h2><a href="#Tables">Tables</a></h2> <table class="function_list"> <tr> - <td class="name" nowrap><a href="#HIGHLIGHT_MODE_NAMES">HIGHLIGHT_MODE_NAMES</a></td> + <td class="name" nowrap><a href="#highlight_mode_names">highlight_mode_names</a></td> <td class="summary">Highlight mode which will be use to render the colour</td> </tr> </table> <h2><a href="#Fields">Fields</a></h2> <table class="function_list"> <tr> - <td class="name" nowrap><a href="#DEFAULT_NAMESPACE">DEFAULT_NAMESPACE</a></td> - <td class="summary">Default namespace used in <a href="../modules/colorizer.buffer_utils.html#highlight_buffer">highlight_buffer</a> and <a href="../modules/colorizer.html#attach_to_buffer">colorizer.attach_to_buffer</a>.</td> + <td class="name" nowrap><a href="#default_namespace">default_namespace</a></td> + <td class="summary">Default namespace used in <a href="../modules/colorizer.buffer.html#highlight">highlight</a> and <a href="../modules/colorizer.html#attach_to_buffer">colorizer.attach_to_buffer</a>.</td> </tr> </table> @@ -113,8 +122,43 @@ </dd> <dt> - <a name = "highlight_buffer"></a> - <strong>highlight_buffer (buf, ns, lines, line_start, line_end, options, options_local)</strong> + <a name = "add_highlight"></a> + <strong>add_highlight (buf, ns, line_start, line_end, data, options)</strong> + </dt> + <dd> + Create highlight and set highlights + + + <h3>Parameters:</h3> + <ul> + <li><span class="parameter">buf</span> + number + </li> + <li><span class="parameter">ns</span> + number + </li> + <li><span class="parameter">line_start</span> + number + </li> + <li><span class="parameter">line_end</span> + number + </li> + <li><span class="parameter">data</span> + table: table output of <a href="../modules/colorizer.buffer.html#parse_lines">parse_lines</a> + </li> + <li><span class="parameter">options</span> + table: Passed in setup, mainly for <code>user_default_options</code> + </li> + </ul> + + + + + +</dd> + <dt> + <a name = "highlight"></a> + <strong>highlight (buf, ns, line_start, line_end, options, options_local)</strong> </dt> <dd> Highlight the buffer region. @@ -128,10 +172,7 @@ number: buffer id </li> <li><span class="parameter">ns</span> - number: The namespace id. Default is DEFAULT_NAMESPACE. Create it with <code>vim.api.create_namespace</code> - </li> - <li><span class="parameter">lines</span> - table: the lines to highlight from the buffer. + number: The namespace id. Default is DEFAULT_NAMESPACE. Create it with <code>vim.api.nvim_create_namespace</code> </li> <li><span class="parameter">line_start</span> number: line_start should be 0-indexed @@ -158,8 +199,43 @@ </dd> <dt> - <a name = "rehighlight_buffer"></a> - <strong>rehighlight_buffer (buf, options, options_local, use_local_lines)</strong> + <a name = "parse_lines"></a> + <strong>parse_lines (buf, lines, line_start, options)</strong> + </dt> + <dd> + Parse the given lines for colors and return a table containing + rgb_hex and range per line + + + <h3>Parameters:</h3> + <ul> + <li><span class="parameter">buf</span> + number + </li> + <li><span class="parameter">lines</span> + table + </li> + <li><span class="parameter">line_start</span> + number: This is the buffer line number, from where to start highlighting + </li> + <li><span class="parameter">options</span> + table: Passed in <a href="../modules/colorizer.html#setup">colorizer.setup</a>, Only uses <code>user_default_options</code> + </li> + </ul> + + <h3>Returns:</h3> + <ol> + + table|nil + </ol> + + + + +</dd> + <dt> + <a name = "rehighlight"></a> + <strong>rehighlight (buf, options, options_local, use_local_lines)</strong> </dt> <dd> Rehighlight the buffer if colorizer is active @@ -196,8 +272,8 @@ <dl class="function"> <dt> - <a name = "HIGHLIGHT_MODE_NAMES"></a> - <strong>HIGHLIGHT_MODE_NAMES</strong> + <a name = "highlight_mode_names"></a> + <strong>highlight_mode_names</strong> </dt> <dd> Highlight mode which will be use to render the colour @@ -232,11 +308,11 @@ <dl class="function"> <dt> - <a name = "DEFAULT_NAMESPACE"></a> - <strong>DEFAULT_NAMESPACE</strong> + <a name = "default_namespace"></a> + <strong>default_namespace</strong> </dt> <dd> - Default namespace used in <a href="../modules/colorizer.buffer_utils.html#highlight_buffer">highlight_buffer</a> and <a href="../modules/colorizer.html#attach_to_buffer">colorizer.attach_to_buffer</a>. + Default namespace used in <a href="../modules/colorizer.buffer.html#highlight">highlight</a> and <a href="../modules/colorizer.html#attach_to_buffer">colorizer.attach_to_buffer</a>. @@ -244,7 +320,7 @@ <h3>See also:</h3> <ul> - <li><a href="../modules/colorizer.buffer_utils.html#highlight_buffer">highlight_buffer</a></li> + <li><a href="../modules/colorizer.buffer.html#highlight">highlight</a></li> <li><a href="../modules/colorizer.html#attach_to_buffer">colorizer.attach_to_buffer</a></li> </ul> diff --git a/doc/modules/colorizer.color_utils.html b/doc/modules/colorizer.color.html index 0ac39db..b36a8aa 100644 --- a/doc/modules/colorizer.color_utils.html +++ b/doc/modules/colorizer.color.html @@ -39,9 +39,9 @@ <h2>Modules</h2> <ul class="nowrap"> <li><a href="../modules/colorizer.html">colorizer</a></li> - <li><a href="../modules/colorizer.buffer_utils.html">buffer_utils</a></li> - <li><strong>color_utils</strong></li> - <li><a href="../modules/colorizer.matcher_utils.html">matcher_utils</a></li> + <li><a href="../modules/colorizer.buffer.html">buffer</a></li> + <li><strong>color</strong></li> + <li><a href="../modules/colorizer.matcher.html">matcher</a></li> <li><a href="../modules/colorizer.sass.html">sass</a></li> <li><a href="../modules/colorizer.tailwind.html">tailwind</a></li> <li><a href="../modules/colorizer.trie.html">trie</a></li> @@ -52,7 +52,7 @@ <div id="content"> -<h1>Module <code>colorizer.color_utils</code></h1> +<h1>Module <code>colorizer.color</code></h1> <p>Helper functions to parse different colour formats</p> <p> @@ -62,20 +62,12 @@ <h2><a href="#Functions">Functions</a></h2> <table class="function_list"> <tr> - <td class="name" nowrap><a href="#color_is_bright">color_is_bright (r, g, b)</a></td> - <td class="summary">Determine whether to use black or white text.</td> - </tr> - <tr> - <td class="name" nowrap><a href="#color_name_parser">color_name_parser (line, i, opts)</a></td> - <td class="summary">Grab all the colour values from <code>vim.api.nvim_get_color_map</code> and create a lookup table.</td> - </tr> - <tr> - <td class="name" nowrap><a href="#rgb_function_parser">rgb_function_parser (line, i)</a></td> - <td class="summary">Parse for rgb() css function and return rgb hex.</td> + <td class="name" nowrap><a href="#argb_hex_parser">argb_hex_parser (line, i)</a></td> + <td class="summary">parse for 0xaarrggbb and return rgb hex.</td> </tr> <tr> - <td class="name" nowrap><a href="#rgba_function_parser">rgba_function_parser (line, i)</a></td> - <td class="summary">Parse for rgba() css function and return rgb hex.</td> + <td class="name" nowrap><a href="#hsl_to_rgb">hsl_to_rgb (h, s, l)</a></td> + <td class="summary">Converts an HSL color value to RGB.</td> </tr> <tr> <td class="name" nowrap><a href="#hsl_function_parser">hsl_function_parser (line, i)</a></td> @@ -86,8 +78,24 @@ <td class="summary">Parse for hsl() css function and return rgb hex.</td> </tr> <tr> - <td class="name" nowrap><a href="#argb_hex_parser">argb_hex_parser (line, i)</a></td> - <td class="summary">parse for 0xaarrggbb and return rgb hex.</td> + <td class="name" nowrap><a href="#hue_to_rgb">hue_to_rgb (p, q, t)</a></td> + <td class="summary">Convert hsl colour values to rgb.</td> + </tr> + <tr> + <td class="name" nowrap><a href="#is_bright">is_bright (r, g, b)</a></td> + <td class="summary">Determine whether to use black or white text.</td> + </tr> + <tr> + <td class="name" nowrap><a href="#name_parser">name_parser (line, i, opts)</a></td> + <td class="summary">Grab all the colour values from <code>vim.api.nvim_get_color_map</code> and create a lookup table.</td> + </tr> + <tr> + <td class="name" nowrap><a href="#rgb_function_parser">rgb_function_parser (line, i)</a></td> + <td class="summary">Parse for rgb() css function and return rgb hex.</td> + </tr> + <tr> + <td class="name" nowrap><a href="#rgba_function_parser">rgba_function_parser (line, i)</a></td> + <td class="summary">Parse for rgba() css function and return rgb hex.</td> </tr> <tr> <td class="name" nowrap><a href="#rgba_hex_parser">rgba_hex_parser (line, i, opts)</a></td> @@ -103,67 +111,73 @@ <dl class="function"> <dt> - <a name = "color_is_bright"></a> - <strong>color_is_bright (r, g, b)</strong> + <a name = "argb_hex_parser"></a> + <strong>argb_hex_parser (line, i)</strong> </dt> <dd> - Determine whether to use black or white text. </p> - -<p> ref: https://stackoverflow.com/a/1855903/837964 - https://stackoverflow.com/questions/596216/formula-to-determine-brightness-of-rgb-color + parse for 0xaarrggbb and return rgb hex. + a format used in android apps <h3>Parameters:</h3> <ul> - <li><span class="parameter">r</span> - number: Red - </li> - <li><span class="parameter">g</span> - number: Green + <li><span class="parameter">line</span> + string: line to parse </li> - <li><span class="parameter">b</span> - number: Blue + <li><span class="parameter">i</span> + number: index of line from where to start parsing </li> </ul> + <h3>Returns:</h3> + <ol> + <li> + number|nil: index of line where the hex value ended</li> + <li> + string|nil: rgb hex value</li> + </ol> </dd> <dt> - <a name = "color_name_parser"></a> - <strong>color_name_parser (line, i, opts)</strong> + <a name = "hsl_to_rgb"></a> + <strong>hsl_to_rgb (h, s, l)</strong> </dt> <dd> - Grab all the colour values from <code>vim.api.nvim_get_color_map</code> and create a lookup table. - COLOR_MAP is used to store the colour values + Converts an HSL color value to RGB. <h3>Parameters:</h3> <ul> - <li><span class="parameter">line</span> - string: Line to parse + <li><span class="parameter">h</span> + number: Hue </li> - <li><span class="parameter">i</span> - number: Index of line from where to start parsing + <li><span class="parameter">s</span> + number: Saturation </li> - <li><span class="parameter">opts</span> - table: Currently contains whether tailwind is enabled or not + <li><span class="parameter">l</span> + number: Lightness </li> </ul> + <h3>Returns:</h3> + <ol> + + number|nil,number|nil,number|nil + </ol> </dd> <dt> - <a name = "rgb_function_parser"></a> - <strong>rgb_function_parser (line, i)</strong> + <a name = "hsl_function_parser"></a> + <strong>hsl_function_parser (line, i)</strong> </dt> <dd> - Parse for rgb() css function and return rgb hex. + Parse for hsl() css function and return rgb hex. <h3>Parameters:</h3> @@ -179,7 +193,7 @@ <h3>Returns:</h3> <ol> <li> - number|nil: Index of line where the rgb function ended</li> + number|nil: Index of line where the hsl function ended</li> <li> string|nil: rgb hex value</li> </ol> @@ -189,14 +203,11 @@ </dd> <dt> - <a name = "rgba_function_parser"></a> - <strong>rgba_function_parser (line, i)</strong> + <a name = "hsla_function_parser"></a> + <strong>hsla_function_parser (line, i)</strong> </dt> <dd> - Parse for rgba() css function and return rgb hex. - Todo consider removing the regexes here - Todo this might not be the best approach to alpha channel. - Things like pumblend might be useful here. + Parse for hsl() css function and return rgb hex. <h3>Parameters:</h3> @@ -212,7 +223,7 @@ <h3>Returns:</h3> <ol> <li> - number|nil: Index of line where the rgba function ended</li> + number|nil: Index of line where the hsla function ended</li> <li> string|nil: rgb hex value</li> </ol> @@ -222,11 +233,73 @@ </dd> <dt> - <a name = "hsl_function_parser"></a> - <strong>hsl_function_parser (line, i)</strong> + <a name = "hue_to_rgb"></a> + <strong>hue_to_rgb (p, q, t)</strong> </dt> <dd> - Parse for hsl() css function and return rgb hex. + Convert hsl colour values to rgb. + Source: https://gist.github.com/mjackson/5311256 + + + <h3>Parameters:</h3> + <ul> + <li><span class="parameter">p</span> + number + </li> + <li><span class="parameter">q</span> + number + </li> + <li><span class="parameter">t</span> + number + </li> + </ul> + + <h3>Returns:</h3> + <ol> + + number + </ol> + + + + +</dd> + <dt> + <a name = "is_bright"></a> + <strong>is_bright (r, g, b)</strong> + </dt> + <dd> + Determine whether to use black or white text. </p> + +<p> ref: https://stackoverflow.com/a/1855903/837964 + https://stackoverflow.com/questions/596216/formula-to-determine-brightness-of-rgb-color + + + <h3>Parameters:</h3> + <ul> + <li><span class="parameter">r</span> + number: Red + </li> + <li><span class="parameter">g</span> + number: Green + </li> + <li><span class="parameter">b</span> + number: Blue + </li> + </ul> + + + + + +</dd> + <dt> + <a name = "name_parser"></a> + <strong>name_parser (line, i, opts)</strong> + </dt> + <dd> + Grab all the colour values from <code>vim.api.nvim_get_color_map</code> and create a lookup table. + COLOR_MAP is used to store the colour values <h3>Parameters:</h3> @@ -237,26 +310,22 @@ <li><span class="parameter">i</span> number: Index of line from where to start parsing </li> + <li><span class="parameter">opts</span> + table: Currently contains whether tailwind is enabled or not + </li> </ul> - <h3>Returns:</h3> - <ol> - <li> - number|nil: Index of line where the hsl function ended</li> - <li> - string|nil: rgb hex value</li> - </ol> </dd> <dt> - <a name = "hsla_function_parser"></a> - <strong>hsla_function_parser (line, i)</strong> + <a name = "rgb_function_parser"></a> + <strong>rgb_function_parser (line, i)</strong> </dt> <dd> - Parse for hsl() css function and return rgb hex. + Parse for rgb() css function and return rgb hex. <h3>Parameters:</h3> @@ -272,7 +341,7 @@ <h3>Returns:</h3> <ol> <li> - number|nil: Index of line where the hsla function ended</li> + number|nil: Index of line where the rgb function ended</li> <li> string|nil: rgb hex value</li> </ol> @@ -282,28 +351,30 @@ </dd> <dt> - <a name = "argb_hex_parser"></a> - <strong>argb_hex_parser (line, i)</strong> + <a name = "rgba_function_parser"></a> + <strong>rgba_function_parser (line, i)</strong> </dt> <dd> - parse for 0xaarrggbb and return rgb hex. - a format used in android apps + Parse for rgba() css function and return rgb hex. + Todo consider removing the regexes here + Todo this might not be the best approach to alpha channel. + Things like pumblend might be useful here. <h3>Parameters:</h3> <ul> <li><span class="parameter">line</span> - string: line to parse + string: Line to parse </li> <li><span class="parameter">i</span> - number: index of line from where to start parsing + number: Index of line from where to start parsing </li> </ul> <h3>Returns:</h3> <ol> <li> - number|nil: index of line where the hex value ended</li> + number|nil: Index of line where the rgba function ended</li> <li> string|nil: rgb hex value</li> </ol> diff --git a/doc/modules/colorizer.html b/doc/modules/colorizer.html index df8879c..e0b8bc8 100644 --- a/doc/modules/colorizer.html +++ b/doc/modules/colorizer.html @@ -41,9 +41,9 @@ <h2>Modules</h2> <ul class="nowrap"> <li><strong>colorizer</strong></li> - <li><a href="../modules/colorizer.buffer_utils.html">buffer_utils</a></li> - <li><a href="../modules/colorizer.color_utils.html">color_utils</a></li> - <li><a href="../modules/colorizer.matcher_utils.html">matcher_utils</a></li> + <li><a href="../modules/colorizer.buffer.html">buffer</a></li> + <li><a href="../modules/colorizer.color.html">color</a></li> + <li><a href="../modules/colorizer.matcher.html">matcher</a></li> <li><a href="../modules/colorizer.sass.html">sass</a></li> <li><a href="../modules/colorizer.tailwind.html">tailwind</a></li> <li><a href="../modules/colorizer.trie.html">trie</a></li> @@ -169,7 +169,7 @@ USE WITH LUA <table class="function_list"> <tr> <td class="name" nowrap><a href="#DEFAULT_NAMESPACE">DEFAULT_NAMESPACE</a></td> - <td class="summary">Default namespace used in <a href="../modules/colorizer.buffer_utils.html#highlight_buffer">colorizer.buffer_utils.highlight_buffer</a> and <a href="../modules/colorizer.html#attach_to_buffer">attach_to_buffer</a>.</td> + <td class="summary">Default namespace used in <a href="../modules/colorizer.buffer.html#highlight">colorizer.buffer.highlight</a> and <a href="../modules/colorizer.html#attach_to_buffer">attach_to_buffer</a>.</td> </tr> </table> @@ -193,7 +193,7 @@ USE WITH LUA <h3>See also:</h3> <ul> - <a href="../modules/colorizer.buffer_utils.html#highlight_buffer">colorizer.buffer_utils.highlight_buffer</a> + <a href="../modules/colorizer.buffer.html#highlight">colorizer.buffer.highlight</a> </ul> @@ -222,7 +222,7 @@ USE WITH LUA <h3>See also:</h3> <ul> - <a href="../modules/colorizer.html#highlight_buffer">highlight_buffer</a> + <a href="../modules/colorizer.buffer.html#highlight">colorizer.buffer.highlight</a> </ul> @@ -469,7 +469,7 @@ Setup an autocmd which enables colorizing for the filetypes and options specifie <strong>DEFAULT_NAMESPACE</strong> </dt> <dd> - Default namespace used in <a href="../modules/colorizer.buffer_utils.html#highlight_buffer">colorizer.buffer_utils.highlight_buffer</a> and <a href="../modules/colorizer.html#attach_to_buffer">attach_to_buffer</a>. + Default namespace used in <a href="../modules/colorizer.buffer.html#highlight">colorizer.buffer.highlight</a> and <a href="../modules/colorizer.html#attach_to_buffer">attach_to_buffer</a>. @@ -477,7 +477,7 @@ Setup an autocmd which enables colorizing for the filetypes and options specifie <h3>See also:</h3> <ul> - <li><a href="../modules/colorizer.buffer_utils.html#highlight_buffer">colorizer.buffer_utils.highlight_buffer</a></li> + <li><a href="../modules/colorizer.buffer.html#highlight">colorizer.buffer.highlight</a></li> <li><a href="../modules/colorizer.html#attach_to_buffer">attach_to_buffer</a></li> </ul> diff --git a/doc/modules/colorizer.matcher_utils.html b/doc/modules/colorizer.matcher.html index 569884f..9b2b67e 100644 --- a/doc/modules/colorizer.matcher_utils.html +++ b/doc/modules/colorizer.matcher.html @@ -39,9 +39,9 @@ <h2>Modules</h2> <ul class="nowrap"> <li><a href="../modules/colorizer.html">colorizer</a></li> - <li><a href="../modules/colorizer.buffer_utils.html">buffer_utils</a></li> - <li><a href="../modules/colorizer.color_utils.html">color_utils</a></li> - <li><strong>matcher_utils</strong></li> + <li><a href="../modules/colorizer.buffer.html">buffer</a></li> + <li><a href="../modules/colorizer.color.html">color</a></li> + <li><strong>matcher</strong></li> <li><a href="../modules/colorizer.sass.html">sass</a></li> <li><a href="../modules/colorizer.tailwind.html">tailwind</a></li> <li><a href="../modules/colorizer.trie.html">trie</a></li> @@ -52,7 +52,7 @@ <div id="content"> -<h1>Module <code>colorizer.matcher_utils</code></h1> +<h1>Module <code>colorizer.matcher</code></h1> <p>Helper functions for colorizer to enable required parsers</p> <p> @@ -62,7 +62,11 @@ <h2><a href="#Functions">Functions</a></h2> <table class="function_list"> <tr> - <td class="name" nowrap><a href="#make_matcher">make_matcher (options)</a></td> + <td class="name" nowrap><a href="#compile">compile (matchers, matchers_trie)</a></td> + <td class="summary">Form a trie stuct with the given prefixes</td> + </tr> + <tr> + <td class="name" nowrap><a href="#make">make (options)</a></td> <td class="summary">Parse the given options and return a function with enabled parsers.</td> </tr> </table> @@ -75,8 +79,36 @@ <dl class="function"> <dt> - <a name = "make_matcher"></a> - <strong>make_matcher (options)</strong> + <a name = "compile"></a> + <strong>compile (matchers, matchers_trie)</strong> + </dt> + <dd> + Form a trie stuct with the given prefixes + + + <h3>Parameters:</h3> + <ul> + <li><span class="parameter">matchers</span> + table: List of prefixes, {"rgb", "hsl"} + </li> + <li><span class="parameter">matchers_trie</span> + table: Table containing information regarding non-trie based parsers + </li> + </ul> + + <h3>Returns:</h3> + <ol> + + function: function which will just parse the line for enabled parsers + </ol> + + + + +</dd> + <dt> + <a name = "make"></a> + <strong>make (options)</strong> </dt> <dd> Parse the given options and return a function with enabled parsers. diff --git a/doc/modules/colorizer.sass.html b/doc/modules/colorizer.sass.html index 1abd56d..56c8c63 100644 --- a/doc/modules/colorizer.sass.html +++ b/doc/modules/colorizer.sass.html @@ -39,9 +39,9 @@ <h2>Modules</h2> <ul class="nowrap"> <li><a href="../modules/colorizer.html">colorizer</a></li> - <li><a href="../modules/colorizer.buffer_utils.html">buffer_utils</a></li> - <li><a href="../modules/colorizer.color_utils.html">color_utils</a></li> - <li><a href="../modules/colorizer.matcher_utils.html">matcher_utils</a></li> + <li><a href="../modules/colorizer.buffer.html">buffer</a></li> + <li><a href="../modules/colorizer.color.html">color</a></li> + <li><a href="../modules/colorizer.matcher.html">matcher</a></li> <li><strong>sass</strong></li> <li><a href="../modules/colorizer.tailwind.html">tailwind</a></li> <li><a href="../modules/colorizer.trie.html">trie</a></li> @@ -62,17 +62,17 @@ <h2><a href="#Functions">Functions</a></h2> <table class="function_list"> <tr> - <td class="name" nowrap><a href="#sass_cleanup">sass_cleanup (buf)</a></td> + <td class="name" nowrap><a href="#cleanup">cleanup (buf)</a></td> <td class="summary">Cleanup sass variables and watch handlers</td> </tr> <tr> - <td class="name" nowrap><a href="#sass_name_parser">sass_name_parser (line, i, buf)</a></td> + <td class="name" nowrap><a href="#name_parser">name_parser (line, i, buf)</a></td> <td class="summary">Parse the given line for sass color names check for value in SASS[buf].DEFINITIONS_ALL</td> </tr> <tr> - <td class="name" nowrap><a href="#sass_update_variables">sass_update_variables (buf, line_start, line_end, lines, color_parser, options, options_local)</a></td> - <td class="summary">Parse the given lines for sass variabled and add to SASS[buf].DEFINITIONS_ALL.</td> + <td class="name" nowrap><a href="#update_variables">update_variables (buf, line_start, line_end, lines, color_parser, options, options_local)</a></td> + <td class="summary">Parse the given lines for sass variabled and add to <code>SASS[buf].DEFINITIONS_ALL</code>.</td> </tr> </table> @@ -84,8 +84,8 @@ <dl class="function"> <dt> - <a name = "sass_cleanup"></a> - <strong>sass_cleanup (buf)</strong> + <a name = "cleanup"></a> + <strong>cleanup (buf)</strong> </dt> <dd> Cleanup sass variables and watch handlers @@ -104,8 +104,8 @@ </dd> <dt> - <a name = "sass_name_parser"></a> - <strong>sass_name_parser (line, i, buf)</strong> + <a name = "name_parser"></a> + <strong>name_parser (line, i, buf)</strong> </dt> <dd> Parse the given line for sass color names @@ -136,13 +136,13 @@ </dd> <dt> - <a name = "sass_update_variables"></a> - <strong>sass_update_variables (buf, line_start, line_end, lines, color_parser, options, options_local)</strong> + <a name = "update_variables"></a> + <strong>update_variables (buf, line_start, line_end, lines, color_parser, options, options_local)</strong> </dt> <dd> - Parse the given lines for sass variabled and add to SASS[buf].DEFINITIONS<em>ALL. - which is then used in |sass</em>name<em>parser| - If lines are not given, then fetch the lines with line</em>start and line_end + Parse the given lines for sass variabled and add to <code>SASS[buf].DEFINITIONS_ALL</code>. + which is then used in |sass<em>name</em>parser| + If lines are not given, then fetch the lines with line<em>start and line</em>end <h3>Parameters:</h3> diff --git a/doc/modules/colorizer.tailwind.html b/doc/modules/colorizer.tailwind.html index ae4d64b..09e17ce 100644 --- a/doc/modules/colorizer.tailwind.html +++ b/doc/modules/colorizer.tailwind.html @@ -39,9 +39,9 @@ <h2>Modules</h2> <ul class="nowrap"> <li><a href="../modules/colorizer.html">colorizer</a></li> - <li><a href="../modules/colorizer.buffer_utils.html">buffer_utils</a></li> - <li><a href="../modules/colorizer.color_utils.html">color_utils</a></li> - <li><a href="../modules/colorizer.matcher_utils.html">matcher_utils</a></li> + <li><a href="../modules/colorizer.buffer.html">buffer</a></li> + <li><a href="../modules/colorizer.color.html">color</a></li> + <li><a href="../modules/colorizer.matcher.html">matcher</a></li> <li><a href="../modules/colorizer.sass.html">sass</a></li> <li><strong>tailwind</strong></li> <li><a href="../modules/colorizer.trie.html">trie</a></li> @@ -62,13 +62,13 @@ <h2><a href="#Functions">Functions</a></h2> <table class="function_list"> <tr> - <td class="name" nowrap><a href="#tailwind_cleanup">tailwind_cleanup (buf)</a></td> + <td class="name" nowrap><a href="#cleanup">cleanup (buf)</a></td> <td class="summary">Cleanup tailwind variables and autocmd</td> </tr> <tr> - <td class="name" nowrap><a href="#tailwind_setup_lsp_colors">tailwind_setup_lsp_colors (buf, options, options_local, add_highlight)</a></td> + <td class="name" nowrap><a href="#setup_lsp_colors">setup_lsp_colors (buf, options, options_local, add_highlight)</a></td> <td class="summary">highlight buffer using values returned by tailwindcss - To see these table information, see |colorizer.buffer_utils|</td> + To see these table information, see |colorizer.buffer|</td> </tr> </table> @@ -80,8 +80,8 @@ <dl class="function"> <dt> - <a name = "tailwind_cleanup"></a> - <strong>tailwind_cleanup (buf)</strong> + <a name = "cleanup"></a> + <strong>cleanup (buf)</strong> </dt> <dd> Cleanup tailwind variables and autocmd @@ -100,12 +100,12 @@ </dd> <dt> - <a name = "tailwind_setup_lsp_colors"></a> - <strong>tailwind_setup_lsp_colors (buf, options, options_local, add_highlight)</strong> + <a name = "setup_lsp_colors"></a> + <strong>setup_lsp_colors (buf, options, options_local, add_highlight)</strong> </dt> <dd> highlight buffer using values returned by tailwindcss - To see these table information, see |colorizer.buffer_utils| + To see these table information, see |colorizer.buffer| <h3>Parameters:</h3> diff --git a/doc/modules/colorizer.trie.html b/doc/modules/colorizer.trie.html index c9433fc..cc64359 100644 --- a/doc/modules/colorizer.trie.html +++ b/doc/modules/colorizer.trie.html @@ -35,9 +35,9 @@ <h2>Modules</h2> <ul class="nowrap"> <li><a href="../modules/colorizer.html">colorizer</a></li> - <li><a href="../modules/colorizer.buffer_utils.html">buffer_utils</a></li> - <li><a href="../modules/colorizer.color_utils.html">color_utils</a></li> - <li><a href="../modules/colorizer.matcher_utils.html">matcher_utils</a></li> + <li><a href="../modules/colorizer.buffer.html">buffer</a></li> + <li><a href="../modules/colorizer.color.html">color</a></li> + <li><a href="../modules/colorizer.matcher.html">matcher</a></li> <li><a href="../modules/colorizer.sass.html">sass</a></li> <li><a href="../modules/colorizer.tailwind.html">tailwind</a></li> <li><strong>trie</strong></li> diff --git a/doc/modules/utils.html b/doc/modules/utils.html index 9170c4e..ecb555b 100644 --- a/doc/modules/utils.html +++ b/doc/modules/utils.html @@ -39,9 +39,9 @@ <h2>Modules</h2> <ul class="nowrap"> <li><a href="../modules/colorizer.html">colorizer</a></li> - <li><a href="../modules/colorizer.buffer_utils.html">buffer_utils</a></li> - <li><a href="../modules/colorizer.color_utils.html">color_utils</a></li> - <li><a href="../modules/colorizer.matcher_utils.html">matcher_utils</a></li> + <li><a href="../modules/colorizer.buffer.html">buffer</a></li> + <li><a href="../modules/colorizer.color.html">color</a></li> + <li><a href="../modules/colorizer.matcher.html">matcher</a></li> <li><a href="../modules/colorizer.sass.html">sass</a></li> <li><a href="../modules/colorizer.tailwind.html">tailwind</a></li> <li><a href="../modules/colorizer.trie.html">trie</a></li> @@ -70,6 +70,10 @@ <td class="summary">Obvious.</td> </tr> <tr> + <td class="name" nowrap><a href="#get_last_modified">get_last_modified (path)</a></td> + <td class="summary">Get last modified time of a file</td> + </tr> + <tr> <td class="name" nowrap><a href="#merge">merge (...)</a></td> <td class="summary">Merge two tables.</td> </tr> @@ -82,10 +86,6 @@ <td class="summary">Obvious.</td> </tr> <tr> - <td class="name" nowrap><a href="#get_last_modified">get_last_modified (path)</a></td> - <td class="summary">Get last modified time of a file</td> - </tr> - <tr> <td class="name" nowrap><a href="#watch_file">watch_file (path, callback, ...)</a></td> <td class="summary">Watch a file for changes and execute callback</td> </tr> @@ -149,6 +149,31 @@ </dd> <dt> + <a name = "get_last_modified"></a> + <strong>get_last_modified (path)</strong> + </dt> + <dd> + Get last modified time of a file + + + <h3>Parameters:</h3> + <ul> + <li><span class="parameter">path</span> + string: file path + </li> + </ul> + + <h3>Returns:</h3> + <ol> + + number|nil: modified time + </ol> + + + + +</dd> + <dt> <a name = "merge"></a> <strong>merge (...)</strong> </dt> @@ -228,31 +253,6 @@ </dd> <dt> - <a name = "get_last_modified"></a> - <strong>get_last_modified (path)</strong> - </dt> - <dd> - Get last modified time of a file - - - <h3>Parameters:</h3> - <ul> - <li><span class="parameter">path</span> - string: file path - </li> - </ul> - - <h3>Returns:</h3> - <ol> - - number|nil: modified time - </ol> - - - - -</dd> - <dt> <a name = "watch_file"></a> <strong>watch_file (path, callback, ...)</strong> </dt> diff --git a/lua/colorizer.lua b/lua/colorizer.lua index 90b964a..e373e02 100644 --- a/lua/colorizer.lua +++ b/lua/colorizer.lua @@ -57,22 +57,9 @@ -- @see colorizer.attach_to_buffer -- @see colorizer.detach_from_buffer -local buffer_utils = require "colorizer.buffer_utils" - ----Default namespace used in `colorizer.buffer_utils.highlight_buffer` and `attach_to_buffer`. --- @see colorizer.buffer_utils.highlight_buffer --- @see attach_to_buffer -local DEFAULT_NAMESPACE = buffer_utils.DEFAULT_NAMESPACE - -local HIGHLIGHT_MODE_NAMES = buffer_utils.HIGHLIGHT_MODE_NAMES +local buffer_utils = require "colorizer.buffer" local clear_hl_cache = buffer_utils.clear_hl_cache -local rehighlight_buffer = buffer_utils.rehighlight_buffer - -local get_buffer_options ----Highlight the buffer region ----@function highlight_buffer --- @see colorizer.buffer_utils.highlight_buffer -local highlight_buffer = buffer_utils.highlight_buffer +local rehighlight_buffer = buffer_utils.rehighlight local utils = require "colorizer.utils" local merge = utils.merge @@ -84,6 +71,18 @@ local buf_get_option = api.nvim_buf_get_option local clear_namespace = api.nvim_buf_clear_namespace local current_buf = api.nvim_get_current_buf +local colorizer = {} + +---Default namespace used in `colorizer.buffer.highlight` and `attach_to_buffer`. +-- @see colorizer.buffer.highlight +-- @see attach_to_buffer +colorizer.DEFAULT_NAMESPACE = buffer_utils.default_namespace + +---Highlight the buffer region +---@function highlight_buffer +-- @see colorizer.buffer.highlight +colorizer.highlight_buffer = buffer_utils.highlight + -- USER FACING FUNCTIONALITY -- local AUGROUP_ID local AUGROUP_NAME = "ColorizerSetup" @@ -168,8 +167,8 @@ end --- Check if attached to a buffer. ---@param buf number|nil: A value of 0 implies the current buffer. ---@return number|nil: if attached to the buffer, false otherwise. ----@see highlight_buffer -local function is_buffer_attached(buf) +---@see colorizer.buffer.highlight +function colorizer.is_buffer_attached(buf) if buf == 0 or buf == nil then buf = current_buf() else @@ -194,13 +193,13 @@ end --- Stop highlighting the current buffer. ---@param buf number|nil: buf A value of 0 or nil implies the current buffer. ---@param ns number|nil: ns the namespace id, if not given DEFAULT_NAMESPACE is used -local function detach_from_buffer(buf, ns) - buf = is_buffer_attached(buf) +function colorizer.detach_from_buffer(buf, ns) + buf = colorizer.is_buffer_attached(buf) if not buf then return end - clear_namespace(buf, ns or DEFAULT_NAMESPACE, 0, -1) + clear_namespace(buf, ns or colorizer.DEFAULT_NAMESPACE, 0, -1) if BUFFER_LOCAL[buf] then for _, namespace in pairs(BUFFER_LOCAL[buf].__detach.ns) do clear_namespace(buf, namespace, 0, -1) @@ -227,7 +226,7 @@ end ---@param buf integer: A value of 0 implies the current buffer. ---@param options table|nil: Configuration options as described in `setup` ---@param typ string|nil: "buf" or "file" - The type of buffer option -local function attach_to_buffer(buf, options, typ) +function colorizer.attach_to_buffer(buf, options, typ) if buf == 0 or buf == nil then buf = current_buf() else @@ -239,7 +238,7 @@ local function attach_to_buffer(buf, options, typ) -- if the buffer is already attached then grab those options if not options then - options = get_buffer_options(buf) + options = colorizer.get_buffer_options(buf) end -- if not make new options @@ -247,7 +246,7 @@ local function attach_to_buffer(buf, options, typ) options = new_buffer_options(buf, typ) end - if not HIGHLIGHT_MODE_NAMES[options.mode] then + if not buffer_utils.highlight_mode_names[options.mode] then if options.mode ~= nil then local mode = options.mode vim.defer_fn(function() @@ -320,7 +319,7 @@ local function attach_to_buffer(buf, options, typ) buffer = buf, callback = function() if BUFFER_OPTIONS[buf] then - detach_from_buffer(buf) + colorizer.detach_from_buffer(buf) end BUFFER_LOCAL[buf].__init = nil end, @@ -351,7 +350,7 @@ end --For all user_default_options, see |user_default_options| ---@param config table: Config containing above parameters. ---@usage `require'colorizer'.setup()` -local function setup(config) +function colorizer.setup(config) if not vim.opt.termguicolors then vim.schedule(function() vim.notify("Colorizer: Error: &termguicolors must be set", "Error") @@ -383,7 +382,7 @@ local function setup(config) -- 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) + colorizer.detach_from_buffer(buf) end BUFFER_LOCAL[buf].__init = nil return @@ -409,7 +408,7 @@ local function setup(config) -- but BufWinEnter also triggers for split formation -- but we don't want that so add a check using local buffer variable if not BUFFER_LOCAL[buf].__init then - attach_to_buffer(buf, options, typ) + colorizer.attach_to_buffer(buf, options, typ) end end @@ -472,35 +471,24 @@ end --- Return the currently active buffer options. ---@param buf number|nil: Buffer number ---@return table|nil -function get_buffer_options(buf) - local buffer = is_buffer_attached(buf) +function colorizer.get_buffer_options(buf) + local buffer = colorizer.is_buffer_attached(buf) if buffer then return BUFFER_OPTIONS[buffer] end end --- Reload all of the currently active highlighted buffers. -local function reload_all_buffers() +function colorizer.reload_all_buffers() for buf, _ in pairs(BUFFER_OPTIONS) do - attach_to_buffer(buf, get_buffer_options(buf)) + colorizer.attach_to_buffer(buf, colorizer.get_buffer_options(buf)) end end --- Clear the highlight cache and reload all buffers. -local function clear_highlight_cache() +function colorizer.clear_highlight_cache() clear_hl_cache() - vim.schedule(reload_all_buffers) + vim.schedule(colorizer.reload_all_buffers) end ---- @export -return { - DEFAULT_NAMESPACE = DEFAULT_NAMESPACE, - setup = setup, - is_buffer_attached = is_buffer_attached, - attach_to_buffer = attach_to_buffer, - detach_from_buffer = detach_from_buffer, - highlight_buffer = highlight_buffer, - reload_all_buffers = reload_all_buffers, - get_buffer_options = get_buffer_options, - clear_highlight_cache = clear_highlight_cache, -} +return colorizer diff --git a/lua/colorizer/buffer_utils.lua b/lua/colorizer/buffer.lua index 1f389c2..3b31daf 100644 --- a/lua/colorizer/buffer_utils.lua +++ b/lua/colorizer/buffer.lua @@ -1,5 +1,5 @@ ---Helper functions to highlight buffer smartly ---@module colorizer.buffer_utils +--@module colorizer.buffer local api = vim.api local buf_set_virtual_text = api.nvim_buf_set_extmark local buf_get_lines = api.nvim_buf_get_lines @@ -7,76 +7,51 @@ local create_namespace = api.nvim_create_namespace local clear_namespace = api.nvim_buf_clear_namespace local set_highlight = api.nvim_set_hl -local color_utils = require "colorizer.color_utils" -local color_is_bright = color_utils.color_is_bright +local color = require "colorizer.color" +local color_is_bright = color.is_bright + +local make_matcher = require("colorizer.matcher").make local sass = require "colorizer.sass" -local sass_update_variables = sass.sass_update_variables -local sass_cleanup = sass.sass_cleanup +local sass_update_variables = sass.update_variables +local sass_cleanup = sass.cleanup local tailwind = require "colorizer.tailwind" -local tailwind_setup_lsp = tailwind.tailwind_setup_lsp_colors -local tailwind_cleanup = tailwind.tailwind_cleanup +local tailwind_setup_lsp = tailwind.setup_lsp_colors +local tailwind_cleanup = tailwind.cleanup -local make_matcher = require("colorizer.matcher_utils").make_matcher +local buffer = {} -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" local HIGHLIGHT_NAME_PREFIX = "colorizer" +local HIGHLIGHT_CACHE = {} + +--- Default namespace used in `highlight` and `colorizer.attach_to_buffer`. +-- @see highlight +-- @see colorizer.attach_to_buffer +buffer.default_namespace = create_namespace "colorizer" + --- Highlight mode which will be use to render the colour -local HIGHLIGHT_MODE_NAMES = { +buffer.highlight_mode_names = { background = "mb", foreground = "mf", virtualtext = "mv", } -local HIGHLIGHT_CACHE = {} - -local function parse_lines(buf, lines, line_start, options) - local loop_parse_fn = make_matcher(options) - if not loop_parse_fn then - return false - end - - local data = {} - for current_linenum, line in ipairs(lines) do - current_linenum = current_linenum - 1 + line_start - data[current_linenum] = data[current_linenum] or {} - - -- Upvalues are options and current_linenum - local i = 1 - while i < #line do - local length, rgb_hex = loop_parse_fn(line, i, buf) - if length and rgb_hex then - table.insert(data[current_linenum], { rgb_hex = rgb_hex, range = { i - 1, i + length - 1 } }) - i = i + length - else - i = i + 1 - end - end - end - - return data -end --- Clean the highlight cache -local function clear_hl_cache() +function buffer.clear_hl_cache() HIGHLIGHT_CACHE = {} end --- Make a deterministic name for a highlight given these attributes local function make_highlight_name(rgb, mode) - return table.concat({ HIGHLIGHT_NAME_PREFIX, HIGHLIGHT_MODE_NAMES[mode], rgb }, "_") + return table.concat({ HIGHLIGHT_NAME_PREFIX, buffer.highlight_mode_names[mode], rgb }, "_") end local function create_highlight(rgb_hex, mode) mode = mode or "background" -- TODO validate rgb format? rgb_hex = rgb_hex:lower() - local cache_key = table.concat({ HIGHLIGHT_MODE_NAMES[mode], rgb_hex }, "_") + local cache_key = table.concat({ buffer.highlight_mode_names[mode], rgb_hex }, "_") local highlight_name = HIGHLIGHT_CACHE[cache_key] -- Look up in our cache. @@ -112,7 +87,14 @@ local function create_highlight(rgb_hex, mode) return highlight_name end -local function add_highlight(options, buf, ns, data, line_start, line_end) +--- Create highlight and set highlights +---@param buf number +---@param ns number +---@param line_start number +---@param line_end number +---@param data table: table output of `parse_lines` +---@param options table: Passed in setup, mainly for `user_default_options` +function buffer.add_highlight(buf, ns, line_start, line_end, data, options) clear_namespace(buf, ns, line_start, line_end) local mode = options.mode == "background" and "background" or "foreground" @@ -140,38 +122,75 @@ end -- Highlight starting from `line_start` (0-indexed) for each line described by `lines` in the -- buffer `buf` and attach it to the namespace `ns`. ---@param buf number: buffer id ----@param ns number: The namespace id. Default is DEFAULT_NAMESPACE. Create it with `vim.api.create_namespace` ----@param lines table: the lines to highlight from the buffer. +---@param ns number: The namespace id. Default is DEFAULT_NAMESPACE. Create it with `vim.api.nvim_create_namespace` ---@param line_start number: line_start should be 0-indexed ---@param line_end number: Last line to highlight ---@param options table: Configuration options as described in `setup` ---@param options_local table: Buffer local variables ---@return nil|boolean|number,table -function highlight_buffer(buf, ns, lines, line_start, line_end, options, options_local) +function buffer.highlight(buf, ns, line_start, line_end, options, options_local) local returns = { detach = { ns = {}, functions = {} } } if buf == 0 or buf == nil then buf = api.nvim_get_current_buf() end - ns = ns or DEFAULT_NAMESPACE + local lines = buf_get_lines(buf, line_start, line_end, false) - -- only update sass varibled when text is changed + ns = ns or buffer.default_namespace + + -- only update sass varibles when text is changed if options_local.__event ~= "WinScrolled" and options.sass and options.sass.enable then table.insert(returns.detach.functions, sass_cleanup) sass_update_variables(buf, 0, -1, nil, make_matcher(options.sass.parsers or { css = true }), options, options_local) end - local data = parse_lines(buf, lines, line_start, options) - add_highlight(options, buf, ns, data, line_start, line_end) + local data = buffer.parse_lines(buf, lines, line_start, options) or {} + buffer.add_highlight(buf, ns, line_start, line_end, data, options) if options.tailwind == "lsp" or options.tailwind == "both" then - tailwind_setup_lsp(buf, options, options_local, add_highlight) + tailwind_setup_lsp(buf, options, options_local, buffer.add_highlight) table.insert(returns.detach.functions, tailwind_cleanup) end return true, returns end +--- Parse the given lines for colors and return a table containing +-- rgb_hex and range per line +---@param buf number +---@param lines table +---@param line_start number: This is the buffer line number, from where to start highlighting +---@param options table: Passed in `colorizer.setup`, Only uses `user_default_options` +---@return table|nil +function buffer.parse_lines(buf, lines, line_start, options) + local loop_parse_fn = make_matcher(options) + if not loop_parse_fn then + return + end + + local data = {} + for current_linenum, line in ipairs(lines) do + current_linenum = current_linenum - 1 + line_start + data[current_linenum] = data[current_linenum] or {} + + -- Upvalues are options and current_linenum + local i = 1 + while i < #line do + local length, rgb_hex = loop_parse_fn(line, i, buf) + if length and rgb_hex then + table.insert(data[current_linenum], { rgb_hex = rgb_hex, range = { i - 1, i + length - 1 } }) + i = i + length + else + i = i + 1 + end + end + end + + return data +end + +-- gets used in rehighlight function only +local BUFFER_LINES = {} -- get the amount lines to highlight local function getrow(buf) if not BUFFER_LINES[buf] then @@ -222,12 +241,12 @@ end ---@param options_local table|nil: Buffer local variables ---@param use_local_lines boolean|nil Whether to use lines num range from options_local ---@return nil|boolean|number,table -function rehighlight_buffer(buf, options, options_local, use_local_lines) +function buffer.rehighlight(buf, options, options_local, use_local_lines) if buf == 0 or buf == nil then buf = api.nvim_get_current_buf() end - local ns = DEFAULT_NAMESPACE + local ns = buffer.default_namespace local min, max if use_local_lines and options_local then @@ -235,20 +254,13 @@ function rehighlight_buffer(buf, options, options_local, use_local_lines) else min, max = getrow(buf) end - local lines = buf_get_lines(buf, min, max, false) - local bool, returns = highlight_buffer(buf, ns, lines, min, max, options, options_local or {}) + local bool, returns = buffer.highlight(buf, ns, min, max, options, options_local or {}) table.insert(returns.detach.functions, function() BUFFER_LINES[buf] = nil end) + return bool, returns end ---- @export -return { - DEFAULT_NAMESPACE = DEFAULT_NAMESPACE, - HIGHLIGHT_MODE_NAMES = HIGHLIGHT_MODE_NAMES, - clear_hl_cache = clear_hl_cache, - rehighlight_buffer = rehighlight_buffer, - highlight_buffer = highlight_buffer, -} +return buffer diff --git a/lua/colorizer/color_utils.lua b/lua/colorizer/color.lua index 2a7e7ab..711c8e4 100644 --- a/lua/colorizer/color_utils.lua +++ b/lua/colorizer/color.lua @@ -1,5 +1,11 @@ ---Helper functions to parse different colour formats ---@module colorizer.color_utils +--@module colorizer.color +local api = vim.api + +local bit = require "bit" +local floor, min, max = math.floor, math.min, math.max +local band, rshift, lshift, tohex = bit.band, bit.rshift, bit.lshift, bit.tohex + local Trie = require "colorizer.trie" local utils = require "colorizer.utils" @@ -8,27 +14,153 @@ local byte_is_hex = utils.byte_is_hex local parse_hex = utils.parse_hex local percent_or_hex = utils.percent_or_hex -local bit = require "bit" -local floor, min, max = math.floor, math.min, math.max -local band, rshift, lshift, tohex = bit.band, bit.rshift, bit.lshift, bit.tohex +local color = {} -local api = vim.api +local ARGB_MINIMUM_LENGTH = #"0xAARRGGBB" - 1 +---parse for 0xaarrggbb and return rgb hex. +-- a format used in android apps +---@param line string: line to parse +---@param i number: index of line from where to start parsing +---@return number|nil: index of line where the hex value ended +---@return string|nil: rgb hex value +function color.argb_hex_parser(line, i) + if #line < i + ARGB_MINIMUM_LENGTH then + return + end ----Determine whether to use black or white text. --- --- ref: https://stackoverflow.com/a/1855903/837964 --- https://stackoverflow.com/questions/596216/formula-to-determine-brightness-of-rgb-color ----@param r number: Red ----@param g number: Green ----@param b number: Blue -local function color_is_bright(r, g, b) - -- counting the perceptive luminance - human eye favors green color - local luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255 - if luminance > 0.5 then - return true -- bright colors, black font + local j = i + 2 + + local n = j + 8 + local alpha + local v = 0 + while j <= min(n, #line) do + local b = line:byte(j) + if not byte_is_hex(b) then + break + end + if j - i <= 3 then + alpha = parse_hex(b) + lshift(alpha or 0, 4) + else + v = parse_hex(b) + lshift(v, 4) + end + j = j + 1 + end + if #line >= j and byte_is_alphanumeric(line:byte(j)) then + return + end + local length = j - i + if length ~= 10 then + return + end + alpha = tonumber(alpha) / 255 + local r = floor(band(rshift(v, 16), 0xFF) * alpha) + local g = floor(band(rshift(v, 8), 0xFF) * alpha) + local b = floor(band(v, 0xFF) * alpha) + local rgb_hex = string.format("%02x%02x%02x", r, g, b) + return length, rgb_hex +end + +--- Converts an HSL color value to RGB. +---@param h number: Hue +---@param s number: Saturation +---@param l number: Lightness +---@return number|nil,number|nil,number|nil +function color.hsl_to_rgb(h, s, l) + if h > 1 or s > 1 or l > 1 then + return + end + if s == 0 then + local r = l * 255 + return r, r, r + end + local q + if l < 0.5 then + q = l * (1 + s) else - return false -- dark colors, white font + q = l + s - l * s + end + local p = 2 * l - q + return 255 * color.hue_to_rgb(p, q, h + 1 / 3), + 255 * color.hue_to_rgb(p, q, h), + 255 * color.hue_to_rgb(p, q, h - 1 / 3) +end + +local CSS_HSL_FN_MINIMUM_LENGTH = #"hsl(0,0%,0%)" - 1 +---Parse for hsl() css function and return rgb hex. +---@param line string: Line to parse +---@param i number: Index of line from where to start parsing +---@return number|nil: Index of line where the hsl function ended +---@return string|nil: rgb hex value +function color.hsl_function_parser(line, i) + if #line < i + CSS_HSL_FN_MINIMUM_LENGTH then + return end + local h, s, l, match_end = line:sub(i):match "^hsl%(%s*(%d+)%s*,%s*(%d+)%%%s*,%s*(%d+)%%%s*%)()" + if not match_end then + h, s, l, match_end = line:sub(i):match "^hsl%(%s*(%d+)%s+(%d+)%%%s+(%d+)%%%s*%)()" + if not match_end then + return + end + end + h = tonumber(h) + if h > 360 then + return + end + s = tonumber(s) + if s > 100 then + return + end + l = tonumber(l) + if l > 100 then + return + end + local r, g, b = color.hsl_to_rgb(h / 360, s / 100, l / 100) + if r == nil or g == nil or b == nil then + return + end + local rgb_hex = string.format("%02x%02x%02x", r, g, b) + return match_end - 1, rgb_hex +end + +local CSS_HSLA_FN_MINIMUM_LENGTH = #"hsla(0,0%,0%,0)" - 1 +---Parse for hsl() css function and return rgb hex. +---@param line string: Line to parse +---@param i number: Index of line from where to start parsing +---@return number|nil: Index of line where the hsla function ended +---@return string|nil: rgb hex value +function color.hsla_function_parser(line, i) + if #line < i + CSS_HSLA_FN_MINIMUM_LENGTH then + return + end + local h, s, l, a, match_end = line:sub(i):match "^hsla%(%s*(%d+)%s*,%s*(%d+)%%%s*,%s*(%d+)%%%s*,%s*([.%d]+)%s*%)()" + if not match_end then + h, s, l, a, match_end = line:sub(i):match "^hsla%(%s*(%d+)%s+(%d+)%%%s+(%d+)%%%s+([.%d]+)%s*%)()" + if not match_end then + return + end + end + a = tonumber(a) + if not a or a > 1 then + return + end + h = tonumber(h) + if h > 360 then + return + end + s = tonumber(s) + if s > 100 then + return + end + l = tonumber(l) + if l > 100 then + return + end + local r, g, b = color.hsl_to_rgb(h / 360, s / 100, l / 100) + if r == nil or g == nil or b == nil then + return + end + local rgb_hex = string.format("%02x%02x%02x", r * a, g * a, b * a) + return match_end - 1, rgb_hex end ---Convert hsl colour values to rgb. @@ -37,7 +169,7 @@ end ---@param q number ---@param t number ---@return number -local function hue_to_rgb(p, q, t) +function color.hue_to_rgb(p, q, t) if t < 0 then t = t + 1 end @@ -56,6 +188,23 @@ local function hue_to_rgb(p, q, t) return p end +---Determine whether to use black or white text. +-- +-- ref: https://stackoverflow.com/a/1855903/837964 +-- https://stackoverflow.com/questions/596216/formula-to-determine-brightness-of-rgb-color +---@param r number: Red +---@param g number: Green +---@param b number: Blue +function color.is_bright(r, g, b) + -- counting the perceptive luminance - human eye favors green color + local luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255 + if luminance > 0.5 then + return true -- bright colors, black font + else + return false -- dark colors, white font + end +end + local COLOR_MAP local COLOR_TRIE local COLOR_NAME_MINLEN, COLOR_NAME_MAXLEN @@ -66,7 +215,7 @@ local TAILWIND_ENABLED = false ---@param line string: Line to parse ---@param i number: Index of line from where to start parsing ---@param opts table: Currently contains whether tailwind is enabled or not -local function color_name_parser(line, i, opts) +function color.name_parser(line, i, opts) --- Setup the COLOR_MAP and COLOR_TRIE if not COLOR_TRIE or opts.tailwind ~= TAILWIND_ENABLED then COLOR_MAP = {} @@ -124,36 +273,13 @@ local function color_name_parser(line, i, opts) end end ---- Converts an HSL color value to RGB. ----@param h number: Hue ----@param s number: Saturation ----@param l number: Lightness ----@return number|nil,number|nil,number|nil -local function hsl_to_rgb(h, s, l) - if h > 1 or s > 1 or l > 1 then - return - end - if s == 0 then - local r = l * 255 - return r, r, r - end - local q - if l < 0.5 then - q = l * (1 + s) - else - q = l + s - l * s - end - local p = 2 * l - q - return 255 * hue_to_rgb(p, q, h + 1 / 3), 255 * hue_to_rgb(p, q, h), 255 * hue_to_rgb(p, q, h - 1 / 3) -end - local CSS_RGB_FN_MINIMUM_LENGTH = #"rgb(0,0,0)" - 1 ---Parse for rgb() css function and return rgb hex. ---@param line string: Line to parse ---@param i number: Index of line from where to start parsing ---@return number|nil: Index of line where the rgb function ended ---@return string|nil: rgb hex value -local function rgb_function_parser(line, i) +function color.rgb_function_parser(line, i) if #line < i + CSS_RGB_FN_MINIMUM_LENGTH then return end @@ -189,7 +315,7 @@ local CSS_RGBA_FN_MINIMUM_LENGTH = #"rgba(0,0,0,0)" - 1 ---@param i number: Index of line from where to start parsing ---@return number|nil: Index of line where the rgba function ended ---@return string|nil: rgb hex value -local function rgba_function_parser(line, i) +function color.rgba_function_parser(line, i) if #line < i + CSS_RGBA_FN_MINIMUM_LENGTH then return end @@ -221,128 +347,6 @@ local function rgba_function_parser(line, i) return match_end - 1, rgb_hex end -local CSS_HSL_FN_MINIMUM_LENGTH = #"hsl(0,0%,0%)" - 1 ----Parse for hsl() css function and return rgb hex. ----@param line string: Line to parse ----@param i number: Index of line from where to start parsing ----@return number|nil: Index of line where the hsl function ended ----@return string|nil: rgb hex value -local function hsl_function_parser(line, i) - if #line < i + CSS_HSL_FN_MINIMUM_LENGTH then - return - end - local h, s, l, match_end = line:sub(i):match "^hsl%(%s*(%d+)%s*,%s*(%d+)%%%s*,%s*(%d+)%%%s*%)()" - if not match_end then - h, s, l, match_end = line:sub(i):match "^hsl%(%s*(%d+)%s+(%d+)%%%s+(%d+)%%%s*%)()" - if not match_end then - return - end - end - h = tonumber(h) - if h > 360 then - return - end - s = tonumber(s) - if s > 100 then - return - end - l = tonumber(l) - if l > 100 then - return - end - local r, g, b = hsl_to_rgb(h / 360, s / 100, l / 100) - if r == nil or g == nil or b == nil then - return - end - local rgb_hex = string.format("%02x%02x%02x", r, g, b) - return match_end - 1, rgb_hex -end - -local CSS_HSLA_FN_MINIMUM_LENGTH = #"hsla(0,0%,0%,0)" - 1 ----Parse for hsl() css function and return rgb hex. ----@param line string: Line to parse ----@param i number: Index of line from where to start parsing ----@return number|nil: Index of line where the hsla function ended ----@return string|nil: rgb hex value -local function hsla_function_parser(line, i) - if #line < i + CSS_HSLA_FN_MINIMUM_LENGTH then - return - end - local h, s, l, a, match_end = line:sub(i):match "^hsla%(%s*(%d+)%s*,%s*(%d+)%%%s*,%s*(%d+)%%%s*,%s*([.%d]+)%s*%)()" - if not match_end then - h, s, l, a, match_end = line:sub(i):match "^hsla%(%s*(%d+)%s+(%d+)%%%s+(%d+)%%%s+([.%d]+)%s*%)()" - if not match_end then - return - end - end - a = tonumber(a) - if not a or a > 1 then - return - end - h = tonumber(h) - if h > 360 then - return - end - s = tonumber(s) - if s > 100 then - return - end - l = tonumber(l) - if l > 100 then - return - end - local r, g, b = hsl_to_rgb(h / 360, s / 100, l / 100) - if r == nil or g == nil or b == nil then - return - end - local rgb_hex = string.format("%02x%02x%02x", r * a, g * a, b * a) - return match_end - 1, rgb_hex -end - -local ARGB_MINIMUM_LENGTH = #"0xAARRGGBB" - 1 ----parse for 0xaarrggbb and return rgb hex. --- a format used in android apps ----@param line string: line to parse ----@param i number: index of line from where to start parsing ----@return number|nil: index of line where the hex value ended ----@return string|nil: rgb hex value -local function argb_hex_parser(line, i) - if #line < i + ARGB_MINIMUM_LENGTH then - return - end - - local j = i + 2 - - local n = j + 8 - local alpha - local v = 0 - while j <= min(n, #line) do - local b = line:byte(j) - if not byte_is_hex(b) then - break - end - if j - i <= 3 then - alpha = parse_hex(b) + lshift(alpha or 0, 4) - else - v = parse_hex(b) + lshift(v, 4) - end - j = j + 1 - end - if #line >= j and byte_is_alphanumeric(line:byte(j)) then - return - end - local length = j - i - if length ~= 10 then - return - end - alpha = tonumber(alpha) / 255 - local r = floor(band(rshift(v, 16), 0xFF) * alpha) - local g = floor(band(rshift(v, 8), 0xFF) * alpha) - local b = floor(band(v, 0xFF) * alpha) - local rgb_hex = string.format("%02x%02x%02x", r, g, b) - return length, rgb_hex -end - ---parse for #rrggbbaa and return rgb hex. -- a format used in android apps ---@param line string: line to parse @@ -350,7 +354,7 @@ end ---@param opts table: Containing minlen, maxlen, valid_lengths ---@return number|nil: index of line where the hex value ended ---@return string|nil: rgb hex value -local function rgba_hex_parser(line, i, opts) +function color.rgba_hex_parser(line, i, opts) local minlen, maxlen, valid_lengths = opts.minlen, opts.maxlen, opts.valid_lengths local j = i + 1 if #line < j + minlen - 1 then @@ -398,14 +402,4 @@ local function rgba_hex_parser(line, i, opts) return (valid_lengths[length - 1] and length), line:sub(i + 1, i + length - 1) end ---- @export -return { - color_is_bright = color_is_bright, - color_name_parser = color_name_parser, - rgba_hex_parser = rgba_hex_parser, - argb_hex_parser = argb_hex_parser, - rgb_function_parser = rgb_function_parser, - rgba_function_parser = rgba_function_parser, - hsl_function_parser = hsl_function_parser, - hsla_function_parser = hsla_function_parser, -} +return color diff --git a/lua/colorizer/matcher_utils.lua b/lua/colorizer/matcher.lua index 3f8047a..dba67c0 100644 --- a/lua/colorizer/matcher_utils.lua +++ b/lua/colorizer/matcher.lua @@ -1,41 +1,45 @@ ---Helper functions for colorizer to enable required parsers ---@module colorizer.matcher_utils +--@module colorizer.matcher local Trie = require "colorizer.trie" local min, max = math.min, math.max -local color_utils = require "colorizer.color_utils" -local color_name_parser = color_utils.color_name_parser -local rgba_hex_parser = color_utils.rgba_hex_parser +local color = require "colorizer.color" +local color_name_parser = color.name_parser +local rgba_hex_parser = color.rgba_hex_parser local sass = require "colorizer.sass" -local sass_name_parser = sass.sass_name_parser +local sass_name_parser = sass.name_parser -local parser = {} -parser["_0x"] = color_utils.argb_hex_parser -parser["_rgb"] = color_utils.rgb_function_parser -parser["_rgba"] = color_utils.rgba_function_parser -parser["_hsl"] = color_utils.hsl_function_parser -parser["_hsla"] = color_utils.hsla_function_parser -local b_hash, dollar_hash = ("#"):byte(), ("$"):byte() +local B_HASH, DOLLAR_HASH = ("#"):byte(), ("$"):byte() + +local parser = { + ["_0x"] = color.argb_hex_parser, + ["_rgb"] = color.rgb_function_parser, + ["_rgba"] = color.rgba_function_parser, + ["_hsl"] = color.hsl_function_parser, + ["_hsla"] = color.hsla_function_parser, +} + +local matcher = {} ---Form a trie stuct with the given prefixes ---@param matchers table: List of prefixes, {"rgb", "hsl"} ---@param matchers_trie table: Table containing information regarding non-trie based parsers ---@return function: function which will just parse the line for enabled parsers -local function compile_matcher(matchers, matchers_trie) +function matcher.compile(matchers, matchers_trie) local trie = Trie(matchers_trie) local function parse_fn(line, i, buf) -- prefix # if matchers.rgba_hex_parser then - if line:byte(i) == b_hash then + if line:byte(i) == B_HASH then return rgba_hex_parser(line, i, matchers.rgba_hex_parser) end end -- prefix $, SASS Colour names if matchers.sass_name_parser then - if line:byte(i) == dollar_hash then + if line:byte(i) == DOLLAR_HASH then return sass_name_parser(line, i, buf) end end @@ -63,7 +67,7 @@ local MATCHER_CACHE = {} --Do not try make the function again if it is present in the cache ---@param options table: options created in `colorizer.setup` ---@return function|boolean: function which will just parse the line for enabled parsers -local function make_matcher(options) +function matcher.make(options) local enable_names = options.css or options.names local enable_sass = options.sass and options.sass.enable local enable_tailwind = options.tailwind @@ -128,7 +132,6 @@ local function make_matcher(options) table.insert(matchers_prefix, "0x") end - -- do not mess with the sequence, hsla before hsl, etc if enable_rgb and enable_hsl then table.insert(matchers_prefix, "hsla") table.insert(matchers_prefix, "rgba") @@ -142,13 +145,10 @@ local function make_matcher(options) table.insert(matchers_prefix, "hsl") end - loop_parse_fn = compile_matcher(matchers, matchers_prefix) + loop_parse_fn = matcher.compile(matchers, matchers_prefix) MATCHER_CACHE[matcher_key] = loop_parse_fn return loop_parse_fn end ---- @export -return { - make_matcher = make_matcher, -} +return matcher diff --git a/lua/colorizer/sass.lua b/lua/colorizer/sass.lua index a003624..429ce2c 100644 --- a/lua/colorizer/sass.lua +++ b/lua/colorizer/sass.lua @@ -7,6 +7,8 @@ local utils = require "colorizer.utils" local get_last_modified = utils.get_last_modified local watch_file = utils.watch_file +local sass = {} + local DOLLAR_HASH = ("$"):byte() local AT_HASH = ("@"):byte() local COLON_HASH = (";"):byte() @@ -29,7 +31,7 @@ end --- Cleanup sass variables and watch handlers ---@param buf number -local function sass_cleanup(buf) +function sass.cleanup(buf) remove_unused_imports(buf, api.nvim_buf_get_name(buf)) SASS[buf] = nil end @@ -40,8 +42,8 @@ end ---@param i number: Index of line from where to start parsing ---@param buf number ---@return number|nil, string|nil -local function sass_name_parser(line, i, buf) - local variable_name = line:sub(i):match "^%$([%w_-]+)" +function sass.name_parser(line, i, buf) + local variable_name = line:match("^%$([%w_-]+)", i) if variable_name then local rgb_hex = SASS[buf].DEFINITIONS_ALL[variable_name] if rgb_hex then @@ -194,6 +196,7 @@ local function sass_parse_lines(buf, line_start, content, name) local lastm = get_last_modified(v) if lastm then + SASS[buf].IMPORTS[name] = SASS[buf].IMPORTS[name] or {} SASS[buf].IMPORTS[name][v] = lastm local cc, inde = {}, 0 for l in io.lines(v) do @@ -204,12 +207,7 @@ local function sass_parse_lines(buf, line_start, content, name) cc = nil end - require("colorizer.buffer_utils").rehighlight_buffer( - buf, - SASS[buf].OPTIONS, - SASS[buf].LOCAL_OPTIONS, - true - ) + require("colorizer.buffer").rehighlight(buf, SASS[buf].OPTIONS, SASS[buf].LOCAL_OPTIONS, true) end SASS[buf].WATCH_IMPORTS[name][v] = watch_file(v, watch_callback) end @@ -234,7 +232,7 @@ local function sass_parse_lines(buf, line_start, content, name) end end -- sass_parse_lines end ---- Parse the given lines for sass variabled and add to SASS[buf].DEFINITIONS_ALL. +--- Parse the given lines for sass variabled and add to `SASS[buf].DEFINITIONS_ALL`. -- which is then used in |sass_name_parser| -- If lines are not given, then fetch the lines with line_start and line_end ---@param buf number @@ -244,7 +242,7 @@ end -- sass_parse_lines end ---@param color_parser function|boolean ---@param options table: Buffer options ---@param options_local table|nil: Buffer local variables -local function sass_update_variables(buf, line_start, line_end, lines, color_parser, options, options_local) +function sass.update_variables(buf, line_start, line_end, lines, color_parser, options, options_local) lines = lines or vim.api.nvim_buf_get_lines(buf, line_start, line_end, false) if not SASS[buf] then @@ -308,9 +306,4 @@ local function sass_update_variables(buf, line_start, line_end, lines, color_par SASS[buf].DEFINITIONS_RECURSIVE_CURRENT_ABSOLUTE = nil end ---- @export -return { - sass_cleanup = sass_cleanup, - sass_name_parser = sass_name_parser, - sass_update_variables = sass_update_variables, -} +return sass diff --git a/lua/colorizer/tailwind.lua b/lua/colorizer/tailwind.lua index 88094f5..839cd0c 100644 --- a/lua/colorizer/tailwind.lua +++ b/lua/colorizer/tailwind.lua @@ -2,6 +2,8 @@ --@module colorizer.tailwind local api = vim.api +local tailwind = {} + -- use a different namespace for tailwind as will be cleared if kept in Default namespace local DEFAULT_NAMESPACE_TAILWIND = api.nvim_create_namespace "colorizer_tailwind" @@ -9,23 +11,27 @@ local TAILWIND = {} --- Cleanup tailwind variables and autocmd ---@param buf number -local function tailwind_cleanup(buf) +function tailwind.cleanup(buf) pcall(api.nvim_del_autocmd, TAILWIND[buf] and TAILWIND[buf].AU_ID[1]) pcall(api.nvim_del_autocmd, TAILWIND[buf] and TAILWIND[buf].AU_ID[2]) api.nvim_buf_clear_namespace(buf, DEFAULT_NAMESPACE_TAILWIND, 0, -1) TAILWIND[buf] = nil end -local function highlight_buffer_tailwind(buf, ns, options, add_highlight) +local function highlight_tailwind(buf, ns, options, add_highlight) -- it can take some time to actually fetch the results -- on top of that, tailwindcss is quite slow in neovim vim.defer_fn(function() + if not TAILWIND[buf] or not TAILWIND[buf].CLIENT or not TAILWIND[buf].CLIENT.request then + return + end + local opts = { textDocument = vim.lsp.util.make_text_document_params() } --@local ---@diagnostic disable-next-line: param-type-mismatch TAILWIND[buf].CLIENT.request("textDocument/documentColor", opts, function(err, results, _, _) if err == nil and results ~= nil then - local datas, line_start, line_end = {}, nil, nil + local data, line_start, line_end = {}, nil, nil for _, color in pairs(results) do local cur_line = color.range.start.line if line_start then @@ -50,22 +56,22 @@ local function highlight_buffer_tailwind(buf, ns, options, add_highlight) local first_col = color.range.start.character local end_col = color.range["end"].character - datas[cur_line] = datas[cur_line] or {} - table.insert(datas[cur_line], { rgb_hex = rgb_hex, range = { first_col, end_col } }) + data[cur_line] = data[cur_line] or {} + table.insert(data[cur_line], { rgb_hex = rgb_hex, range = { first_col, end_col } }) end - add_highlight(options, buf, ns, datas, line_start or 0, line_end and (line_end + 2) or -1) + add_highlight(buf, ns, line_start or 0, line_end and (line_end + 2) or -1, data, options) end end) end, 10) end --- highlight buffer using values returned by tailwindcss --- To see these table information, see |colorizer.buffer_utils| +-- To see these table information, see |colorizer.buffer| ---@param buf number ---@param options table ---@param options_local table ---@param add_highlight function -local function tailwind_setup_lsp_colors(buf, options, options_local, add_highlight) +function tailwind.setup_lsp_colors(buf, options, options_local, add_highlight) TAILWIND[buf] = TAILWIND[buf] or {} TAILWIND[buf].AU_ID = TAILWIND[buf].AU_ID or {} @@ -73,7 +79,7 @@ local function tailwind_setup_lsp_colors(buf, options, options_local, add_highli if vim.version().minor >= 8 then -- create the autocmds so tailwind colours only activate when tailwindcss lsp is active if not TAILWIND[buf].AU_CREATED then - tailwind_cleanup(buf) + tailwind.cleanup(buf) TAILWIND[buf].AU_ID[1] = api.nvim_create_autocmd("LspAttach", { group = options_local.__augroup_id, buffer = buf, @@ -84,7 +90,7 @@ local function tailwind_setup_lsp_colors(buf, options, options_local, add_highli -- wait 100 ms for the first request TAILWIND[buf].CLIENT = client vim.defer_fn(function() - highlight_buffer_tailwind(buf, DEFAULT_NAMESPACE_TAILWIND, options, add_highlight) + highlight_tailwind(buf, DEFAULT_NAMESPACE_TAILWIND, options, add_highlight) end, 100) end end @@ -95,7 +101,7 @@ local function tailwind_setup_lsp_colors(buf, options, options_local, add_highli group = options_local.__augroup_id, buffer = buf, callback = function() - tailwind_cleanup(buf) + tailwind.cleanup(buf) end, }) TAILWIND[buf].AU_CREATED = true @@ -117,16 +123,19 @@ local function tailwind_setup_lsp_colors(buf, options, options_local, add_highli for _, cl in pairs(tailwind_client) do if cl["name"] == "tailwindcss" then tailwind_client = cl - ok = false + ok = true break end end if - vim.tbl_isempty(tailwind_client or {}) - or not tailwind_client - or not tailwind_client.supports_method - or not tailwind_client.supports_method "textDocument/documentColor" + not ok + and ( + vim.tbl_isempty(tailwind_client or {}) + or not tailwind_client + or not tailwind_client.supports_method + or not tailwind_client.supports_method "textDocument/documentColor" + ) then return true end @@ -135,20 +144,16 @@ local function tailwind_setup_lsp_colors(buf, options, options_local, add_highli -- wait 500 ms for the first request vim.defer_fn(function() - highlight_buffer_tailwind(buf, DEFAULT_NAMESPACE_TAILWIND, options, add_highlight) - end, 500) + highlight_tailwind(buf, DEFAULT_NAMESPACE_TAILWIND, options, add_highlight) + end, 1000) return true end -- only try to do tailwindcss highlight if lsp is attached if TAILWIND[buf].CLIENT then - highlight_buffer_tailwind(buf, DEFAULT_NAMESPACE_TAILWIND, options, add_highlight) + highlight_tailwind(buf, DEFAULT_NAMESPACE_TAILWIND, options, add_highlight) end end ----@export -return { - tailwind_cleanup = tailwind_cleanup, - tailwind_setup_lsp_colors = tailwind_setup_lsp_colors, -} +return tailwind diff --git a/lua/colorizer/utils.lua b/lua/colorizer/utils.lua index 9dad9d8..c83c32d 100644 --- a/lua/colorizer/utils.lua +++ b/lua/colorizer/utils.lua @@ -5,6 +5,8 @@ local band, bor, rshift, lshift = bit.band, bit.bor, bit.rshift, bit.lshift local uv = vim.loop +local utils = {} + -- -- TODO use rgb as the return value from the matcher functions -- -- instead of the rgb_hex. Can be the highlight key as well -- -- when you shift it left 8 bits. Use the lower 8 bits for @@ -22,36 +24,38 @@ local CATEGORY_ALPHA = lshift(1, 1) local CATEGORY_HEX = lshift(1, 2) 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 >= 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 - 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 <= byte_values["f"] then +do + -- 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 >= 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(lowercase - byte_values["a"] + 10, 4)) + 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 <= byte_values["f"] then + v = bor(v, lshift(1, 2)) + v = bor(v, lshift(lowercase - byte_values["a"] + 10, 4)) + end + elseif extra_char[i] then + v = i end - elseif extra_char[i] then - v = i + BYTE_CATEGORY[i] = v end - BYTE_CATEGORY[i] = v end ---Obvious. ---@param byte number ---@return boolean -local function byte_is_alphanumeric(byte) +function utils.byte_is_alphanumeric(byte) local category = BYTE_CATEGORY[byte] return band(category, CATEGORY_ALPHANUM) ~= 0 end @@ -59,15 +63,33 @@ end ---Obvious. ---@param byte number ---@return boolean -local function byte_is_hex(byte) +function utils.byte_is_hex(byte) return band(BYTE_CATEGORY[byte], CATEGORY_HEX) ~= 0 end +--- Get last modified time of a file +---@param path string: file path +---@return number|nil: modified time +function utils.get_last_modified(path) + local fd = uv.fs_open(path, "r", 438) + if not fd then + return + end + + local stat = uv.fs_fstat(fd) + uv.fs_close(fd) + if stat then + return stat.mtime.nsec + else + return + end +end + ---Merge two tables. -- -- todo: Remove this and use `vim.tbl_deep_extend` ---@return table -local function merge(...) +function utils.merge(...) local res = {} for i = 1, select("#", ...) do local o = select(i, ...) @@ -84,7 +106,7 @@ end --- Obvious. ---@param byte number ---@return number -local function parse_hex(byte) +function utils.parse_hex(byte) return rshift(BYTE_CATEGORY[byte], 4) end @@ -92,7 +114,7 @@ local b_percent = string.byte "%" --- Obvious. ---@param v string ---@return number|nil -local function percent_or_hex(v) +function utils.percent_or_hex(v) if v:byte(-1) == b_percent then return tonumber(v:sub(1, -2)) / 100 * 255 end @@ -103,30 +125,12 @@ local function percent_or_hex(v) return x end ---- Get last modified time of a file ----@param path string: file path ----@return number|nil: modified time -local function get_last_modified(path) - local fd = uv.fs_open(path, "r", 438) - if not fd then - return - end - - local stat = uv.fs_fstat(fd) - uv.fs_close(fd) - if stat then - return stat.mtime.nsec - else - return - end -end - --- Watch a file for changes and execute callback ---@param path string: File path ---@param callback function: Callback to execute ---@param ... array: params for callback ---@return function|nil -local function watch_file(path, callback, ...) +function utils.watch_file(path, callback, ...) if not path or type(callback) ~= "function" then return end @@ -145,7 +149,7 @@ local function watch_file(path, callback, ...) callback(filename, unpack(args)) -- Debounce: stop/start. handle:stop() - if not err or not get_last_modified(filename) then + if not err or not utils.get_last_modified(filename) then start() end end @@ -165,13 +169,4 @@ local function watch_file(path, callback, ...) return handle end ---- @export -return { - byte_is_alphanumeric = byte_is_alphanumeric, - byte_is_hex = byte_is_hex, - merge = merge, - parse_hex = parse_hex, - percent_or_hex = percent_or_hex, - get_last_modified = get_last_modified, - watch_file = watch_file, -} +return utils diff --git a/test/sample.scss b/test/sample.scss index 9384fd5..d89903c 100644 --- a/test/sample.scss +++ b/test/sample.scss @@ -1,7 +1,17 @@ @import "scss/colors1"; +// simple variables +$test2: hotpink; +$test5: rgb(123, 255, 123); +$test7: rgba(255, 123, 123, 0.9); + +// reference $dd: $test7; + +// present in colors2.sass which is imported by colors1.sass $test0: $color1; +// present in colors1.sass +$test1: $color2; // recursive definitions $d: $test1; @@ -9,15 +19,10 @@ $dark: $d; $e: $dark; $f: $e; $g: $f; -$h: $g; -$test1: $color2; -$test2: hotpink; $test3: $color3; $test4: $color4; -$test5: rgb(123, 255, 123); $test6: $color6; // this should be absent and this comment shoudl be ignored $color6: black -$test7: rgba(255, 123, 123, 0.9); $test8: $color8; $test9: $color9; $test10: $color10; |