fix: Make always update buffer configurable | #52

disabled by default
This commit is contained in:
akianonymus 2023-02-26 23:47:38 +05:30
parent ebb03999a8
commit 33d4bb4a64
6 changed files with 124 additions and 129 deletions

View file

@ -100,6 +100,9 @@ library to do custom highlighting themselves.
-- parsers can contain values used in |user_default_options|
sass = { enable = false, parsers = { "css" }, }, -- Enable sass colors
virtualtext = "■",
-- update color values even if buffer is not focused
-- example use: cmp_menu, cmp_docs
always_update = false
},
-- all the sub-options of filetypes apply to buftypes
buftypes = {},
@ -160,6 +163,13 @@ require 'colorizer'.setup {
},
}
-- Alwyas update the color values in cmp_docs even if it not focused
require 'colorizer'.setup {
filetypes = {
'*'; -- Highlight all files, but customize some others.
cmp_docs = {always_update = true}
},
}
```
In `user_default_options`, there are 2 types of options
@ -188,6 +198,22 @@ e.g: Here `names`, `RGB`, `RRGGBB`, `RRGGBBAA` is enabled but not `rgb_fn` and `
require 'colorizer'.setup { user_default_options = { css_fn = false, css = true } }
```
### Updating color even when buffer is not focused
Like in floating windows, popup menu, etc
use `always_update` flag. Use with caution, as this will update for any change in that buffer, whether focused or not.
```lua
-- Alwyas update the color values in cmp_docs even if it not focused
require 'colorizer'.setup {
filetypes = {
'*'; -- Highlight all files, but customize some others.
cmp_docs = {always_update = true}
},
}
```
All the above examples can also be apply to buftypes. Also no buftypes trigger colorizer by default
Buftype value is fetched by `vim.bo.buftype`

View file

@ -245,6 +245,8 @@ user_default_options *colorizer.user_default_options*
-- parsers can contain values used in |user_default_options|
sass = { enable = false, parsers = { css }, }, -- Enable sass colors
virtualtext = "■",
-- update color values even if buffer is not focused
always_update = false
}
<
@ -263,6 +265,7 @@ user_default_options *colorizer.user_default_options*
{tailwind} - boolean|string
{sass} - table
{virtualtext} - string
{always_update} - boolean
@ -430,9 +433,8 @@ Functions: ~
|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.
|rgb_function_parser| - Parse for rgb() rgba() css function and return rgb
hex.
|rgba_hex_parser| - parse for #rrggbbaa and return rgb hex.
@ -529,33 +531,20 @@ name_parser({line}, {i}, {opts}) *colorizer.color.name_parser*
rgb_function_parser({line}, {i}) *colorizer.color.rgb_function_parser*
Parse for rgb() css function and return rgb hex.
rgb_function_parser({line}, {i}, {opts}) *colorizer.color.rgb_function_parser*
Parse for rgb() rgba() 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
string or nil: rgb hex value
rgba_function_parser({line}, {i}) *colorizer.color.rgba_function_parser*
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.
For more info:
https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/rgb
Parameters: ~
{line} - string: Line to parse
{i} - number: Index of line from where to start parsing
{opts} - table: Values passed from matchers like prefix
returns:~
number or nil: Index of line where the rgba function ended
number or nil: Index of line where the rgb/rgba function ended
string or nil: rgb hex value
@ -747,14 +736,14 @@ Functions: ~
|byte_is_valid_colorchar| - Valid colorchars are alphanumeric and - (
tailwind colors )
|count| - Count the number of character in a string
|get_last_modified| - Get last modified time of a file
|merge| - Merge two tables.
|parse_hex| - Obvious.
|percent_or_hex| - Obvious.
|watch_file| - Watch a file for changes and execute callback
@ -791,6 +780,18 @@ byte_is_valid_colorchar({byte}) *colorizer.utils.byte_is_valid_colorchar*
count({str}, {pattern}) *colorizer.utils.count*
Count the number of character in a string
Parameters: ~
{str} - string
{pattern} - string
returns:~
number
get_last_modified({path}) *colorizer.utils.get_last_modified*
Get last modified time of a file
@ -828,24 +829,13 @@ parse_hex({byte}) *colorizer.utils.parse_hex*
percent_or_hex({v}) *colorizer.utils.percent_or_hex*
Obvious.
Parameters: ~
{v} - string
returns:~
number or nil
watch_file({path}, {callback}, {...}) *colorizer.utils.watch_file*
Watch a file for changes and execute callback
Parameters: ~
{path} - string: File path
{callback} - function: Callback to execute
{...} - array: params for callback
{...} - table: params for callback
returns:~
function or nil

View file

@ -86,12 +86,8 @@
<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>
<td class="name" nowrap><a href="#rgb_function_parser">rgb_function_parser (line, i, opts)</a></td>
<td class="summary">Parse for rgb() 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>
@ -292,10 +288,11 @@
</dd>
<dt>
<a name = "rgb_function_parser"></a>
<strong>rgb_function_parser (line, i)</strong>
<strong>rgb_function_parser (line, i, opts)</strong>
</dt>
<dd>
Parse for rgb() css function and return rgb hex.
Parse for rgb() rgba() css function and return rgb hex.
For more info: https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/rgb
<h3>Parameters:</h3>
@ -306,45 +303,15 @@
<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 rgb function ended</li>
<li>
string|nil: rgb hex value</li>
</ol>
</dd>
<dt>
<a name = "rgba_function_parser"></a>
<strong>rgba_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.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">line</span>
string: Line to parse
</li>
<li><span class="parameter">i</span>
number: Index of line from where to start parsing
<li><span class="parameter">opts</span>
table: Values passed from matchers like prefix
</li>
</ul>
<h3>Returns:</h3>
<ol>
<li>
number|nil: Index of line where the rgba function ended</li>
number|nil: Index of line where the rgb/rgba function ended</li>
<li>
string|nil: rgb hex value</li>
</ol>

View file

@ -437,6 +437,8 @@ In <a href="../modules/colorizer.html#user_default_options">user_default_options
-- parsers can contain values used in |user_default_options|
sass = { enable = false, parsers = { css }, }, -- Enable sass colors
virtualtext = "■",
-- update color values even if buffer is not focused
always_update = false
}
</pre>
@ -484,6 +486,9 @@ In <a href="../modules/colorizer.html#user_default_options">user_default_options
<li><span class="parameter">virtualtext</span>
string
</li>
<li><span class="parameter">always_update</span>
boolean
</li>
</ul>

View file

@ -74,6 +74,10 @@
<td class="summary">Valid colorchars are alphanumeric and - ( tailwind colors )</td>
</tr>
<tr>
<td class="name" nowrap><a href="#count">count (str, pattern)</a></td>
<td class="summary">Count the number of character in a string</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>
@ -86,10 +90,6 @@
<td class="summary">Obvious.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#percent_or_hex">percent_or_hex (v)</a></td>
<td class="summary">Obvious.</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>
@ -176,6 +176,34 @@
</dd>
<dt>
<a name = "count"></a>
<strong>count (str, pattern)</strong>
</dt>
<dd>
Count the number of character in a string
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">str</span>
string
</li>
<li><span class="parameter">pattern</span>
string
</li>
</ul>
<h3>Returns:</h3>
<ol>
number
</ol>
</dd>
<dt>
<a name = "get_last_modified"></a>
@ -255,31 +283,6 @@
</dd>
<dt>
<a name = "percent_or_hex"></a>
<strong>percent_or_hex (v)</strong>
</dt>
<dd>
Obvious.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">v</span>
string
</li>
</ul>
<h3>Returns:</h3>
<ol>
number|nil
</ol>
</dd>
<dt>
<a name = "watch_file"></a>
@ -298,7 +301,7 @@
function: Callback to execute
</li>
<li><span class="parameter">...</span>
array: params for callback
table: params for callback
</li>
</ul>

View file

@ -139,6 +139,8 @@ local CURRENT_BUF = 0
-- -- parsers can contain values used in |user_default_options|
-- sass = { enable = false, parsers = { css }, }, -- Enable sass colors
-- virtualtext = "■",
-- -- update color values even if buffer is not focused
-- always_update = false
-- }
--</pre>
---@table user_default_options
@ -155,6 +157,7 @@ local CURRENT_BUF = 0
--@field tailwind boolean|string
--@field sass table
--@field virtualtext string
--@field always_update boolean
local USER_DEFAULT_OPTIONS = {
RGB = true,
RRGGBB = true,
@ -169,6 +172,7 @@ local USER_DEFAULT_OPTIONS = {
tailwind = false,
sass = { enable = false, parsers = { css = true } },
virtualtext = "",
always_update = false,
}
local OPTIONS = { buf = {}, file = {} }
@ -200,8 +204,6 @@ local function parse_buffer_options(options)
["css"] = { "names", "RGB", "RRGGBB", "RRGGBBAA", "hsl_fn", "rgb_fn" },
["css_fn"] = { "hsl_fn", "rgb_fn" },
}
local css_includes = { "names", "RGB", "RRGGBB", "RRGGBBAA", "hsl_fn", "rgb_fn" }
local css_fn_includes = { "hsl_fn", "rgb_fn" }
local default_opts = USER_DEFAULT_OPTIONS
local function handle_alias(name, opts, d_opts)
@ -364,28 +366,30 @@ function colorizer.attach_to_buffer(buf, options, typ)
CURRENT_BUF = buf
end
-- attach using lua api so buffer gets updated even when not the current buffer
-- completely moving to buf_attach is not possible because it doesn't handle all the text change events
vim.api.nvim_buf_attach(buf, false, {
on_lines = function(_, buffer)
-- only reload if the buffer is not the current one
if not (CURRENT_BUF == buffer) then
-- only reload if it was not disabled using detach_from_buffer
if BUFFER_OPTIONS[buf] then
rehighlight_buffer(buf, options, BUFFER_LOCAL[buf])
if options.always_update then
-- attach using lua api so buffer gets updated even when not the current buffer
-- completely moving to buf_attach is not possible because it doesn't handle all the text change events
vim.api.nvim_buf_attach(buf, false, {
on_lines = function(_, buffer)
-- only reload if the buffer is not the current one
if not (CURRENT_BUF == buffer) then
-- only reload if it was not disabled using detach_from_buffer
if BUFFER_OPTIONS[buf] then
rehighlight_buffer(buf, options, BUFFER_LOCAL[buf])
end
end
end
end,
on_reload = function(_, buffer)
-- only reload if the buffer is not the current one
if not (CURRENT_BUF == buffer) then
-- only reload if it was not disabled using detach_from_buffer
if BUFFER_OPTIONS[buf] then
rehighlight_buffer(buf, options, BUFFER_LOCAL[buf])
end,
on_reload = function(_, buffer)
-- only reload if the buffer is not the current one
if not (CURRENT_BUF == buffer) then
-- only reload if it was not disabled using detach_from_buffer
if BUFFER_OPTIONS[buf] then
rehighlight_buffer(buf, options, BUFFER_LOCAL[buf])
end
end
end
end,
})
end,
})
end
autocmds[#autocmds + 1] = autocmd(text_changed_au, {
group = au_group_id,