Skip to content

Commit

Permalink
Fix/buffer.clear_hl_cache calling unknown function (#97)
Browse files Browse the repository at this point in the history
* fix: calls clear_hl_cache from colorizer.lua

* fix: fixes parsing 0xAARRGGBB

* chore: adds luadoc for clear_highlight_cache api
  • Loading branch information
catgoose authored Nov 19, 2024
1 parent 22a3b4c commit f6bba01
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 78 deletions.
29 changes: 15 additions & 14 deletions doc/colorizer.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ Functions: ~
|setup| - Easy to use function if you want the full setup without fine
grained control.

|clear_highlight_cache| - Clears the highlight cache and reloads all
buffers.

Fields: ~
|default_namespace| - Default namespace used in `colorizer.buffer.highlight`
and `attach_to_buffer`.
Expand Down Expand Up @@ -202,6 +205,11 @@ setup({opts}) *colorizer.setup*



clear_highlight_cache() *colorizer.clear_highlight_cache*
Clears the highlight cache and reloads all buffers.



default_namespace *colorizer.default_namespace*
Default namespace used in `colorizer.buffer.highlight` and
`attach_to_buffer`.
Expand Down Expand Up @@ -234,8 +242,6 @@ Functions: ~
containing
rgb_hex and range per line

|clear_highlight_cache| - Clear the highlight cache and reload all buffers.

Tables: ~
|highlight_mode_names| - Highlight mode which will be use to render the
color
Expand Down Expand Up @@ -278,8 +284,8 @@ highlight({bufnr}, {ns_id}, {line_start}, {line_end}, {options},


Parameters: ~
{bufnr} - number: buffer number (0 for current)
{ns_id} - number: namespace id. default is "colorizer", created with
{bufnr} - number: Buffer number, 0 for current
{ns_id} - number: Namespace id, default is "colorizer" created with
vim.api.nvim_create_namespace
{line_start} - number: line_start should be 0-indexed
{line_end} - number: Last line to highlight
Expand All @@ -298,8 +304,8 @@ parse_lines({bufnr}, {lines}, {line_start}, {options})
rgb_hex and range per line

Parameters: ~
{bufnr} - number: buffer number (0 for current)
{lines} - table: table of lines to parse
{bufnr} - number: Buffer number (0 for current)
{lines} - table: Table of lines to parse
{line_start} - number: This is the buffer line number, from where to
start highlighting
{options} - table: Passed in `colorizer.setup`, Only uses
Expand All @@ -310,11 +316,6 @@ parse_lines({bufnr}, {lines}, {line_start}, {options})



clear_highlight_cache() *colorizer.buffer.clear_highlight_cache*
Clear the highlight cache and reload all buffers.



highlight_mode_names *colorizer.buffer.highlight_mode_names*
Highlight mode which will be use to render the color

Expand Down Expand Up @@ -868,7 +869,7 @@ name_parser({line}, {i}, {bufnr}) *colorizer.sass.name_parser*
Parameters: ~
{line} - string: Line to parse
{i} - number: Index of line from where to start parsing
{bufnr} - number
{bufnr} - number: Buffer number

returns:~
number or nil, string or nil
Expand All @@ -887,7 +888,7 @@ update_variables({bufnr}, {line_start}, {line_end}, {lines}, {color_parser},


Parameters: ~
{bufnr} - number: buffer number (0 for current)
{bufnr} - number: Buffer number
{line_start} - number
{line_end} - number
{lines} - table|nil
Expand Down Expand Up @@ -933,7 +934,7 @@ setup_lsp_colors({bufnr}, {options}, {options_local}, {add_highlight})
To see these table information, see |colorizer.buffer|

Parameters: ~
{bufnr} - number: buffer number (0 for current)
{bufnr} - number: Buffer number (0 for current)
{options} - table
{options_local} - table
{add_highlight} - function
Expand Down
26 changes: 4 additions & 22 deletions doc/modules/colorizer.buffer.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,6 @@ <h2><a href="#Functions">Functions</a></h2>
<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="#clear_highlight_cache">clear_highlight_cache ()</a></td>
<td class="summary">Clear the highlight cache and reload all buffers.</td>
</tr>
</table>
<h2><a href="#Tables">Tables</a></h2>
<table class="function_list">
Expand Down Expand Up @@ -175,10 +171,10 @@ <h3>Parameters:</h3>
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">bufnr</span>
number: buffer number (0 for current)
number: Buffer number, 0 for current
</li>
<li><span class="parameter">ns_id</span>
number: namespace id. default is "colorizer", created with vim.api.nvim_create_namespace
number: Namespace id, default is "colorizer" created with vim.api.nvim_create_namespace
</li>
<li><span class="parameter">line_start</span>
number: line_start should be 0-indexed
Expand Down Expand Up @@ -216,10 +212,10 @@ <h3>Returns:</h3>
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">bufnr</span>
number: buffer number (0 for current)
number: Buffer number (0 for current)
</li>
<li><span class="parameter">lines</span>
table: table of lines to parse
table: Table of lines to parse
</li>
<li><span class="parameter">line_start</span>
number: This is the buffer line number, from where to start highlighting
Expand All @@ -238,20 +234,6 @@ <h3>Returns:</h3>



</dd>
<dt>
<a name = "clear_highlight_cache"></a>
<strong>clear_highlight_cache ()</strong>
</dt>
<dd>
Clear the highlight cache and reload all buffers.







</dd>
</dl>
<h2 class="section-header "><a name="Tables"></a>Tables</h2>
Expand Down
18 changes: 18 additions & 0 deletions doc/modules/colorizer.html
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ <h2><a href="#Functions">Functions</a></h2>
<td class="name" nowrap><a href="#setup">setup (opts)</a></td>
<td class="summary">Easy to use function if you want the full setup without fine grained control.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#clear_highlight_cache">clear_highlight_cache ()</a></td>
<td class="summary">Clears the highlight cache and reloads all buffers.</td>
</tr>
</table>
<h2><a href="#Fields">Fields</a></h2>
<table class="function_list">
Expand Down Expand Up @@ -375,6 +379,20 @@ <h3>Usage:</h3>
<pre class="example"><span class="backtick">`require("colorizer").setup()`</span></pre>
</ul>

</dd>
<dt>
<a name = "clear_highlight_cache"></a>
<strong>clear_highlight_cache ()</strong>
</dt>
<dd>
Clears the highlight cache and reloads all buffers.







</dd>
</dl>
<h2 class="section-header "><a name="Fields"></a>Fields</h2>
Expand Down
4 changes: 2 additions & 2 deletions doc/modules/colorizer.sass.html
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ <h3>Parameters:</h3>
number: Index of line from where to start parsing
</li>
<li><span class="parameter">bufnr</span>
number
number: Buffer number
</li>
</ul>

Expand All @@ -157,7 +157,7 @@ <h3>Returns:</h3>
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">bufnr</span>
number: buffer number (0 for current)
number: Buffer number
</li>
<li><span class="parameter">line_start</span>
number
Expand Down
2 changes: 1 addition & 1 deletion doc/modules/colorizer.tailwind.html
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ <h3>Parameters:</h3>
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">bufnr</span>
number: buffer number (0 for current)
number: Buffer number (0 for current)
</li>
<li><span class="parameter">options</span>
table
Expand Down
10 changes: 7 additions & 3 deletions lua/colorizer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,7 @@ function M.setup(opts)

vim.api.nvim_create_autocmd("ColorScheme", {
group = colorizer_state.augroup,
callback = function()
buffer.clear_highlight_cache()
end,
callback = M.clear_highlight_cache,
})

parse_opts("filetype", s.filetypes)
Expand All @@ -488,4 +486,10 @@ function M.setup(opts)
require("colorizer.usercmds").make(s.user_commands)
end

--- Clears the highlight cache and reloads all buffers.
function M.clear_highlight_cache()
buffer.clear_hl_cache()
vim.schedule(M.reload_all_buffers)
end

return M
7 changes: 0 additions & 7 deletions lua/colorizer/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ local color = require("colorizer.color")
local plugin_name = "colorizer"
local sass = require("colorizer.sass")
local tailwind = require("colorizer.tailwind")
local utils = require("colorizer.utils")
local make_matcher = require("colorizer.matcher").make

local hl_state = {
Expand Down Expand Up @@ -204,10 +203,4 @@ function M.parse_lines(bufnr, lines, line_start, options)
return data
end

--- Clear the highlight cache and reload all buffers.
function M.clear_highlight_cache()
utils.clear_hl_cache()
vim.schedule(buffer.reload_all_buffers)
end

return M
60 changes: 31 additions & 29 deletions lua/colorizer/parser/argb_hex.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
local M = {}

local bit = require("bit")
local utils = require("colorizer.utils")
local floor, min = math.floor, math.min
local band, rshift, lshift = bit.band, bit.rshift, bit.lshift

local argb_minimum_length = #"0xAARRGGBB" - 1
local utils = require("colorizer.utils")

--- Parses a `0xAARRGGBB` formatted hexadecimal color and converts it to an RGB hex value.
-- This function reads a color from a line of text, expecting it in the `0xAARRGGBB` format (common in Android apps).
Expand All @@ -20,55 +19,58 @@ local argb_minimum_length = #"0xAARRGGBB" - 1
-- @return number|nil The end index of the parsed hex value within the line, or `nil` if parsing failed
-- @return string|nil The RGB hexadecimal color (e.g., "ff0000" for red), or `nil` if parsing failed
function M.argb_hex_parser(line, i)
if #line < i + argb_minimum_length then
-- Minimum length of a valid hex color (e.g., "0xRGB")
local minlen = #"0xRGB" - 1
-- Maximum length of a valid hex color (e.g., "0xAARRGGBB")
local maxlen = #"0xAARRGGBB" - 1

-- Ensure the line has enough characters to contain a valid hex color
if #line < i + minlen then
return
end

local j = i + 2
local n = j + 8
local alpha
local v = 0
local j = i + 2 -- Skip the "0x" prefix
local n = j + maxlen
local alpha, r, g, b
local v = 0 -- Holds the parsed value

-- Parse the hex characters starting from the given index
while j <= min(n, #line) do
local b = line:byte(j)
if not utils.byte_is_hex(b) then
local byte = line:byte(j)
-- Stop parsing if the character is not a valid hex digit
if not utils.byte_is_hex(byte) then
break
end
if j - i <= 3 then
alpha = utils.parse_hex(b) + lshift(alpha or 0, 4)
else
v = utils.parse_hex(b) + lshift(v, 4)
end
-- Shift the current value left by 4 bits and add the parsed hex digit
v = utils.parse_hex(byte) + lshift(v, 4)
j = j + 1
end

-- If the next character is alphanumeric, the value is invalid
if #line >= j and utils.byte_is_alphanumeric(line:byte(j)) then
return
end

local length = j - i
local length = j - i -- Calculate the length of the parsed hex value

-- Parse the color components based on the detected length
if length == 10 then -- 0xAARRGGBB
alpha = band(rshift(v, 24), 0xFF) / 255
r = floor(band(rshift(v, 16), 0xFF) * alpha)
g = floor(band(rshift(v, 8), 0xFF) * alpha)
b = floor(band(v, 0xFF) * alpha)
alpha = band(rshift(v, 24), 0xFF) / 255 -- Extract and normalize the alpha value
r = floor(band(rshift(v, 16), 0xFF) * alpha) -- Apply alpha to red
g = floor(band(rshift(v, 8), 0xFF) * alpha) -- Apply alpha to green
b = floor(band(v, 0xFF) * alpha) -- Apply alpha to blue
elseif length == 8 then -- 0xRRGGBB
r = band(rshift(v, 16), 0xFF)
g = band(rshift(v, 8), 0xFF)
b = band(v, 0xFF)
r = band(rshift(v, 16), 0xFF) -- Extract red
g = band(rshift(v, 8), 0xFF) -- Extract green
b = band(v, 0xFF) -- Extract blue
elseif length == 5 then -- 0xRGB
r = band(rshift(v, 8), 0xF) * 17
g = band(rshift(v, 4), 0xF) * 17
b = band(v, 0xF) * 17
r = band(rshift(v, 8), 0xF) * 17 -- Scale single hex digit to full byte
g = band(rshift(v, 4), 0xF) * 17 -- Scale single hex digit to full byte
b = band(v, 0xF) * 17 -- Scale single hex digit to full byte
else
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
Expand Down

0 comments on commit f6bba01

Please sign in to comment.