Skip to content

Commit

Permalink
fix: colorizertoggle not attaching (#104)
Browse files Browse the repository at this point in the history
* ref: is_buffer_attached returns -1 if colorizer is not attached to bufnr

* fix: ColorizerToggle reattaches to buffer and highlights from user_default_options

* fix: when resolving aliases, do not mutate config.user_default_options
  • Loading branch information
catgoose authored Nov 22, 2024
1 parent 486212f commit 4487342
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 81 deletions.
19 changes: 8 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,23 +302,20 @@ or use `:h colorizer` once installed.

## Testing

To test colorization with your config, edit `test/expect.txt` to see expected
highlights. The returned table from `text/expect.txt` can be edited and when
saved will reattach the Colorizer with those settings.
For troubleshooting use `test/minimal.lua`.
Startup neovim with `nvim --clean -u minimal.lua` in the `test` directory.

For troubleshooting use `test/minimal.lua`. Edit the file to configure colorizer.
Run `minimal.lua` to startup neovim:

```bash
nvim --clean -u minimal.lua expect.txt
```

Also there is a script that you can run from the root of the repo:
Alternatively, use the following script from root directory:

```bash
scripts/start_minimal.sh
```

To test colorization with your config, edit `test/expect.txt` to see expected
highlights.
The returned table of `user_default_options` from `text/expect.txt` will be used
to conveniently reattach Colorizer to `test/expect.txt` on save.

## Extras

Documentaion is generated using ldoc. See
Expand Down
9 changes: 8 additions & 1 deletion doc/colorizer.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ is_buffer_attached({bufnr}) *colorizer.is_buffer_attached*
{bufnr} - number|nil: buffer number (0 for current)

returns:~
number or nil: if attached to the buffer, false otherwise.
number: returns bufnr if attached, otherwise -1

See also:~
|colorizer.buffer.highlight|
Expand Down Expand Up @@ -439,6 +439,8 @@ Functions: ~
Tables: ~
|user_default_options| - Default user options for colorizer.

|default_options| - Plugin default options cache from vim.deepcopy

|opts| - Configuration options for the `setup` function.


Expand Down Expand Up @@ -547,6 +549,11 @@ user_default_options *colorizer.config.user_default_options*



default_options *colorizer.config.default_options*
Plugin default options cache from vim.deepcopy



opts *colorizer.config.opts*
Configuration options for the `setup` function.

Expand Down
18 changes: 18 additions & 0 deletions doc/modules/colorizer.config.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ <h2><a href="#Tables">Tables</a></h2>
<td class="summary">Default user options for colorizer.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#default_options">default_options</a></td>
<td class="summary">Plugin default options cache from vim.deepcopy</td>
</tr>
<tr>
<td class="name" nowrap><a href="#opts">opts</a></td>
<td class="summary">Configuration options for the `setup` function.</td>
</tr>
Expand Down Expand Up @@ -317,6 +321,20 @@ <h3>Fields:</h3>



</dd>
<dt>
<a name = "default_options"></a>
<strong>default_options</strong>
</dt>
<dd>
Plugin default options cache from vim.deepcopy







</dd>
<dt>
<a name = "opts"></a>
Expand Down
2 changes: 1 addition & 1 deletion doc/modules/colorizer.html
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ <h3>Parameters:</h3>
<h3>Returns:</h3>
<ol>

number|nil: if attached to the buffer, false otherwise.
number: returns bufnr if attached, otherwise -1
</ol>


Expand Down
22 changes: 11 additions & 11 deletions lua/colorizer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,15 @@ end

---Check if attached to a buffer
---@param bufnr number|nil: buffer number (0 for current)
---@return number|nil: if attached to the buffer, false otherwise.
---@return number: returns bufnr if attached, otherwise -1
---@see colorizer.buffer.highlight
function M.is_buffer_attached(bufnr)
if bufnr == 0 or not bufnr then
bufnr = utils.bufme(bufnr)
else
if not vim.api.nvim_buf_is_valid(bufnr) then
colorizer_state.buffer_local[bufnr], colorizer_state.buffer_options[bufnr] = nil, nil
return
return -1
end
end
local au = vim.api.nvim_get_autocmds({
Expand All @@ -189,7 +189,7 @@ function M.is_buffer_attached(bufnr)
buffer = bufnr,
})
if not colorizer_state.buffer_options[bufnr] or vim.tbl_isempty(au) then
return
return -1
end
return bufnr
end
Expand All @@ -198,9 +198,9 @@ end
---@param bufnr number: Buffer number (0 for current)
---@return table|nil
local function get_buffer_options(bufnr)
local _bufnr = M.is_buffer_attached(bufnr)
if _bufnr then
return colorizer_state.buffer_options[_bufnr]
local attached_bufnr = M.is_buffer_attached(bufnr)
if attached_bufnr > -1 then
return colorizer_state.buffer_options[attached_bufnr]
end
end

Expand All @@ -227,9 +227,9 @@ function M.attach_to_buffer(bufnr, options, bo_type)
end

-- set options by grabbing existing or creating new options, then parsing
options = config.parse_buffer_options(
options or get_buffer_options(bufnr) or config.new_buffer_options(bufnr, bo_type)
)
local got_options = get_buffer_options(bufnr)
local new_options = config.new_buffer_options(bufnr, bo_type)
options = config.parse_buffer_options(options or got_options or new_options)

if not buffer.highlight_mode_names[options.mode] then
if options.mode ~= nil then
Expand Down Expand Up @@ -344,8 +344,8 @@ end
function M.detach_from_buffer(bufnr)
bufnr = utils.bufme(bufnr)
bufnr = M.is_buffer_attached(bufnr)
if not bufnr then
return
if bufnr < 0 then
return -1
end
vim.api.nvim_buf_clear_namespace(bufnr, buffer.default_namespace, 0, -1)
if colorizer_state.buffer_local[bufnr] then
Expand Down
7 changes: 1 addition & 6 deletions lua/colorizer/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,7 @@ local function create_highlight(rgb_hex, mode)
else
local rr, gg, bb = rgb_hex:sub(1, 2), rgb_hex:sub(3, 4), rgb_hex:sub(5, 6)
local r, g, b = tonumber(rr, 16), tonumber(gg, 16), tonumber(bb, 16)
local fg_color
if color.is_bright(r, g, b) then
fg_color = "Black"
else
fg_color = "White"
end
local fg_color = color.is_bright(r, g, b) and "Black" or "White"
vim.api.nvim_set_hl(0, highlight_name, { fg = fg_color, bg = "#" .. rgb_hex })
end
hl_state.cache[cache_key] = highlight_name
Expand Down
71 changes: 37 additions & 34 deletions lua/colorizer/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,25 @@ local M = {}
local utils = require("colorizer.utils")

--- Defaults for colorizer options
local _user_defaults = {
RGB = true,
RRGGBB = true,
names = true,
RRGGBBAA = false,
AARRGGBB = false,
rgb_fn = false,
hsl_fn = false,
css = false,
css_fn = false,
mode = "background",
tailwind = false,
sass = { enable = false, parsers = { css = true } },
virtualtext = "",
virtualtext_inline = false,
virtualtext_mode = "foreground",
always_update = false,
}
local function user_defaults()
return vim.deepcopy(_user_defaults)
return vim.deepcopy({
RGB = true,
RRGGBB = true,
names = true,
RRGGBBAA = false,
AARRGGBB = false,
rgb_fn = false,
hsl_fn = false,
css = false,
css_fn = false,
mode = "background",
tailwind = false,
sass = { enable = false, parsers = { css = true } },
virtualtext = "",
virtualtext_inline = false,
virtualtext_mode = "foreground",
always_update = false,
})
end

--- Default user options for colorizer.
Expand Down Expand Up @@ -58,7 +57,7 @@ end
-- @field virtualtext_mode 'background'|'foreground': Mode for virtual text display.
-- @field always_update boolean: Always update color values, even if buffer is not focused.

-- Default options for the user
-- Configured user options
---@table user_default_options
--@field RGB boolean
--@field RRGGBB boolean
Expand All @@ -76,12 +75,15 @@ end
--@field virtualtext_inline boolean
--@field virtualtext_mode 'background'|'foreground'
--@field always_update boolean
M.user_default_options = user_defaults()
M.user_default_options = nil

--- Plugin default options cache from vim.deepcopy
---@table default_options
local plugin_default_options = user_defaults()

-- State for managing buffer and filetype-specific options
local options_state = { buftype = {}, filetype = {} }

-- TODO: 2024-11-20 - use vim.validate?
--- Validates user options and sets defaults if necessary.
local function validate_opts(settings)
if
Expand All @@ -90,20 +92,20 @@ local function validate_opts(settings)
settings.default_options.mode
)
then
settings.default_options.mode = M.user_default_options.mode
settings.default_options.mode = plugin_default_options.mode
end
if
not vim.tbl_contains({ "background", "foreground" }, settings.default_options.virtualtext_mode)
then
settings.default_options.virtualtext_mode = M.user_default_options.virtualtext_mode
settings.default_options.virtualtext_mode = plugin_default_options.virtualtext_mode
end
if
not vim.tbl_contains(
{ true, false, "normal", "lsp", "both" },
settings.default_options.tailwind
)
then
settings.default_options.tailwind = M.user_default_options.tailwind
settings.default_options.tailwind = plugin_default_options.tailwind
end
return settings
end
Expand Down Expand Up @@ -148,23 +150,24 @@ function M.get_settings(opts)
filetypes = { "*" },
buftypes = nil,
user_commands = true,
user_default_options = user_defaults(),
user_default_options = plugin_default_options,
}
-- TODO: 2024-11-21 - verify that vim.tbl_deep_extend is doing what it should
opts = vim.tbl_deep_extend("force", default_opts, opts)
local settings = {
exclusions = { buftype = {}, filetype = {} },
all = { buftype = false, filetype = false },
default_options = vim.tbl_deep_extend(
"force",
M.user_default_options,
plugin_default_options,
opts.user_default_options
),
user_commands = opts.user_commands,
filetypes = opts.filetypes,
buftypes = opts.buftypes,
}
validate_opts(settings)

