Skip to content

Commit 8c7f243

Browse files
authored
Feat: support lazyloading (#129)
* feat: add config option to support lazyloading * doc: updates ldoc documentation
1 parent dd643c1 commit 8c7f243

File tree

5 files changed

+49
-14
lines changed

5 files changed

+49
-14
lines changed

README.md

+20-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- [Why another highlighter?](#why-another-highlighter)
1414
- [Customization](#customization)
1515
- [Updating color even when buffer is not focused](#updating-color-even-when-buffer-is-not-focused)
16+
- [Lazyload Colorizer with Lazy.nvim](#lazyload-colorizer-with-lazynvim)
1617
- [Tailwind](#tailwind)
1718
- [Testing](#testing)
1819
- [Extras](#extras)
@@ -113,11 +114,13 @@ library to do custom highlighting themselves.
113114
114115
```lua
115116
require("colorizer").setup({
117+
-- Filetype options. Accepts table like `user_default_options`
116118
filetypes = { "*" },
117-
-- all the sub-options of filetypes apply to buftypes
119+
-- Buftype options. Accepts table like `user_default_options`
118120
buftypes = {},
119121
-- Boolean | List of usercommands to enable. See User commands section.
120122
user_commands = true, -- Enable all or some usercommands
123+
lazy_load = false, -- Lazily schedule buffer highlighting setup function
121124
user_default_options = {
122125
names = true, -- "Name" codes like Blue or red. Added from `vim.api.nvim_get_color_map()`
123126
names_opts = { -- options for mutating/filtering names.
@@ -146,9 +149,10 @@ library to do custom highlighting themselves.
146149
-- Tailwind colors. boolean|'normal'|'lsp'|'both'. True is same as normal
147150
tailwind = false, -- Enable tailwind colors
148151
tailwind_opts = { -- Options for highlighting tailwind names
149-
update_names = false, -- When using tailwind = 'both', update tailwind names from LSP results. See tailwind section
152+
update_names = false, -- When using tailwind = 'both', update tailwind
153+
-- names from LSP results. See tailwind section
150154
},
151-
-- parsers can contain values used in |user_default_options|
155+
-- parsers can contain values used in `user_default_options`
152156
sass = { enable = false, parsers = { "css" } }, -- Enable sass colors
153157
-- Virtualtext character to use
154158
virtualtext = "",
@@ -329,6 +333,19 @@ For lower level interface, see
329333
[LuaDocs for API details](https://catgoose.github.io/nvim-colorizer.lua/modules/colorizer.html)
330334
or use `:h colorizer` once installed.
331335

336+
### Lazyload Colorizer with Lazy.nvim
337+
338+
```lua
339+
return {
340+
"catgoose/nvim-colorizer.lua",
341+
event = "VeryLazy",
342+
opts = {
343+
lazy_load = true,
344+
-- other setup options
345+
},
346+
}
347+
```
348+
332349
### Tailwind
333350

334351
Tailwind colors can either be parsed from the Tailwind colors file (found in
@@ -357,7 +374,6 @@ This can be useful if you are highlighting `cmp_menu` filetype.
357374
```
358375

359376
![tailwind.update_names](https://github.com/catgoose/screenshots/blob/51466fa599efe6d9821715616106c1712aad00c3/nvim-colorizer.lua/tailwind_update_names.png)
360-
361377
To improve highlighting performance with the slow Tailwind LSP, results from LSP
362378
are cached and returned on `WinScrolled` event.
363379

doc/colorizer.txt

+9-5
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@ options *colorizer.config.options*
575575
{filetypes} -
576576
{buftypes} -
577577
{user_commands} -
578+
{lazy_load} -
578579
{user_default_options} -
579580
{exclusions} -
580581
{all} -
@@ -630,15 +631,18 @@ opts *colorizer.config.opts*
630631
for virtual text.
631632
- `always_update` (boolean): If true, updates color values even if the buffer
632633
is not focused.
633-
{buftypes} - table|nil Optional. A list of buffer types where colorizer
634-
should be enabled. Defaults to all buffer types if not provided.
635-
{user_commands} - boolean|table If true, enables all user commands for
636-
colorizer. If `false`, disables user commands. Alternatively, provide a
637-
table of specific commands to enable:
634+
{buftypes} - (table|nil): Optional. A list of buffer types where
635+
colorizer should be enabled. Defaults to all buffer types if not
636+
provided.
637+
{user_commands} - (boolean|table): If true, enables all user commands
638+
for colorizer. If `false`, disables user commands. Alternatively,
639+
provide a table of specific commands to enable:
638640
- `"ColorizerAttachToBuffer"`
639641
- `"ColorizerDetachFromBuffer"`
640642
- `"ColorizerReloadAllBuffers"`
641643
- `"ColorizerToggle"`
644+
{lazy_load} - (boolean): If true, lazily schedule buffer highlighting
645+
setup function
642646

643647

644648

doc/modules/colorizer.config.html

+8-2
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,9 @@ <h3>Fields:</h3>
374374
</li>
375375
<li><span class="parameter">user_commands</span>
376376

377+
</li>
378+
<li><span class="parameter">lazy_load</span>
379+
377380
</li>
378381
<li><span class="parameter">user_default_options</span>
379382

@@ -435,15 +438,18 @@ <h3>Fields:</h3>
435438
- `always_update` (boolean): If true, updates color values even if the buffer is not focused.
436439
</li>
437440
<li><span class="parameter">buftypes</span>
438-
table|nil Optional. A list of buffer types where colorizer should be enabled. Defaults to all buffer types if not provided.
441+
(table|nil): Optional. A list of buffer types where colorizer should be enabled. Defaults to all buffer types if not provided.
439442
</li>
440443
<li><span class="parameter">user_commands</span>
441-
boolean|table If true, enables all user commands for colorizer. If `false`, disables user commands. Alternatively, provide a table of specific commands to enable:
444+
(boolean|table): If true, enables all user commands for colorizer. If `false`, disables user commands. Alternatively, provide a table of specific commands to enable:
442445
- `"ColorizerAttachToBuffer"`
443446
- `"ColorizerDetachFromBuffer"`
444447
- `"ColorizerReloadAllBuffers"`
445448
- `"ColorizerToggle"`
446449
</li>
450+
<li><span class="parameter">lazy_load</span>
451+
(boolean): If true, lazily schedule buffer highlighting setup function
452+
</li>
447453
</ul>
448454

449455

lua/colorizer.lua

+7-1
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,13 @@ function M.setup(opts)
538538
group = colorizer_state.augroup,
539539
pattern = bo_type == "filetype" and (s.all[bo_type] and "*" or list) or nil,
540540
callback = function()
541-
setup(bo_type)
541+
if s.lazy_load then
542+
vim.schedule(function()
543+
setup(bo_type)
544+
end)
545+
else
546+
setup(bo_type)
547+
end
542548
end,
543549
})
544550
end

lua/colorizer/config.lua

+5-2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ local plugin_user_default_options = {
7373
--@field filetypes
7474
--@field buftypes
7575
--@field user_commands
76+
--@field lazy_load
7677
--@field user_default_options
7778
--@field exclusions
7879
--@field all
@@ -83,6 +84,7 @@ local function init_options()
8384
filetypes = { "*" },
8485
buftypes = {},
8586
user_commands = true,
87+
lazy_load = false,
8688
user_default_options = plugin_user_default_options,
8789
-- shortcuts for filetype, buftype inclusion, exclusion settings
8890
exclusions = { buftype = {}, filetype = {} },
@@ -202,12 +204,13 @@ end
202204
-- - `virtualtext_inline` (boolean|'before'|'after'): Shows the virtual text inline with the color. True defaults to 'before'. False or nil disables.
203205
-- - `virtualtext_mode` ('background'|'foreground'): Determines the display mode for virtual text.
204206
-- - `always_update` (boolean): If true, updates color values even if the buffer is not focused.
205-
-- @field buftypes table|nil Optional. A list of buffer types where colorizer should be enabled. Defaults to all buffer types if not provided.
206-
-- @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:
207+
-- @field buftypes (table|nil): Optional. A list of buffer types where colorizer should be enabled. Defaults to all buffer types if not provided.
208+
-- @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:
207209
-- - `"ColorizerAttachToBuffer"`
208210
-- - `"ColorizerDetachFromBuffer"`
209211
-- - `"ColorizerReloadAllBuffers"`
210212
-- - `"ColorizerToggle"`
213+
-- @field lazy_load (boolean): If true, lazily schedule buffer highlighting setup function
211214

212215
--- Initializes colorizer with user-provided options.
213216
-- Merges default settings with any user-specified options, setting up `filetypes`,

0 commit comments

Comments
 (0)