Skip to content

Commit

Permalink
Feat: support lazyloading (#129)
Browse files Browse the repository at this point in the history
* feat: add config option to support lazyloading

* doc: updates ldoc documentation
  • Loading branch information
catgoose authored Jan 10, 2025
1 parent dd643c1 commit 8c7f243
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 14 deletions.
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- [Why another highlighter?](#why-another-highlighter)
- [Customization](#customization)
- [Updating color even when buffer is not focused](#updating-color-even-when-buffer-is-not-focused)
- [Lazyload Colorizer with Lazy.nvim](#lazyload-colorizer-with-lazynvim)
- [Tailwind](#tailwind)
- [Testing](#testing)
- [Extras](#extras)
Expand Down Expand Up @@ -113,11 +114,13 @@ library to do custom highlighting themselves.
```lua
require("colorizer").setup({
-- Filetype options. Accepts table like `user_default_options`
filetypes = { "*" },
-- all the sub-options of filetypes apply to buftypes
-- Buftype options. Accepts table like `user_default_options`
buftypes = {},
-- Boolean | List of usercommands to enable. See User commands section.
user_commands = true, -- Enable all or some usercommands
lazy_load = false, -- Lazily schedule buffer highlighting setup function
user_default_options = {
names = true, -- "Name" codes like Blue or red. Added from `vim.api.nvim_get_color_map()`
names_opts = { -- options for mutating/filtering names.
Expand Down Expand Up @@ -146,9 +149,10 @@ library to do custom highlighting themselves.
-- Tailwind colors. boolean|'normal'|'lsp'|'both'. True is same as normal
tailwind = false, -- Enable tailwind colors
tailwind_opts = { -- Options for highlighting tailwind names
update_names = false, -- When using tailwind = 'both', update tailwind names from LSP results. See tailwind section
update_names = false, -- When using tailwind = 'both', update tailwind
-- names from LSP results. See tailwind section
},
-- parsers can contain values used in |user_default_options|
-- parsers can contain values used in `user_default_options`
sass = { enable = false, parsers = { "css" } }, -- Enable sass colors
-- Virtualtext character to use
virtualtext = "",
Expand Down Expand Up @@ -329,6 +333,19 @@ For lower level interface, see
[LuaDocs for API details](https://catgoose.github.io/nvim-colorizer.lua/modules/colorizer.html)
or use `:h colorizer` once installed.

### Lazyload Colorizer with Lazy.nvim

```lua
return {
"catgoose/nvim-colorizer.lua",
event = "VeryLazy",
opts = {
lazy_load = true,
-- other setup options
},
}
```

### Tailwind

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

![tailwind.update_names](https://github.com/catgoose/screenshots/blob/51466fa599efe6d9821715616106c1712aad00c3/nvim-colorizer.lua/tailwind_update_names.png)

To improve highlighting performance with the slow Tailwind LSP, results from LSP
are cached and returned on `WinScrolled` event.

Expand Down
14 changes: 9 additions & 5 deletions doc/colorizer.txt
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,7 @@ options *colorizer.config.options*
{filetypes} -
{buftypes} -
{user_commands} -
{lazy_load} -
{user_default_options} -
{exclusions} -
{all} -
Expand Down Expand Up @@ -630,15 +631,18 @@ opts *colorizer.config.opts*
for virtual text.
- `always_update` (boolean): If true, updates color values even if the buffer
is not focused.
{buftypes} - table|nil Optional. A list of buffer types where colorizer
should be enabled. Defaults to all buffer types if not provided.
{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:
{buftypes} - (table|nil): Optional. A list of buffer types where
colorizer should be enabled. Defaults to all buffer types if not
provided.
{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:
- `"ColorizerAttachToBuffer"`
- `"ColorizerDetachFromBuffer"`
- `"ColorizerReloadAllBuffers"`
- `"ColorizerToggle"`
{lazy_load} - (boolean): If true, lazily schedule buffer highlighting
setup function



Expand Down
10 changes: 8 additions & 2 deletions doc/modules/colorizer.config.html
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,9 @@ <h3>Fields:</h3>
</li>
<li><span class="parameter">user_commands</span>

</li>
<li><span class="parameter">lazy_load</span>

</li>
<li><span class="parameter">user_default_options</span>

Expand Down Expand Up @@ -435,15 +438,18 @@ <h3>Fields:</h3>
- `always_update` (boolean): If true, updates color values even if the buffer is not focused.
</li>
<li><span class="parameter">buftypes</span>
table|nil Optional. A list of buffer types where colorizer should be enabled. Defaults to all buffer types if not provided.
(table|nil): Optional. A list of buffer types where colorizer should be enabled. Defaults to all buffer types if not provided.
</li>
<li><span class="parameter">user_commands</span>
boolean|table If true, enables all user commands for colorizer. If `false`, disables user commands. Alternatively, provide a table of specific commands to enable:
(boolean|table): If true, enables all user commands for colorizer. If `false`, disables user commands. Alternatively, provide a table of specific commands to enable:
- `"ColorizerAttachToBuffer"`
- `"ColorizerDetachFromBuffer"`
- `"ColorizerReloadAllBuffers"`
- `"ColorizerToggle"`
</li>
<li><span class="parameter">lazy_load</span>
(boolean): If true, lazily schedule buffer highlighting setup function
</li>
</ul>


Expand Down
8 changes: 7 additions & 1 deletion lua/colorizer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,13 @@ function M.setup(opts)
group = colorizer_state.augroup,
pattern = bo_type == "filetype" and (s.all[bo_type] and "*" or list) or nil,
callback = function()
setup(bo_type)
if s.lazy_load then
vim.schedule(function()
setup(bo_type)
end)
else
setup(bo_type)
end
end,
})
end
Expand Down
7 changes: 5 additions & 2 deletions lua/colorizer/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ local plugin_user_default_options = {
--@field filetypes
--@field buftypes
--@field user_commands
--@field lazy_load
--@field user_default_options
--@field exclusions
--@field all
Expand All @@ -83,6 +84,7 @@ local function init_options()
filetypes = { "*" },
buftypes = {},
user_commands = true,
lazy_load = false,
user_default_options = plugin_user_default_options,
-- shortcuts for filetype, buftype inclusion, exclusion settings
exclusions = { buftype = {}, filetype = {} },
Expand Down Expand Up @@ -202,12 +204,13 @@ end
-- - `virtualtext_inline` (boolean|'before'|'after'): Shows the virtual text inline with the color. True defaults to 'before'. False or nil disables.
-- - `virtualtext_mode` ('background'|'foreground'): Determines the display mode for virtual text.
-- - `always_update` (boolean): If true, updates color values even if the buffer is not focused.
-- @field buftypes table|nil Optional. A list of buffer types where colorizer should be enabled. Defaults to all buffer types if not provided.
-- @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:
-- @field buftypes (table|nil): Optional. A list of buffer types where colorizer should be enabled. Defaults to all buffer types if not provided.
-- @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:
-- - `"ColorizerAttachToBuffer"`
-- - `"ColorizerDetachFromBuffer"`
-- - `"ColorizerReloadAllBuffers"`
-- - `"ColorizerToggle"`
-- @field lazy_load (boolean): If true, lazily schedule buffer highlighting setup function

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

0 comments on commit 8c7f243

Please sign in to comment.