M.user_default_options = settings.default_options
return settings
end

Expand Down Expand Up @@ -197,11 +200,11 @@ end
---@param options table: options table
---@return table
function M.parse_buffer_options(options)
local default = vim.deepcopy(M.user_default_options)
local includes = {
["css"] = { "names", "RGB", "RRGGBB", "RRGGBBAA", "hsl_fn", "rgb_fn" },
["css_fn"] = { "hsl_fn", "rgb_fn" },
}
local default_opts = M.user_default_options

local function handle_alias(name, opts, d_opts)
if not includes[name] then
Expand All @@ -213,23 +216,23 @@ function M.parse_buffer_options(options)
end

-- https://github.com/NvChad/nvim-colorizer.lua/issues/48
handle_alias("css", options, default_opts)
handle_alias("css_fn", options, default_opts)
handle_alias("css", options, default)
handle_alias("css_fn", options, default)

if options.sass then
if type(options.sass.parsers) == "table" then
for child, _ in pairs(options.sass.parsers) do
handle_alias(child, options.sass.parsers, default_opts.sass.parsers)
handle_alias(child, options.sass.parsers, default.sass.parsers)
end
else
options.sass.parsers = {}
for child, _ in pairs(default_opts.sass.parsers) do
for child, _ in pairs(default.sass.parsers) do
handle_alias(child, true, options.sass.parsers)
end
end
end

options = utils.merge(default_opts, options)
options = utils.merge(default, options)
return options
end
return M
6 changes: 3 additions & 3 deletions lua/colorizer/usercmds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ function M.make(cmds)
ColorizerDetachFromBuffer = wrap("ColorizerDetachFromBuffer", c.detach_from_buffer),
ColorizerReloadAllBuffers = wrap("ColorizerReloadAllBuffers", c.reload_all_buffers),
ColorizerToggle = wrap("ColorizerToggle", function()
if c.is_buffer_attached() then
c.detach_from_buffer()
else
if c.is_buffer_attached() < 0 then
c.attach_to_buffer()
else
c.detach_from_buffer()
end
end),
}
Expand Down
Loading

0 comments on commit 4487342

Please sign in to comment.