Skip to content

Commit 24f305f

Browse files
authored
feat(names): Configure lower/camel/upper case of names highlights (#123)
* ref: initialize caches in do block upon module load * feat: adds casing, strip_digit customization to names config * doc: updates documentation for names_opts
1 parent 3437b6b commit 24f305f

10 files changed

+275
-179
lines changed

README.md

+20-9
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,17 @@ library to do custom highlighting themselves.
123123
require("colorizer").setup({
124124
filetypes = { "*" },
125125
user_default_options = {
126-
names = true, -- "Name" codes like Blue or blue
127-
-- Expects a table of color name to rgb value pairs. # is optional
126+
names = true, -- "Name" codes like Blue or red. Added from `vim.api.nvim_get_color_map()`
127+
names_opts = { -- options for mutating/filtering names.
128+
lowercase = true, -- name:lower(), highlight `blue` and `red`
129+
camelcase = true, -- name, highlight `Blue` and `Red`
130+
uppercase = false, -- name:upper(), highlight `BLUE` and `RED`
131+
strip_digits = false, -- ignore names with digits,
132+
-- highlight `blue` and `red`, but not `blue3` and `red4`
133+
},
134+
-- Expects a table of color name to #RRGGBB value pairs. # is optional
128135
-- Example: { cool = "#107dac", ["notcool"] = "ee9240" }
129-
-- Set to false|nil to disable
136+
-- Set to false|nil to disable, for example when setting filetype options
130137
names_custom = false, -- Custom names to be highlighted: table|function|false|nil
131138
RGB = true, -- #RGB hex codes
132139
RGBA = true, -- #RGBA hex codes
@@ -135,7 +142,8 @@ library to do custom highlighting themselves.
135142
AARRGGBB = false, -- 0xAARRGGBB hex codes
136143
rgb_fn = false, -- CSS rgb() and rgba() functions
137144
hsl_fn = false, -- CSS hsl() and hsla() functions
138-
css = false, -- Enable all CSS features: rgb_fn, hsl_fn, names, RGB, RRGGBB
145+
css = false, -- Enable all CSS *features*:
146+
-- names, RGB, RGBA, RRGGBB, RRGGBBAA, AARRGGBB, rgb_fn, hsl_fn
139147
css_fn = false, -- Enable all CSS *functions*: rgb_fn, hsl_fn
140148
-- Highlighting mode. 'background'|'foreground'|'virtualtext'
141149
mode = "background", -- Set the display mode
@@ -246,22 +254,24 @@ require("colorizer").setup({
246254

247255
In `user_default_options`, there are 2 types of options
248256

249-
1. Individual options - `names`, `RGB`, `RRGGBB`, `RRGGBBAA`, `hsl_fn`, `rgb_fn`,
250-
`RRGGBBAA`, `AARRGGBB`, `tailwind`, `sass`
257+
1. Individual options - `names`, `names_opts`, `names_custom`, `RGB`, `RGBA`,
258+
`RRGGBB`, `RRGGBBAA`, `hsl_fn`, `rgb_fn`, `RRGGBBAA`, `AARRGGBB`, `tailwind`,
259+
`sass`
251260

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

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

256-
If `css` is true, then `names`, `RGB`, `RRGGBB`, `RRGGBBAA`, `hsl_fn`, `rgb_fn`
265+
If `css` is true, then `names`, `RGB`, `RGBA`, `RRGGBB`, `RRGGBBAA`, `hsl_fn`, `rgb_fn`
257266
becomes `true`
258267

259268
These options have a priority, Individual options have the highest priority,
260269
then alias options
261270

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

264-
e.g: Here `RGB`, `RRGGBB`, `RRGGBBAA`, `hsl_fn`, `rgb_fn` is enabled but not `names`
273+
e.g: Here `RGB`, `RGBA`, `RRGGBB`, `RRGGBBAA`, `AARRGGBB`, `hsl_fn`, `rgb_fn` is
274+
enabled but not `names`
265275

266276
```lua
267277
require("colorizer").setup({
@@ -272,7 +282,8 @@ require("colorizer").setup({
272282
})
273283
```
274284

275-
e.g: Here `names`, `RGB`, `RRGGBB`, `RRGGBBAA` is enabled but not `rgb_fn` and `hsl_fn`
285+
e.g: Here `names`, `RGB`, `RGBA`, `RRGGBB`, `RRGGBBAA`, `AARRGGBB` is enabled but
286+
not `rgb_fn` and `hsl_fn`
276287

277288
```lua
278289
require("colorizer").setup({

doc/colorizer.txt

+46-29
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,8 @@ Provides configuration options and utilities for setting up colorizer.
453453
LUA API *colorizer.config-lua-api*
454454

455455
Functions: ~
456+
|reset_cache| - Reset the cache for buffer options.
457+
456458
|set_bo_value| - Set options for a specific buffer or file type.
457459

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

474476

477+
reset_cache() *colorizer.config.reset_cache*
478+
Reset the cache for buffer options.
479+
480+
Called from colorizer.setup
481+
482+
483+
484+
475485
set_bo_value({bo_type}, {value}, {options}) *colorizer.config.set_bo_value*
476486
Set options for a specific buffer or file type.
477487

@@ -559,12 +569,15 @@ user_default_options *colorizer.config.user_default_options*
559569

560570

561571
Fields: ~
562-
{RGB} - boolean: Enables `#RGB` hex codes.
563-
{RRGGBB} - boolean: Enables `#RRGGBB` hex codes.
564572
{names} - boolean: Enables named colors (e.g., "Blue").
565-
{names_custom} - table|function|nil: Extra color name to RGB value
566-
mappings
573+
{names_opts} - table: Names options for customizing casing, digit
574+
stripping, etc
575+
{names_custom} - table|function|false|nil: Custom color name to RGB
576+
value mappings
567577
should return a table of color names to RGB value pairs
578+
{RGB} - boolean: Enables `#RGB` hex codes.
579+
{RGBA} - boolean: Enables `#RGBA` hex codes.
580+
{RRGGBB} - boolean: Enables `#RRGGBB` hex codes.
568581
{RRGGBBAA} - boolean: Enables `#RRGGBBAA` hex codes.
569582
{AARRGGBB} - boolean: Enables `0xAARRGGBB` hex codes.
570583
{rgb_fn} - boolean: Enables CSS `rgb()` and `rgba()` functions.
@@ -606,10 +619,14 @@ opts *colorizer.config.opts*
606619
{filetypes} - table A list of file types where colorizer should be
607620
enabled. Use `"*"` for all file types.
608621
{user_default_options} - table Default options for color handling.
622+
- `names` (boolean): Enables named color codes like `"Blue"`.
623+
- `names_opts` (table): Names options for customizing casing, digit
624+
stripping, etc
625+
- `names_custom` (table|function|false|nil): Custom color name to RGB value
626+
mappings
609627
- `RGB` (boolean): Enables support for `#RGB` hex codes.
628+
- `RGBA` (boolean): Enables support for `#RGBA` hex codes.
610629
- `RRGGBB` (boolean): Enables support for `#RRGGBB` hex codes.
611-
- `names` (boolean): Enables named color codes like `"Blue"`.
612-
- `names_custom` (table|function|nil): Extra color name to RGB value mappings
613630
- `RRGGBBAA` (boolean): Enables support for `#RRGGBBAA` hex codes.
614631
- `AARRGGBB` (boolean): Enables support for `0xAARRGGBB` hex codes.
615632
- `rgb_fn` (boolean): Enables CSS `rgb()` and `rgba()` functions.
@@ -658,34 +675,34 @@ Manages matching and parsing of color patterns in buffers.
658675
LUA API *colorizer.matcher-lua-api*
659676

660677
Functions: ~
678+
|reset_cache| - Reset matcher cache
679+
Called from colorizer.setup
680+
661681
|make| - Parse the given options and return a function with enabled parsers.
662682

663-
|reset_cache| - Reset the cache of matchers
664-
Called from colorizer.setup
683+
684+
reset_cache() *colorizer.matcher.reset_cache*
685+
Reset matcher cache
686+
Called from colorizer.setup
687+
665688

666689

667-
make({options}) *colorizer.matcher.make*
690+
make({opts}) *colorizer.matcher.make*
668691
Parse the given options and return a function with enabled parsers.
669692

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

673696

674697
Parameters: ~
675-
{options} - table: options created in `colorizer.setup`
698+
{opts} - table: options created in `colorizer.setup`
676699

677700
returns:~
678701
function or boolean: function which will just parse the line for enabled
679702
parsers
680703

681704

682705

683-
reset_cache() *colorizer.matcher.reset_cache*
684-
Reset the cache of matchers
685-
Called from colorizer.setup
686-
687-
688-
689706
==============================================================================
690707
HSL *colorizer.parser.hsl-introduction*
691708

@@ -743,29 +760,29 @@ text.
743760
LUA API *colorizer.parser.names-lua-api*
744761

745762
Functions: ~
746-
|parser| - Parses a line to identify color names.
763+
|reset_cache| - Reset the color names cache.
747764

748-
|reset_cache| - Resets the color names cache.
765+
|parser| - Parses a line to identify color names.
749766

750767

751-
parser({line}, {i}, {opts}) *colorizer.parser.names.parser*
752-
Parses a line to identify color names.
768+
reset_cache() *colorizer.parser.names.reset_cache*
769+
Reset the color names cache.
753770

754-
Parameters: ~
755-
{line} - string The text line to parse.
756-
{i} - number The index to start parsing from.
757-
{opts} - table Parsing options.
771+
Called from colorizer.setup
758772

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

762774

763775

764-
reset_cache() *colorizer.parser.names.reset_cache*
765-
Resets the color names cache.
776+
parser({line}, {i}, {opts}) *colorizer.parser.names.parser*
777+
Parses a line to identify color names.
766778

767-
Called from colorizer.setup
779+
Parameters: ~
780+
{line} - string: The text line to parse.
781+
{i} - number: The index to start parsing from.
782+
{opts} - table: Parsing options.
768783

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

770787

771788

doc/modules/colorizer.config.html

+36-9
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ <h1>Module <code>colorizer.config</code></h1>
6868

6969
<h2><a href="#Functions">Functions</a></h2>
7070
<table class="function_list">
71+
<tr>
72+
<td class="name" nowrap><a href="#reset_cache">reset_cache ()</a></td>
73+
<td class="summary">Reset the cache for buffer options.</td>
74+
</tr>
7175
<tr>
7276
<td class="name" nowrap><a href="#set_bo_value">set_bo_value (bo_type, value, options)</a></td>
7377
<td class="summary">Set options for a specific buffer or file type.</td>
@@ -112,6 +116,21 @@ <h2><a href="#Tables">Tables</a></h2>
112116
<h2 class="section-header "><a name="Functions"></a>Functions</h2>
113117

114118
<dl class="function">
119+
<dt>
120+
<a name = "reset_cache"></a>
121+
<strong>reset_cache ()</strong>
122+
</dt>
123+
<dd>
124+
Reset the cache for buffer options.
125+
Called from colorizer.setup
126+
127+
128+
129+
130+
131+
132+
133+
</dd>
115134
<dt>
116135
<a name = "set_bo_value"></a>
117136
<strong>set_bo_value (bo_type, value, options)</strong>
@@ -267,19 +286,25 @@ <h2 class="section-header "><a name="Tables"></a>Tables</h2>
267286

268287
<h3>Fields:</h3>
269288
<ul>
270-
<li><span class="parameter">RGB</span>
271-
boolean: Enables `#RGB` hex codes.
272-
</li>
273-
<li><span class="parameter">RRGGBB</span>
274-
boolean: Enables `#RRGGBB` hex codes.
275-
</li>
276289
<li><span class="parameter">names</span>
277290
boolean: Enables named colors (e.g., "Blue").
278291
</li>
292+
<li><span class="parameter">names_opts</span>
293+
table: Names options for customizing casing, digit stripping, etc
294+
</li>
279295
<li><span class="parameter">names_custom</span>
280-
table|function|nil: Extra color name to RGB value mappings
296+
table|function|false|nil: Custom color name to RGB value mappings
281297
should return a table of color names to RGB value pairs
282298
</li>
299+
<li><span class="parameter">RGB</span>
300+
boolean: Enables `#RGB` hex codes.
301+
</li>
302+
<li><span class="parameter">RGBA</span>
303+
boolean: Enables `#RGBA` hex codes.
304+
</li>
305+
<li><span class="parameter">RRGGBB</span>
306+
boolean: Enables `#RRGGBB` hex codes.
307+
</li>
283308
<li><span class="parameter">RRGGBBAA</span>
284309
boolean: Enables `#RRGGBBAA` hex codes.
285310
</li>
@@ -379,10 +404,12 @@ <h3>Fields:</h3>
379404
</li>
380405
<li><span class="parameter">user_default_options</span>
381406
table Default options for color handling.
407+
- `names` (boolean): Enables named color codes like `"Blue"`.
408+
- `names_opts` (table): Names options for customizing casing, digit stripping, etc
409+
- `names_custom` (table|function|false|nil): Custom color name to RGB value mappings
382410
- `RGB` (boolean): Enables support for `#RGB` hex codes.
411+
- `RGBA` (boolean): Enables support for `#RGBA` hex codes.
383412
- `RRGGBB` (boolean): Enables support for `#RRGGBB` hex codes.
384-
- `names` (boolean): Enables named color codes like `"Blue"`.
385-
- `names_custom` (table|function|nil): Extra color name to RGB value mappings
386413
- `RRGGBBAA` (boolean): Enables support for `#RRGGBBAA` hex codes.
387414
- `AARRGGBB` (boolean): Enables support for `0xAARRGGBB` hex codes.
388415
- `rgb_fn` (boolean): Enables CSS `rgb()` and `rgba()` functions.

doc/modules/colorizer.matcher.html

+22-22
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ <h1>Module <code>colorizer.matcher</code></h1>
7171
<h2><a href="#Functions">Functions</a></h2>
7272
<table class="function_list">
7373
<tr>
74-
<td class="name" nowrap><a href="#make">make (options)</a></td>
75-
<td class="summary">Parse the given options and return a function with enabled parsers.</td>
74+
<td class="name" nowrap><a href="#reset_cache">reset_cache ()</a></td>
75+
<td class="summary">Reset matcher cache
76+
Called from colorizer.setup</td>
7677
</tr>
7778
<tr>
78-
<td class="name" nowrap><a href="#reset_cache">reset_cache ()</a></td>
79-
<td class="summary">Reset the cache of matchers
80-
Called from colorizer.setup</td>
79+
<td class="name" nowrap><a href="#make">make (opts)</a></td>
80+
<td class="summary">Parse the given options and return a function with enabled parsers.</td>
8181
</tr>
8282
</table>
8383

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

9090
<dl class="function">
91+
<dt>
92+
<a name = "reset_cache"></a>
93+
<strong>reset_cache ()</strong>
94+
</dt>
95+
<dd>
96+
Reset matcher cache
97+
Called from colorizer.setup
98+
99+
100+
101+
102+
103+
104+
105+
</dd>
91106
<dt>
92107
<a name = "make"></a>
93-
<strong>make (options)</strong>
108+
<strong>make (opts)</strong>
94109
</dt>
95110
<dd>
96111
Parse the given options and return a function with enabled parsers.
@@ -100,7 +115,7 @@ <h2 class="section-header "><a name="Functions"></a>Functions</h2>
100115

101116
<h3>Parameters:</h3>
102117
<ul>
103-
<li><span class="parameter">options</span>
118+
<li><span class="parameter">opts</span>
104119
table: options created in `colorizer.setup`
105120
</li>
106121
</ul>
@@ -114,21 +129,6 @@ <h3>Returns:</h3>
114129

115130

116131

117-
</dd>
118-
<dt>
119-
<a name = "reset_cache"></a>
120-
<strong>reset_cache ()</strong>
121-
</dt>
122-
<dd>
123-
Reset the cache of matchers
124-
Called from colorizer.setup
125-
126-
127-
128-
129-
130-
131-
132132
</dd>
133133
</dl>
134134

0 commit comments

Comments
 (0)