Skip to content

Commit 39142aa

Browse files
authored
Feat/add hooks: disable_line_highlight (#138)
* feat: adds user_default_option hook table with `disable_line_highlight` function
1 parent ed12b53 commit 39142aa

File tree

6 files changed

+75
-6
lines changed

6 files changed

+75
-6
lines changed

README.md

+25-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
- [Lua API](#lua-api)
1313
- [Why another highlighter?](#why-another-highlighter)
1414
- [Customization](#customization)
15+
- [Hooks](#hooks)
16+
- [Setup Examples](#setup-examples)
1517
- [Updating color even when buffer is not focused](#updating-color-even-when-buffer-is-not-focused)
1618
- [Lazyload Colorizer with Lazy.nvim](#lazyload-colorizer-with-lazynvim)
1719
- [Tailwind](#tailwind)
@@ -164,11 +166,33 @@ library to do custom highlighting themselves.
164166
-- update color values even if buffer is not focused
165167
-- example use: cmp_menu, cmp_docs
166168
always_update = false,
169+
-- hooks to invert control of colorizer
170+
hooks = {
171+
-- called before line parsing. Set to function that returns a boolean and accepts the following parameters. See hooks section.
172+
do_lines_parse = false,
173+
},
167174
},
168175
})
169176
```
170177

171-
Setup examples:
178+
### Hooks
179+
180+
Hooks into colorizer can be defined to customize colorization behavior.
181+
182+
`do_lines_parse`: Expects a function that returns a boolean. The function is called before line parsing with the following function signature:
183+
184+
```lua
185+
---@param line string: Line's contents
186+
---@param bufnr number: Buffer number
187+
---@line_num number: Line number (0-indexed). Ad 1 to get the line number in buffer
188+
---@return boolean: Return true if current line should be parsed for highlighting.
189+
function(line, bufnr, line_num)
190+
-- Treesitter could also be used, but be warned it will be quite laggy unless you are caching results somehow
191+
return string.sub(line, 1, 2) ~= "--"
192+
end
193+
```
194+
195+
### Setup Examples
172196

173197
```lua
174198
-- Attaches to every FileType with default options

doc/colorizer.txt

+10
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,9 @@ user_default_options *colorizer.config.user_default_options*
548548
display.
549549
{always_update} - boolean: Always update color values, even if buffer
550550
is not focused.
551+
hooks - table: Table of hook functions
552+
{disable_line_highlight} - function: Returns boolean which controls if
553+
line should be parsed for highlights
551554

552555

553556

@@ -615,6 +618,13 @@ ud_opts *colorizer.config.ud_opts*
615618
for virtual text.
616619
- `always_update` (boolean): If true, updates color values even if the buffer
617620
is not focused.<
621+
- `hooks` (table): Table of hook functions
622+
- `disable_line_highlight` (function): Returns a boolean that controls if
623+
the line should be parsed for highlights. Called with 3 parameters:
624+
- `line` (string): The line's contents.
625+
- `bufnr` (number): The buffer number.
626+
- `line_num` (number): The line number (0-indexed). Add 1 to get the line
627+
number in the buffer.
618628
{buftypes} - (table|nil): Optional. A list of buffer types where
619629
colorizer should be enabled. Defaults to all buffer types if not
620630
provided.

doc/modules/colorizer.config.html

+11
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,12 @@ <h3>Fields:</h3>
329329
<li><span class="parameter">always_update</span>
330330
boolean: Always update color values, even if buffer is not focused.
331331
</li>
332+
<li><span class="parameter">hooks</span> table: Table of hook functions
333+
<ul>
334+
<li><span class="parameter">disable_line_highlight</span>
335+
function: Returns boolean which controls if line should be parsed for highlights
336+
</li>
337+
</li></ul>
332338
</ul>
333339

334340

@@ -417,6 +423,11 @@ <h3>Fields:</h3>
417423
- `virtualtext_inline` (boolean|'before'|'after'): Shows the virtual text inline with the color. True defaults to 'before'.
418424
- `virtualtext_mode` ('background'|'foreground'): Determines the display mode for virtual text.
419425
- `always_update` (boolean): If true, updates color values even if the buffer is not focused.</pre>
426+
- `hooks` (table): Table of hook functions
427+
- `disable_line_highlight` (function): Returns a boolean that controls if the line should be parsed for highlights. Called with 3 parameters:
428+
- `line` (string): The line's contents.
429+
- `bufnr` (number): The buffer number.
430+
- `line_num` (number): The line number (0-indexed). Add 1 to get the line number in the buffer.
420431
</li>
421432
<li><span class="parameter">buftypes</span>
422433
(table|nil): Optional. A list of buffer types where colorizer should be enabled. Defaults to all buffer types if not provided.

lua/colorizer/buffer.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ function M.parse_lines(bufnr, lines, line_start, ud_opts)
292292
line_nr = line_nr - 1 + line_start
293293
local i = 1
294294
while i < #line do
295-
local length, rgb_hex = loop_parse_fn(line, i, bufnr)
295+
local length, rgb_hex = loop_parse_fn(line, i, bufnr, line_nr)
296296
if length and not rgb_hex then
297297
utils.log_message(
298298
string.format(

lua/colorizer/config.lua

+15
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ local plugin_user_default_options = {
3333
virtualtext_inline = false,
3434
virtualtext_mode = "foreground",
3535
always_update = false,
36+
hooks = {
37+
disable_line_highlight = false,
38+
},
3639
}
3740

3841
--[[-- Default user options for colorizer.
@@ -71,6 +74,8 @@ If both `css` and `css_fn` are true, `css_fn` has more priority over `css`.
7174
-- @field virtualtext_inline boolean|'before'|'after': Shows virtual text inline with color.
7275
-- @field virtualtext_mode 'background'|'foreground': Mode for virtual text display.
7376
-- @field always_update boolean: Always update color values, even if buffer is not focused.
77+
-- @field hooks table: Table of hook functions
78+
-- @field hooks.disable_line_highlight function: Returns boolean which controls if line should be parsed for highlights
7479

7580
--- Options for colorizer that were passed in to setup function
7681
--@field filetypes
@@ -165,6 +170,11 @@ local function validate_options(ud_opts)
165170
}
166171
ud_opts.names_custom = false
167172
end
173+
if ud_opts.hooks then
174+
if type(ud_opts.hooks.disable_line_highlight) ~= "function" then
175+
ud_opts.hooks.disable_line_highlight = false
176+
end
177+
end
168178
end
169179

170180
--- Set options for a specific buffer or file type.
@@ -242,6 +252,11 @@ end
242252
-- - `virtualtext_inline` (boolean|'before'|'after'): Shows the virtual text inline with the color. True defaults to 'before'.
243253
-- - `virtualtext_mode` ('background'|'foreground'): Determines the display mode for virtual text.
244254
-- - `always_update` (boolean): If true, updates color values even if the buffer is not focused.</pre>
255+
-- - `hooks` (table): Table of hook functions
256+
-- - `disable_line_highlight` (function): Returns a boolean that controls if the line should be parsed for highlights. Called with 3 parameters:
257+
-- - `line` (string): The line's contents.
258+
-- - `bufnr` (number): The buffer number.
259+
-- - `line_num` (number): The line number (0-indexed). Add 1 to get the line number in the buffer.
245260
-- @field buftypes (table|nil): Optional. A list of buffer types where colorizer should be enabled. Defaults to all buffer types if not provided.
246261
-- @field user_commands (boolean|table): If true, enables all user commands for colorizer. If `false`, disables user commands. Alternatively, provide a table of specific commands to enable:
247262
-- - `"ColorizerAttachToBuffer"`

lua/colorizer/matcher.lua

+13-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ It uses a trie-based structure to optimize prefix-based parsing.
77
local M = {}
88

99
local Trie = require("colorizer.trie")
10-
local utils = require("colorizer.utils")
1110
local min, max = math.min, math.max
1211

1312
local parsers = {
@@ -31,11 +30,21 @@ parsers.prefix = {
3130
---Form a trie stuct with the given prefixes
3231
---@param matchers table: List of prefixes, {"rgb", "hsl"}
3332
---@param matchers_trie table: Table containing information regarding non-trie based parsers
33+
---@param hooks? table: Table of hook functions
34+
-- hooks.disable_line_highlight: function to be called after parsing the line
3435
---@return function: function which will just parse the line for enabled parsers
35-
local function compile(matchers, matchers_trie)
36+
local function compile(matchers, matchers_trie, hooks)
3637
local trie = Trie(matchers_trie)
3738

38-
local function parse_fn(line, i, bufnr)
39+
local function parse_fn(line, i, bufnr, line_nr)
40+
if
41+
hooks
42+
and hooks.disable_line_highlight
43+
and hooks.disable_line_highlight(line, line_nr, bufnr)
44+
then
45+
return
46+
end
47+
3948
-- prefix #
4049
if matchers.rgba_hex_parser then
4150
if line:byte(i) == ("#"):byte() then
@@ -199,7 +208,7 @@ function M.make(ud_opts)
199208
matchers[value] = { prefix = value }
200209
end
201210

202-
loop_parse_fn = compile(matchers, matchers_prefix)
211+
loop_parse_fn = compile(matchers, matchers_prefix, ud_opts.hooks)
203212
matcher_cache[matcher_mask] = loop_parse_fn
204213

205214
return loop_parse_fn

0 commit comments

Comments
 (0)