Fix/112 aliases application (#115)

* doc: updates readme

* ref: reset config cache when setting up colorizer

* fix: correctly apply alias value to it's target options

* doc: updates docs

* ref: simplifies setup configuration

* ref: refactor config options

* ref: calling colorizer.setup should re-initialize config.options

* ref: refactors names parser

* ref: refactors config module
This commit is contained in:
Meow Honk 2024-12-24 17:02:25 -06:00 committed by GitHub
parent b8ed35bf2c
commit cbbd548dd0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 308 additions and 402 deletions

View file

@ -126,7 +126,8 @@ library to do custom highlighting themselves.
names = true, -- "Name" codes like Blue or blue
-- Expects a table of color name to rgb value pairs. # is optional
-- Example: { cool = "#107dac", ["notcool"] = "ee9240" }
names_custom = nil, -- Extra names to be highlighted: table|function|nil
-- Set to false|nil to disable
names_custom = false, -- Custom names to be highlighted: table|function|false|nil
RGB = true, -- #RGB hex codes
RRGGBB = true, -- #RRGGBB hex codes
RRGGBBAA = false, -- #RRGGBBAA hex codes
@ -167,10 +168,10 @@ MODES:
For basic setup, you can use a command like the following.
```lua
-- Attaches to every FileType mode
-- Attaches to every FileType with default options
require("colorizer").setup()
-- Attach to certain Filetypes, add special configuration for `html`
-- Attach to certain Filetypes, add special `mode` configuration for `html`
-- Use `background` for everything else.
require("colorizer").setup({
filetypes = {
@ -180,8 +181,8 @@ require("colorizer").setup({
},
})
-- Use the `default_options` as the second parameter, which uses
-- `foreground` for every mode. This is the inverse of the previous
-- Use `user_default_options` as the second parameter, which uses
-- `background` for every mode. This is the inverse of the previous
-- setup configuration.
require("colorizer").setup({
filetypes = {
@ -192,9 +193,7 @@ require("colorizer").setup({
user_default_options = { mode = "background" },
})
-- Use the `default_options` as the second parameter, which uses
-- `foreground` for every mode. This is the inverse of the previous
-- setup configuration.
-- Use default for all filetypes with overrides for css and html
require("colorizer").setup({
filetypes = {
"*", -- Highlight all files, but customize some others.
@ -208,7 +207,7 @@ require("colorizer").setup({
filetypes = {
"*", -- Highlight all files, but customize some others.
"!vim", -- Exclude vim from highlighting.
-- Exclusion Only makes sense if '*' is specified!
-- Exclusion Only makes sense if '*' is specified first!
},
})
@ -332,10 +331,10 @@ Alternatively, use the following script from root directory:
scripts/start_minimal.sh
```
To test colorization with your config, edit `test/expect.txt` to see expected
To test colorization with your config, edit `test/expect.lua` 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.
The returned table of `user_default_options` from `text/expect.lua` will be used
to conveniently reattach Colorizer to `test/expect.lua` on save.
## Extras
@ -348,7 +347,6 @@ Documentaion is generated using ldoc. See
- [ ] Add more display modes. E.g - sign column
- [ ] Use a more space efficient trie implementation.
- [ ] Support custom parsers
- [ ] Allow custom color names
## Similar projects

View file

@ -437,68 +437,25 @@ Provides configuration options and utilities for setting up colorizer.
LUA API *colorizer.config-lua-api*
Functions: ~
|get_settings| - Initializes colorizer with user-provided options.
|new_buffer_options| - Retrieve default options or buffer-specific options.
|get_options| - Retrieve options based on buffer type and file type.
|set_bo_value| - Set options for a specific buffer or file type.
|parse_buffer_options| - Parse buffer Configuration and convert aliases to
normal values
|apply_alias_options| - Parse and apply alias options to the user options.
|get_setup_options| - Initializes colorizer with user-provided options.
|new_bo_options| - Retrieve buffer-specific options or user_default_options
defined when setup() was called.
|get_bo_options| - Retrieve options based on buffer type and file type.
Tables: ~
|user_default_options| - Default user options for colorizer.
|setup_options| - Options for colorizer that were passed in to setup
function
|default_options| - Plugin default options cache from vim.deepcopy
|options| - Options for colorizer that were passed in to setup function
|opts| - Configuration options for the `setup` function.
get_settings({opts}) *colorizer.config.get_settings*
Initializes colorizer with user-provided options.
Merges default settings with any user-specified options, setting up
`filetypes`,
`user_default_options`, and `user_commands`.
Parameters: ~
{opts} - opts User-provided configuration options.
returns:~
table Final settings after merging user and default options.
new_buffer_options({bufnr}, {bo_type}) *colorizer.config.new_buffer_options*
Retrieve default options or buffer-specific options.
Parameters: ~
{bufnr} - number: The buffer number.
{bo_type} - 'buftype'|'filetype': The type of buffer option
get_options({bo_type}, {buftype}, {filetype}) *colorizer.config.get_options*
Retrieve options based on buffer type and file type.
Prefer filetype.
Parameters: ~
{bo_type} - 'buftype'|'filetype': The type of buffer option
{buftype} - string: Buffer type.
{filetype} - string: File type.
returns:~
table
set_bo_value({bo_type}, {value}, {options}) *colorizer.config.set_bo_value*
Set options for a specific buffer or file type.
@ -509,11 +466,54 @@ set_bo_value({bo_type}, {value}, {options}) *colorizer.config.set_bo_value*
parse_buffer_options({options}) *colorizer.config.parse_buffer_options*
Parse buffer Configuration and convert aliases to normal values
apply_alias_options({options}) *colorizer.config.apply_alias_options*
Parse and apply alias options to the user options.
Parameters: ~
{options} - table: options table
{options} - table: user_default_options
returns:~
table
get_setup_options({opts}) *colorizer.config.get_setup_options*
Initializes colorizer with user-provided options.
Merges default settings with any user-specified options, setting up
`filetypes`,
`user_default_options`, and `user_commands`.
Parameters: ~
{opts} - table: Configuration options for colorizer.
returns:~
table Final settings after merging user and default options.
new_bo_options({bufnr}, {bo_type}) *colorizer.config.new_bo_options*
Retrieve buffer-specific options or user_default_options defined when
setup() was called.
Parameters: ~
{bufnr} - number: The buffer number.
{bo_type} - 'buftype'|'filetype': The type of buffer option
*colorizer.config.get_bo_options*
get_bo_options({bo_type}, {buftype}, {filetype})
Retrieve options based on buffer type and file type.
Prefer filetype.
Parameters: ~
{bo_type} - 'buftype'|'filetype': The type of buffer option
{buftype} - string: Buffer type.
{filetype} - string: File type.
returns:~
table
@ -569,21 +569,17 @@ user_default_options *colorizer.config.user_default_options*
setup_options *colorizer.config.setup_options*
options *colorizer.config.options*
Options for colorizer that were passed in to setup function
Fields: ~
{exclusions} - table
{all} - table
{default_options} - table
{user_commands} - boolean
{filetypes} - table
{buftypes} - table
default_options *colorizer.config.default_options*
Plugin default options cache from vim.deepcopy
{setup_options} -
{exclusions} -
{all} -
{default_options} -
{user_commands} -
{filetypes} -
{buftypes} -
@ -597,7 +593,7 @@ opts *colorizer.config.opts*
- `RGB` (boolean): Enables support for `#RGB` hex codes.
- `RRGGBB` (boolean): Enables support for `#RRGGBB` hex codes.
- `names` (boolean): Enables named color codes like `"Blue"`.
- `names_custom` (table|function): Extra color name to RGB value mappings
- `names_custom` (table|function|nil): Extra color name to RGB value mappings
- `RRGGBBAA` (boolean): Enables support for `#RRGGBBAA` hex codes.
- `AARRGGBB` (boolean): Enables support for `0xAARRGGBB` hex codes.
- `rgb_fn` (boolean): Enables CSS `rgb()` and `rgba()` functions.
@ -689,13 +685,11 @@ This module provides a parser for identifying and converting `hsl()` and
LUA API *colorizer.parser.hsl-lua-api*
Functions: ~
|hsl_function_parser| - Parses `hsl()` and `hsla()` CSS functions and
converts them to RGB hexadecimal format.
|parser| - Parses `hsl()` and `hsla()` CSS functions and converts them to
RGB hexadecimal format.
*colorizer.parser.hsl.hsl_function_parser*
hsl_function_parser({line}, {i}, {opts})
parser({line}, {i}, {opts}) *colorizer.parser.hsl.parser*
Parses `hsl()` and `hsla()` CSS functions and converts them to RGB
hexadecimal format.
@ -862,13 +856,11 @@ This module provides a parser for identifying and converting `rgb()` and
LUA API *colorizer.parser.rgb-lua-api*
Functions: ~
|rgb_function_parser| - Parses `rgb()` and `rgba()` CSS functions and
converts them to RGB hexadecimal format.
|parser| - Parses `rgb()` and `rgba()` CSS functions and converts them to
RGB hexadecimal format.
*colorizer.parser.rgb.rgb_function_parser*
rgb_function_parser({line}, {i}, {opts})
parser({line}, {i}, {opts}) *colorizer.parser.rgb.parser*
Parses `rgb()` and `rgba()` CSS functions and converts them to RGB
hexadecimal format.

View file

@ -68,25 +68,25 @@
<h2><a href="#Functions">Functions</a></h2>
<table class="function_list">
<tr>
<td class="name" nowrap><a href="#get_settings">get_settings (opts)</a></td>
<td class="summary">Initializes colorizer with user-provided options.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#new_buffer_options">new_buffer_options (bufnr, bo_type)</a></td>
<td class="summary">Retrieve default options or buffer-specific options.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#get_options">get_options (bo_type, buftype, filetype)</a></td>
<td class="summary">Retrieve options based on buffer type and file type.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#set_bo_value">set_bo_value (bo_type, value, options)</a></td>
<td class="summary">Set options for a specific buffer or file type.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#parse_buffer_options">parse_buffer_options (options)</a></td>
<td class="summary">Parse buffer Configuration and convert aliases to normal values</td>
<td class="name" nowrap><a href="#apply_alias_options">apply_alias_options (options)</a></td>
<td class="summary">Parse and apply alias options to the user options.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#get_setup_options">get_setup_options (opts)</a></td>
<td class="summary">Initializes colorizer with user-provided options.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#new_bo_options">new_bo_options (bufnr, bo_type)</a></td>
<td class="summary">Retrieve buffer-specific options or user_default_options defined when setup() was called.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#get_bo_options">get_bo_options (bo_type, buftype, filetype)</a></td>
<td class="summary">Retrieve options based on buffer type and file type.</td>
</tr>
</table>
<h2><a href="#Tables">Tables</a></h2>
@ -96,14 +96,10 @@
<td class="summary">Default user options for colorizer.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#setup_options">setup_options</a></td>
<td class="name" nowrap><a href="#options">options</a></td>
<td class="summary">Options for colorizer that were passed in to setup function</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>
@ -116,87 +112,6 @@
<h2 class="section-header "><a name="Functions"></a>Functions</h2>
<dl class="function">
<dt>
<a name = "get_settings"></a>
<strong>get_settings (opts)</strong>
</dt>
<dd>
Initializes colorizer with user-provided options.
Merges default settings with any user-specified options, setting up `filetypes`,
`user_default_options`, and `user_commands`.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">opts</span>
opts User-provided configuration options.
</li>
</ul>
<h3>Returns:</h3>
<ol>
table Final settings after merging user and default options.
</ol>
</dd>
<dt>
<a name = "new_buffer_options"></a>
<strong>new_buffer_options (bufnr, bo_type)</strong>
</dt>
<dd>
Retrieve default options or buffer-specific options.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">bufnr</span>
number: The buffer number.
</li>
<li><span class="parameter">bo_type</span>
'buftype'|'filetype': The type of buffer option
</li>
</ul>
</dd>
<dt>
<a name = "get_options"></a>
<strong>get_options (bo_type, buftype, filetype)</strong>
</dt>
<dd>
Retrieve options based on buffer type and file type. Prefer filetype.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">bo_type</span>
'buftype'|'filetype': The type of buffer option
</li>
<li><span class="parameter">buftype</span>
string: Buffer type.
</li>
<li><span class="parameter">filetype</span>
string: File type.
</li>
</ul>
<h3>Returns:</h3>
<ol>
table
</ol>
</dd>
<dt>
<a name = "set_bo_value"></a>
<strong>set_bo_value (bo_type, value, options)</strong>
@ -224,17 +139,98 @@
</dd>
<dt>
<a name = "parse_buffer_options"></a>
<strong>parse_buffer_options (options)</strong>
<a name = "apply_alias_options"></a>
<strong>apply_alias_options (options)</strong>
</dt>
<dd>
Parse buffer Configuration and convert aliases to normal values
Parse and apply alias options to the user options.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">options</span>
table: options table
table: user_default_options
</li>
</ul>
<h3>Returns:</h3>
<ol>
table
</ol>
</dd>
<dt>
<a name = "get_setup_options"></a>
<strong>get_setup_options (opts)</strong>
</dt>
<dd>
Initializes colorizer with user-provided options.
Merges default settings with any user-specified options, setting up `filetypes`,
`user_default_options`, and `user_commands`.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">opts</span>
table: Configuration options for colorizer.
</li>
</ul>
<h3>Returns:</h3>
<ol>
table Final settings after merging user and default options.
</ol>
</dd>
<dt>
<a name = "new_bo_options"></a>
<strong>new_bo_options (bufnr, bo_type)</strong>
</dt>
<dd>
Retrieve buffer-specific options or user_default_options defined when setup() was called.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">bufnr</span>
number: The buffer number.
</li>
<li><span class="parameter">bo_type</span>
'buftype'|'filetype': The type of buffer option
</li>
</ul>
</dd>
<dt>
<a name = "get_bo_options"></a>
<strong>get_bo_options (bo_type, buftype, filetype)</strong>
</dt>
<dd>
Retrieve options based on buffer type and file type. Prefer filetype.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">bo_type</span>
'buftype'|'filetype': The type of buffer option
</li>
<li><span class="parameter">buftype</span>
string: Buffer type.
</li>
<li><span class="parameter">filetype</span>
string: File type.
</li>
</ul>
@ -331,8 +327,8 @@
</dd>
<dt>
<a name = "setup_options"></a>
<strong>setup_options</strong>
<a name = "options"></a>
<strong>options</strong>
</dt>
<dd>
Options for colorizer that were passed in to setup function
@ -340,23 +336,26 @@
<h3>Fields:</h3>
<ul>
<li><span class="parameter">setup_options</span>
</li>
<li><span class="parameter">exclusions</span>
table
</li>
<li><span class="parameter">all</span>
table
</li>
<li><span class="parameter">default_options</span>
table
</li>
<li><span class="parameter">user_commands</span>
boolean
</li>
<li><span class="parameter">filetypes</span>
table
</li>
<li><span class="parameter">buftypes</span>
table
</li>
</ul>
@ -364,20 +363,6 @@
</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>
@ -397,7 +382,7 @@
- `RGB` (boolean): Enables support for `#RGB` hex codes.
- `RRGGBB` (boolean): Enables support for `#RRGGBB` hex codes.
- `names` (boolean): Enables named color codes like `"Blue"`.
- `names_custom` (table|function): Extra color name to RGB value mappings
- `names_custom` (table|function|nil): Extra color name to RGB value mappings
- `RRGGBBAA` (boolean): Enables support for `#RRGGBBAA` hex codes.
- `AARRGGBB` (boolean): Enables support for `0xAARRGGBB` hex codes.
- `rgb_fn` (boolean): Enables CSS `rgb()` and `rgba()` functions.

View file

@ -70,7 +70,7 @@
<h2><a href="#Functions">Functions</a></h2>
<table class="function_list">
<tr>
<td class="name" nowrap><a href="#hsl_function_parser">hsl_function_parser (line, i, opts)</a></td>
<td class="name" nowrap><a href="#parser">parser (line, i, opts)</a></td>
<td class="summary">Parses `hsl()` and `hsla()` CSS functions and converts them to RGB hexadecimal format.</td>
</tr>
</table>
@ -83,8 +83,8 @@
<dl class="function">
<dt>
<a name = "hsl_function_parser"></a>
<strong>hsl_function_parser (line, i, opts)</strong>
<a name = "parser"></a>
<strong>parser (line, i, opts)</strong>
</dt>
<dd>
Parses `hsl()` and `hsla()` CSS functions and converts them to RGB hexadecimal format.

View file

@ -70,7 +70,7 @@
<h2><a href="#Functions">Functions</a></h2>
<table class="function_list">
<tr>
<td class="name" nowrap><a href="#rgb_function_parser">rgb_function_parser (line, i, opts)</a></td>
<td class="name" nowrap><a href="#parser">parser (line, i, opts)</a></td>
<td class="summary">Parses `rgb()` and `rgba()` CSS functions and converts them to RGB hexadecimal format.</td>
</tr>
</table>
@ -83,8 +83,8 @@
<dl class="function">
<dt>
<a name = "rgb_function_parser"></a>
<strong>rgb_function_parser (line, i, opts)</strong>
<a name = "parser"></a>
<strong>parser (line, i, opts)</strong>
</dt>
<dd>
Parses `rgb()` and `rgba()` CSS functions and converts them to RGB hexadecimal format.

View file

@ -195,10 +195,10 @@ function M.is_buffer_attached(bufnr)
return bufnr
end
--- Return the currently active buffer options.
--- Return buffer options if buffer is attached to colorizer.
---@param bufnr number: Buffer number (0 for current)
---@return table|nil
local function get_buffer_options(bufnr)
local function get_attached_buffer_options(bufnr)
local attached_bufnr = M.is_buffer_attached(bufnr)
if attached_bufnr > -1 then
return colorizer_state.buffer_options[attached_bufnr]
@ -209,7 +209,7 @@ end
function M.reload_all_buffers()
for bufnr, _ in pairs(colorizer_state.buffer_options) do
bufnr = utils.bufme(bufnr)
M.attach_to_buffer(bufnr, get_buffer_options(bufnr), "buftype")
M.attach_to_buffer(bufnr, get_attached_buffer_options(bufnr), "buftype")
end
end
@ -262,18 +262,15 @@ function M.attach_to_buffer(bufnr, options, bo_type)
colorizer_state.buffer_local[bufnr], colorizer_state.buffer_options[bufnr] = nil, nil
return
end
-- TODO: 2024-11-22 - When attaching using user command (args to attach_to_buffer are all nil) does this respect buffer settings?
options = options or config.setup_options.filetypes[vim.bo.filetype] or {}
-- TODO: 2024-11-22 - Why default to "buftype"?
bo_type = vim.tbl_contains({ "buftype", "filetype" }, bo_type) and bo_type or "buftype"
-- check if options is empty table
options = next(options) and options
-- options for filetype
options = config.options.filetypes[vim.bo.filetype]
-- cached buffer options
or get_buffer_options(bufnr)
or get_attached_buffer_options(bufnr)
-- new buffer options
or config.new_buffer_options(bufnr, bo_type)
options = config.parse_buffer_options(options)
or config.new_options(bufnr, bo_type)
options = config.apply_alias_options(options)
-- TODO: 2024-11-26 - This seems to be validated in config.validate_opts
if not buffer.highlight_mode_names[options.mode] then
@ -453,7 +450,8 @@ function M.setup(opts)
}
require("colorizer.matcher").reset_cache()
require("colorizer.parser.names").reset_cache()
local s = config.get_settings(opts)
require("colorizer.config").reset_cache()
local s = config.get_setup_options(opts)
-- Setup the buffer with the correct options
local function setup(bo_type)
@ -483,6 +481,7 @@ function M.setup(opts)
end
end
-- TODO: 2024-12-24 - refactor this
-- Setup highlighting autocmds for filetypes and buftypes
local bo_type_ac = {
filetype = "FileType",
@ -502,13 +501,13 @@ function M.setup(opts)
local list = {}
for k, v in pairs(bo_type_option) do
local value
local options = s.default_options
local options = s.user_default_options
if type(k) == "string" then
value = k
if type(v) ~= "table" then
vim.notify(string.format("colorizer: Invalid option type for %s", value), 4)
else
options = vim.tbl_extend("force", s.default_options, v)
options = vim.tbl_extend("force", options, v)
end
else
value = v

View file

@ -3,27 +3,25 @@
local M = {}
--- Defaults for colorizer options
local function user_defaults()
return vim.deepcopy({
names = true,
names_custom = nil,
RGB = true,
RRGGBB = 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
local plugin_user_default_options = {
names = true,
names_custom = false,
RGB = true,
RRGGBB = 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,
}
--- Default user options for colorizer.
-- This table defines individual options and alias options, allowing configuration of
@ -58,69 +56,70 @@ 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.
-- Configured user options
---@table user_default_options
--@field RGB boolean
--@field RRGGBB boolean
--@field names boolean
--@field names_custom nil
--@field RRGGBBAA boolean
--@field AARRGGBB boolean
--@field rgb_fn boolean
--@field hsl_fn boolean
--@field css boolean
--@field css_fn boolean
--@field mode 'background'|'foreground'|'virtualtext'
--@field tailwind boolean|string
--@field sass table
--@field virtualtext string
--@field virtualtext_inline boolean
--@field virtualtext_mode 'background'|'foreground'
--@field always_update boolean
M.user_default_options = nil
--- Options for colorizer that were passed in to setup function
---@table setup_options
--@field exclusions table
--@field all table
--@field default_options table
--@field user_commands boolean
--@field filetypes table
--@field buftypes table
M.setup_options = nil
--@field setup_options
--@field exclusions
--@field all
--@field default_options
--@field user_commands
--@field filetypes
--@field buftypes
M.options = {}
--- Plugin default options cache from vim.deepcopy
---@table default_options
local plugin_default_options = user_defaults()
local function init_options()
M.options = {
-- setup options
filetypes = { "*" },
buftypes = {},
user_commands = true,
user_default_options = plugin_user_default_options,
-- shortcuts for filetype, buftype inclusion, exclusion settings
exclusions = { buftype = {}, filetype = {} },
all = { buftype = false, filetype = false },
}
end
-- State for managing buffer and filetype-specific options
local options_state
local options_cache = { buftype = {}, filetype = {} }
-- TODO: 2024-11-26 - background is being validated in colorizer.attach_to_buffer
--- Validates user options and sets defaults if necessary.
local function validate_opts(settings)
if
not vim.tbl_contains(
{ "background", "foreground", "virtualtext" },
settings.default_options.mode
)
then
settings.default_options.mode = plugin_default_options.mode
--- Set options for a specific buffer or file type.
---@param bo_type 'buftype'|'filetype': The type of buffer option
---@param value string: The specific value to set.
---@param options table: Options to associate with the value.
function M.set_bo_value(bo_type, value, options)
options_cache[bo_type][value] = options
end
--- Parse and apply alias options to the user options.
---@param options table: user_default_options
---@return table
function M.apply_alias_options(options)
local aliases = {
["css"] = { "names", "RGB", "RRGGBB", "RRGGBBAA", "hsl_fn", "rgb_fn" },
["css_fn"] = { "hsl_fn", "rgb_fn" },
}
local function handle_alias(name, opts)
if not aliases[name] then
return
end
for _, option in ipairs(aliases[name]) do
if opts[option] == nil then
opts[option] = options[name]
end
end
end
if
not vim.tbl_contains({ "background", "foreground" }, settings.default_options.virtualtext_mode)
then
settings.default_options.virtualtext_mode = plugin_default_options.virtualtext_mode
for alias, _ in pairs(aliases) do
handle_alias(alias, options)
end
if
not vim.tbl_contains(
{ true, false, "normal", "lsp", "both" },
settings.default_options.tailwind
)
then
settings.default_options.tailwind = plugin_default_options.tailwind
if options.sass and options.sass.enable then
for child, _ in pairs(options.sass.parsers) do
handle_alias(child, options.sass.parsers)
end
end
return settings
options = vim.tbl_deep_extend("force", M.options.user_default_options, options)
return options
end
--- Configuration options for the `setup` function.
@ -130,7 +129,7 @@ end
-- - `RGB` (boolean): Enables support for `#RGB` hex codes.
-- - `RRGGBB` (boolean): Enables support for `#RRGGBB` hex codes.
-- - `names` (boolean): Enables named color codes like `"Blue"`.
-- - `names_custom` (table|function): Extra color name to RGB value mappings
-- - `names_custom` (table|function|nil): Extra color name to RGB value mappings
-- - `RRGGBBAA` (boolean): Enables support for `#RRGGBBAA` hex codes.
-- - `AARRGGBB` (boolean): Enables support for `0xAARRGGBB` hex codes.
-- - `rgb_fn` (boolean): Enables CSS `rgb()` and `rgba()` functions.
@ -156,41 +155,22 @@ end
--- Initializes colorizer with user-provided options.
-- Merges default settings with any user-specified options, setting up `filetypes`,
-- `user_default_options`, and `user_commands`.
-- @param opts opts User-provided configuration options.
-- @param opts table: Configuration options for colorizer.
-- @return table Final settings after merging user and default options.
function M.get_settings(opts)
options_state = { buftype = {}, filetype = {} }
function M.get_setup_options(opts)
init_options()
opts = opts or {}
local default_opts = {
filetypes = { "*" },
buftypes = nil,
user_commands = true,
user_default_options = plugin_default_options,
}
opts = vim.tbl_deep_extend("force", default_opts, opts)
M.setup_options = {
exclusions = { buftype = {}, filetype = {} },
all = { buftype = false, filetype = false },
default_options = vim.tbl_deep_extend(
"force",
plugin_default_options,
opts.user_default_options
),
user_commands = opts.user_commands,
filetypes = opts.filetypes,
buftypes = opts.buftypes,
}
validate_opts(M.setup_options)
M.user_default_options = M.setup_options.default_options
return M.setup_options
opts.user_default_options = M.apply_alias_options(opts.user_default_options)
M.options = vim.tbl_deep_extend("force", M.options, opts)
return M.options
end
--- Retrieve default options or buffer-specific options.
--- Retrieve buffer-specific options or user_default_options defined when setup() was called.
---@param bufnr number: The buffer number.
---@param bo_type 'buftype'|'filetype': The type of buffer option
function M.new_buffer_options(bufnr, bo_type)
function M.new_bo_options(bufnr, bo_type)
local value = vim.api.nvim_get_option_value(bo_type, { buf = bufnr })
return options_state.filetype[value] or M.user_default_options
return options_cache.filetype[value] or M.options.user_default_options
end
--- Retrieve options based on buffer type and file type. Prefer filetype.
@ -198,59 +178,13 @@ end
---@param buftype string: Buffer type.
---@param filetype string: File type.
---@return table
function M.get_options(bo_type, buftype, filetype)
local fo, bo = options_state[bo_type][filetype], options_state[bo_type][buftype]
function M.get_bo_options(bo_type, buftype, filetype)
local fo, bo = options_cache[bo_type][filetype], options_cache[bo_type][buftype]
return fo or bo
end
--- Set options for a specific buffer or file type.
---@param bo_type 'buftype'|'filetype': The type of buffer option
---@param value string: The specific value to set.
---@param options table: Options to associate with the value.
function M.set_bo_value(bo_type, value, options)
options_state[bo_type][value] = options
function M.reset_cache()
options_cache = { buftype = {}, filetype = {} }
end
--- Parse buffer Configuration and convert aliases to normal values
---@param options table: options table
---@return table
function M.parse_buffer_options(options)
local default = vim.deepcopy(M.user_default_options)
-- TODO: 2024-12-20 - verify that these are actually being set correctly
local includes = {
["css"] = { "names", "RGB", "RRGGBB", "RRGGBBAA", "hsl_fn", "rgb_fn" },
["css_fn"] = { "hsl_fn", "rgb_fn" },
}
local function handle_alias(name, opts, d_opts)
if not includes[name] then
return
end
for _, child in ipairs(includes[name]) do
if opts[name] ~= nil then
d_opts[child] = opts[name]
end
end
end
-- https://github.com/catgoose/nvim-colorizer.lua/issues/48
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.sass.parsers)
end
else
options.sass.parsers = {}
for child, _ in pairs(default.sass.parsers) do
handle_alias(child, true, options.sass.parsers)
end
end
end
options = vim.tbl_deep_extend("force", default, options)
return options
end
return M

View file

@ -9,13 +9,11 @@ local utils = require("colorizer.utils")
local tohex = require("bit").tohex
local min, max = math.min, math.max
-- Internal state encapsulation
local names_cache = {
color_map = {},
color_trie = nil,
color_name_minlen = nil,
color_name_maxlen = nil,
-- TODO: 2024-12-20 - Should these be configurable in settings opts?
color_name_settings = { lowercase = true, strip_digits = false },
tailwind_enabled = false,
}
@ -57,13 +55,13 @@ local function handle_names_custom(names_custom)
return
end
local extra_data = {}
local names = {}
if type(names_custom) == "table" then
extra_data = names_custom
names = names_custom
elseif type(names_custom) == "function" then
local status, result = pcall(names_custom)
if status and type(result) == "table" then
extra_data = result
names = result
else
vim.api.nvim_err_writeln(
"Error in names_custom function: " .. (result or "Invalid return value")
@ -73,10 +71,10 @@ local function handle_names_custom(names_custom)
end
-- Add additional characters found in names_custom keys
local additonal_chars = extract_non_alphanum_keys(extra_data)
names_cache.color_trie:additional_chars(additonal_chars)
local chars = extract_non_alphanum_keys(names)
names_cache.color_trie:additional_chars(chars)
for name, hex in pairs(extra_data) do
for name, hex in pairs(names) do
if type(hex) == "string" then
local normalized_hex = hex:gsub("^#", ""):gsub("%s", "")
if normalized_hex:match("^%x%x%x%x%x%x$") then

View file

@ -22,7 +22,7 @@ $g: $f;
$test3: $color3;
$test4: $color4;
$test6: $color6; // this should be absent and this comment shoudl be ignored $color6: black
$test6: $color6; // this should be absent and this comment should be ignored $color6: black
$test8: $color8;
$test9: $color9;
$test10: $color10;