Skip to content

Commit

Permalink
feat(names): Configure lower/camel/upper case of names highlights (#123)
Browse files Browse the repository at this point in the history
* ref: initialize caches in do block upon module load

* feat: adds casing, strip_digit customization to names config

* doc: updates documentation for names_opts
  • Loading branch information
catgoose authored Dec 27, 2024
1 parent 3437b6b commit 24f305f
Show file tree
Hide file tree
Showing 10 changed files with 275 additions and 179 deletions.
29 changes: 20 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,17 @@ library to do custom highlighting themselves.
require("colorizer").setup({
filetypes = { "*" },
user_default_options = {
names = true, -- "Name" codes like Blue or blue
-- Expects a table of color name to rgb value pairs. # is optional
names = true, -- "Name" codes like Blue or red. Added from `vim.api.nvim_get_color_map()`
names_opts = { -- options for mutating/filtering names.
lowercase = true, -- name:lower(), highlight `blue` and `red`
camelcase = true, -- name, highlight `Blue` and `Red`
uppercase = false, -- name:upper(), highlight `BLUE` and `RED`
strip_digits = false, -- ignore names with digits,
-- highlight `blue` and `red`, but not `blue3` and `red4`
},
-- Expects a table of color name to #RRGGBB value pairs. # is optional
-- Example: { cool = "#107dac", ["notcool"] = "ee9240" }
-- Set to false|nil to disable
-- Set to false|nil to disable, for example when setting filetype options
names_custom = false, -- Custom names to be highlighted: table|function|false|nil
RGB = true, -- #RGB hex codes
RGBA = true, -- #RGBA hex codes
Expand All @@ -135,7 +142,8 @@ library to do custom highlighting themselves.
AARRGGBB = false, -- 0xAARRGGBB hex codes
rgb_fn = false, -- CSS rgb() and rgba() functions
hsl_fn = false, -- CSS hsl() and hsla() functions
css = false, -- Enable all CSS features: rgb_fn, hsl_fn, names, RGB, RRGGBB
css = false, -- Enable all CSS *features*:
-- names, RGB, RGBA, RRGGBB, RRGGBBAA, AARRGGBB, rgb_fn, hsl_fn
css_fn = false, -- Enable all CSS *functions*: rgb_fn, hsl_fn
-- Highlighting mode. 'background'|'foreground'|'virtualtext'
mode = "background", -- Set the display mode
Expand Down Expand Up @@ -246,22 +254,24 @@ require("colorizer").setup({

In `user_default_options`, there are 2 types of options

1. Individual options - `names`, `RGB`, `RRGGBB`, `RRGGBBAA`, `hsl_fn`, `rgb_fn`,
`RRGGBBAA`, `AARRGGBB`, `tailwind`, `sass`
1. Individual options - `names`, `names_opts`, `names_custom`, `RGB`, `RGBA`,
`RRGGBB`, `RRGGBBAA`, `hsl_fn`, `rgb_fn`, `RRGGBBAA`, `AARRGGBB`, `tailwind`,
`sass`

1. Alias options - `css`, `css_fn`

If `css_fn` is true, then `hsl_fn`, `rgb_fn` becomes `true`

If `css` is true, then `names`, `RGB`, `RRGGBB`, `RRGGBBAA`, `hsl_fn`, `rgb_fn`
If `css` is true, then `names`, `RGB`, `RGBA`, `RRGGBB`, `RRGGBBAA`, `hsl_fn`, `rgb_fn`
becomes `true`

These options have a priority, Individual options have the highest priority,
then alias options

For alias, `css_fn` has more priority over `css`

e.g: Here `RGB`, `RRGGBB`, `RRGGBBAA`, `hsl_fn`, `rgb_fn` is enabled but not `names`
e.g: Here `RGB`, `RGBA`, `RRGGBB`, `RRGGBBAA`, `AARRGGBB`, `hsl_fn`, `rgb_fn` is
enabled but not `names`

```lua
require("colorizer").setup({
Expand All @@ -272,7 +282,8 @@ require("colorizer").setup({
})
```

e.g: Here `names`, `RGB`, `RRGGBB`, `RRGGBBAA` is enabled but not `rgb_fn` and `hsl_fn`
e.g: Here `names`, `RGB`, `RGBA`, `RRGGBB`, `RRGGBBAA`, `AARRGGBB` is enabled but
not `rgb_fn` and `hsl_fn`

```lua
require("colorizer").setup({
Expand Down
75 changes: 46 additions & 29 deletions doc/colorizer.txt
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,8 @@ Provides configuration options and utilities for setting up colorizer.
LUA API *colorizer.config-lua-api*

Functions: ~
|reset_cache| - Reset the cache for buffer options.

|set_bo_value| - Set options for a specific buffer or file type.

|apply_alias_options| - Parse and apply alias options to the user options.
Expand All @@ -472,6 +474,14 @@ Tables: ~
|opts| - Configuration options for the `setup` function.


reset_cache() *colorizer.config.reset_cache*
Reset the cache for buffer options.

Called from colorizer.setup




set_bo_value({bo_type}, {value}, {options}) *colorizer.config.set_bo_value*
Set options for a specific buffer or file type.

Expand Down Expand Up @@ -559,12 +569,15 @@ user_default_options *colorizer.config.user_default_options*


Fields: ~
{RGB} - boolean: Enables `#RGB` hex codes.
{RRGGBB} - boolean: Enables `#RRGGBB` hex codes.
{names} - boolean: Enables named colors (e.g., "Blue").
{names_custom} - table|function|nil: Extra color name to RGB value
mappings
{names_opts} - table: Names options for customizing casing, digit
stripping, etc
{names_custom} - table|function|false|nil: Custom color name to RGB
value mappings
should return a table of color names to RGB value pairs
{RGB} - boolean: Enables `#RGB` hex codes.
{RGBA} - boolean: Enables `#RGBA` hex codes.
{RRGGBB} - boolean: Enables `#RRGGBB` hex codes.
{RRGGBBAA} - boolean: Enables `#RRGGBBAA` hex codes.
{AARRGGBB} - boolean: Enables `0xAARRGGBB` hex codes.
{rgb_fn} - boolean: Enables CSS `rgb()` and `rgba()` functions.
Expand Down Expand Up @@ -606,10 +619,14 @@ opts *colorizer.config.opts*
{filetypes} - table A list of file types where colorizer should be
enabled. Use `"*"` for all file types.
{user_default_options} - table Default options for color handling.
- `names` (boolean): Enables named color codes like `"Blue"`.
- `names_opts` (table): Names options for customizing casing, digit
stripping, etc
- `names_custom` (table|function|false|nil): Custom color name to RGB value
mappings
- `RGB` (boolean): Enables support for `#RGB` hex codes.
- `RGBA` (boolean): Enables support for `#RGBA` hex codes.
- `RRGGBB` (boolean): Enables support for `#RRGGBB` hex codes.
- `names` (boolean): Enables named color codes like `"Blue"`.
- `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.
Expand Down Expand Up @@ -658,34 +675,34 @@ Manages matching and parsing of color patterns in buffers.
LUA API *colorizer.matcher-lua-api*

Functions: ~
|reset_cache| - Reset matcher cache
Called from colorizer.setup

|make| - Parse the given options and return a function with enabled parsers.

|reset_cache| - Reset the cache of matchers
Called from colorizer.setup

reset_cache() *colorizer.matcher.reset_cache*
Reset matcher cache
Called from colorizer.setup



make({options}) *colorizer.matcher.make*
make({opts}) *colorizer.matcher.make*
Parse the given options and return a function with enabled parsers.

if no parsers enabled then return false
Do not try make the function again if it is present in the cache


Parameters: ~
{options} - table: options created in `colorizer.setup`
{opts} - table: options created in `colorizer.setup`

returns:~
function or boolean: function which will just parse the line for enabled
parsers



reset_cache() *colorizer.matcher.reset_cache*
Reset the cache of matchers
Called from colorizer.setup



==============================================================================
HSL *colorizer.parser.hsl-introduction*

Expand Down Expand Up @@ -743,29 +760,29 @@ text.
LUA API *colorizer.parser.names-lua-api*

Functions: ~
|parser| - Parses a line to identify color names.
|reset_cache| - Reset the color names cache.

|reset_cache| - Resets the color names cache.
|parser| - Parses a line to identify color names.


parser({line}, {i}, {opts}) *colorizer.parser.names.parser*
Parses a line to identify color names.
reset_cache() *colorizer.parser.names.reset_cache*
Reset the color names cache.

Parameters: ~
{line} - string The text line to parse.
{i} - number The index to start parsing from.
{opts} - table Parsing options.
Called from colorizer.setup

returns:~
number or nil, string or nil Length of match and hex value if found.



reset_cache() *colorizer.parser.names.reset_cache*
Resets the color names cache.
parser({line}, {i}, {opts}) *colorizer.parser.names.parser*
Parses a line to identify color names.

Called from colorizer.setup
Parameters: ~
{line} - string: The text line to parse.
{i} - number: The index to start parsing from.
{opts} - table: Parsing options.

returns:~
number or nil, string or nil: Length of match and hex value if found.



Expand Down
45 changes: 36 additions & 9 deletions doc/modules/colorizer.config.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ <h1>Module <code>colorizer.config</code></h1>

<h2><a href="#Functions">Functions</a></h2>
<table class="function_list">
<tr>
<td class="name" nowrap><a href="#reset_cache">reset_cache ()</a></td>
<td class="summary">Reset the cache for buffer options.</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>
Expand Down Expand Up @@ -112,6 +116,21 @@ <h2><a href="#Tables">Tables</a></h2>
<h2 class="section-header "><a name="Functions"></a>Functions</h2>

<dl class="function">
<dt>
<a name = "reset_cache"></a>
<strong>reset_cache ()</strong>
</dt>
<dd>
Reset the cache for buffer options.
Called from colorizer.setup







</dd>
<dt>
<a name = "set_bo_value"></a>
<strong>set_bo_value (bo_type, value, options)</strong>
Expand Down Expand Up @@ -267,19 +286,25 @@ <h2 class="section-header "><a name="Tables"></a>Tables</h2>

<h3>Fields:</h3>
<ul>
<li><span class="parameter">RGB</span>
boolean: Enables `#RGB` hex codes.
</li>
<li><span class="parameter">RRGGBB</span>
boolean: Enables `#RRGGBB` hex codes.
</li>
<li><span class="parameter">names</span>
boolean: Enables named colors (e.g., "Blue").
</li>
<li><span class="parameter">names_opts</span>
table: Names options for customizing casing, digit stripping, etc
</li>
<li><span class="parameter">names_custom</span>
table|function|nil: Extra color name to RGB value mappings
table|function|false|nil: Custom color name to RGB value mappings
should return a table of color names to RGB value pairs
</li>
<li><span class="parameter">RGB</span>
boolean: Enables `#RGB` hex codes.
</li>
<li><span class="parameter">RGBA</span>
boolean: Enables `#RGBA` hex codes.
</li>
<li><span class="parameter">RRGGBB</span>
boolean: Enables `#RRGGBB` hex codes.
</li>
<li><span class="parameter">RRGGBBAA</span>
boolean: Enables `#RRGGBBAA` hex codes.
</li>
Expand Down Expand Up @@ -379,10 +404,12 @@ <h3>Fields:</h3>
</li>
<li><span class="parameter">user_default_options</span>
table Default options for color handling.
- `names` (boolean): Enables named color codes like `"Blue"`.
- `names_opts` (table): Names options for customizing casing, digit stripping, etc
- `names_custom` (table|function|false|nil): Custom color name to RGB value mappings
- `RGB` (boolean): Enables support for `#RGB` hex codes.
- `RGBA` (boolean): Enables support for `#RGBA` hex codes.
- `RRGGBB` (boolean): Enables support for `#RRGGBB` hex codes.
- `names` (boolean): Enables named color codes like `"Blue"`.
- `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.
Expand Down
44 changes: 22 additions & 22 deletions doc/modules/colorizer.matcher.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ <h1>Module <code>colorizer.matcher</code></h1>
<h2><a href="#Functions">Functions</a></h2>
<table class="function_list">
<tr>
<td class="name" nowrap><a href="#make">make (options)</a></td>
<td class="summary">Parse the given options and return a function with enabled parsers.</td>
<td class="name" nowrap><a href="#reset_cache">reset_cache ()</a></td>
<td class="summary">Reset matcher cache
Called from colorizer.setup</td>
</tr>
<tr>
<td class="name" nowrap><a href="#reset_cache">reset_cache ()</a></td>
<td class="summary">Reset the cache of matchers
Called from colorizer.setup</td>
<td class="name" nowrap><a href="#make">make (opts)</a></td>
<td class="summary">Parse the given options and return a function with enabled parsers.</td>
</tr>
</table>

Expand All @@ -88,9 +88,24 @@ <h2><a href="#Functions">Functions</a></h2>
<h2 class="section-header "><a name="Functions"></a>Functions</h2>

<dl class="function">
<dt>
<a name = "reset_cache"></a>
<strong>reset_cache ()</strong>
</dt>
<dd>
Reset matcher cache
Called from colorizer.setup







</dd>
<dt>
<a name = "make"></a>
<strong>make (options)</strong>
<strong>make (opts)</strong>
</dt>
<dd>
Parse the given options and return a function with enabled parsers.
Expand All @@ -100,7 +115,7 @@ <h2 class="section-header "><a name="Functions"></a>Functions</h2>

<h3>Parameters:</h3>
<ul>
<li><span class="parameter">options</span>
<li><span class="parameter">opts</span>
table: options created in `colorizer.setup`
</li>
</ul>
Expand All @@ -114,21 +129,6 @@ <h3>Returns:</h3>



</dd>
<dt>
<a name = "reset_cache"></a>
<strong>reset_cache ()</strong>
</dt>
<dd>
Reset the cache of matchers
Called from colorizer.setup







</dd>
</dl>

Expand Down
Loading

0 comments on commit 24f305f

Please sign in to comment.