aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorakianonymus <anonymus.aki@gmail.com>2023-02-22 18:43:01 +0530
committerAkshay <anonymus.aki@gmail.com>2023-02-26 11:02:44 +0530
commit7e1f3b2a040e37895cc6797e5fb6032f9165e5c9 (patch)
tree5961df8743282bc7a157afd5c68923daf8135cc7
parentfeat: Improve hsl parser (diff)
fix: Improve individual opts behaviour | #48
See https://github.com/NvChad/nvim-colorizer.lua/issues/48 css is true, names is true: Show names css is true, names is unset: Show names css is true, names is false: Don't show names
-rw-r--r--README.md26
-rw-r--r--doc/colorizer.txt33
-rw-r--r--doc/modules/colorizer.html33
-rw-r--r--lua/colorizer.lua83
-rw-r--r--lua/colorizer/buffer.lua3
-rw-r--r--lua/colorizer/matcher.lua16
-rw-r--r--lua/colorizer/sass.lua5
7 files changed, 185 insertions, 14 deletions
diff --git a/README.md b/README.md
index 2d1f368..9944770 100644
--- a/README.md
+++ b/README.md
@@ -162,6 +162,32 @@ 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. 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` 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`
+
+```lua
+require 'colorizer'.setup { user_default_options = { names = false, css = true } }
+```
+
+e.g: Here `names`, `RGB`, `RRGGBB`, `RRGGBBAA` is enabled but not `rgb_fn` and `hsl_fn`
+
+```lua
+require 'colorizer'.setup { user_default_options = { css_fn = false, css = true } }
+```
+
All the above examples can also be apply to buftypes. Also no buftypes trigger colorizer by default
Buftype value is fetched by `vim.bo.buftype`
diff --git a/doc/colorizer.txt b/doc/colorizer.txt
index 1727fc4..0dfbf63 100644
--- a/doc/colorizer.txt
+++ b/doc/colorizer.txt
@@ -192,6 +192,39 @@ clear_highlight_cache() *colorizer.clear_highlight_cache*
user_default_options *colorizer.user_default_options*
defaults options.
+ 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. 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` 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`
+
+>
+ require 'colorizer'.setup { user_default_options = { names = false, css =
+ true } }
+<
+
+ e.g: Here `names`, `RGB`, `RRGGBB`, `RRGGBBAA` is enabled but not `rgb_fn`
+ and `hsl_fn`
+
+>
+ require 'colorizer'.setup { user_default_options = { css_fn = false, css =
+ true } }
+<
+
>
user_default_options = {
RGB = true, -- #RGB hex codes
diff --git a/doc/modules/colorizer.html b/doc/modules/colorizer.html
index e9fcd62..1196e4d 100644
--- a/doc/modules/colorizer.html
+++ b/doc/modules/colorizer.html
@@ -385,7 +385,38 @@ Setup an autocmd which enables colorizing for the filetypes and options specifie
</dt>
<dd>
-<p>defaults options. </p>
+<p>defaults options.
+In <a href="../modules/colorizer.html#user_default_options">user_default_options</a>, there are 2 types of options</p>
+
+<ol>
+ <li><p>Individual options - <code>names</code>, <code>RGB</code>, <code>RRGGBB</code>, <code>RRGGBBAA</code>, <code>hsl_fn</code>, <code>rgb_fn</code> , <code>RRGGBBAA</code>, <code>AARRGGBB</code>, <code>tailwind</code>, <code>sass</code></p></li>
+ <li><p>Alias options - <code>css</code>, <code>css_fn</code></p></li>
+</ol>
+
+<p>If <code>css_fn</code> is true, then <code>hsl_fn</code>, <code>rgb_fn</code> becomes <code>true</code></p>
+
+<p>If <code>css</code> is true, then <code>names</code>, <code>RGB</code>, <code>RRGGBB</code>, <code>RRGGBBAA</code>, <code>hsl_fn</code>, <code>rgb_fn</code> becomes <code>true</code></p>
+
+<p>These options have a priority, Individual options have the highest priority, then alias options</p>
+
+<p>For alias, <code>css_fn</code> has more priority over <code>css</code></p>
+
+<p>e.g: Here <code>RGB</code>, <code>RRGGBB</code>, <code>RRGGBBAA</code>, <code>hsl_fn</code>, <code>rgb_fn</code> is enabled but not <code>names</code></p>
+
+
+<pre>
+ require 'colorizer'.setup { user_default_options = { names = false, css = true } }
+</pre>
+
+
+<p>e.g: Here <code>names</code>, <code>RGB</code>, <code>RRGGBB</code>, <code>RRGGBBAA</code> is enabled but not <code>rgb_fn</code> and <code>hsl_fn</code></p>
+
+
+<pre>
+ require 'colorizer'.setup { user_default_options = { css_fn = false, css = true } }
+</pre>
+
+
<pre>
user_default_options = {
diff --git a/lua/colorizer.lua b/lua/colorizer.lua
index 21822a9..8da9ce0 100644
--- a/lua/colorizer.lua
+++ b/lua/colorizer.lua
@@ -92,6 +92,32 @@ local BUFFER_OPTIONS = {}
local BUFFER_LOCAL = {}
---defaults options.
+--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. 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` 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`
+--
+--<pre>
+-- require 'colorizer'.setup { user_default_options = { names = false, css = true } }
+--</pre>
+--
+--e.g: Here `names`, `RGB`, `RRGGBB`, `RRGGBBAA` is enabled but not `rgb_fn` and `hsl_fn`
+--
+--<pre>
+-- require 'colorizer'.setup { user_default_options = { css_fn = false, css = true } }
+--</pre>
+--
--<pre>
-- user_default_options = {
-- RGB = true, -- #RGB hex codes
@@ -164,6 +190,53 @@ local function new_buffer_options(buf, typ)
return OPTIONS.file[value] or SETUP_SETTINGS.default_options
end
+--- Parse buffer Configuration and convert aliases to normal values
+---@param options table: options table
+---@return table
+local function parse_buffer_options(options)
+ local includes = {
+ ["css"] = { "names", "RGB", "RRGGBB", "RRGGBBAA", "hsl_fn", "rgb_fn" },
+ ["css_fn"] = { "hsl_fn", "rgb_fn" },
+ }
+ local css_includes = { "names", "RGB", "RRGGBB", "RRGGBBAA", "hsl_fn", "rgb_fn" }
+ local css_fn_includes = { "hsl_fn", "rgb_fn" }
+ local default_opts = USER_DEFAULT_OPTIONS
+
+ local function handle_alias(name, opts, d_opts)
+ if not includes[name] then
+ return
+ end
+ if opts == true or opts[name] == true then
+ for _, child in ipairs(includes[name]) do
+ d_opts[child] = true
+ end
+ elseif opts[name] == false then
+ for _, child in ipairs(includes[name]) do
+ d_opts[child] = false
+ end
+ end
+ end
+ -- https://github.com/NvChad/nvim-colorizer.lua/issues/48
+ handle_alias("css", options, default_opts)
+ handle_alias("css_fn", options, default_opts)
+
+ 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)
+ end
+ else
+ options.sass.parsers = {}
+ for child, _ in pairs(default_opts.sass.parsers) do
+ handle_alias(child, true, options.sass.parsers)
+ end
+ end
+ end
+
+ options = merge(default_opts, options)
+ return options
+end
+
--- Check if attached to a buffer.
---@param buf number|nil: A value of 0 implies the current buffer.
---@return number|nil: if attached to the buffer, false otherwise.
@@ -246,6 +319,8 @@ function colorizer.attach_to_buffer(buf, options, typ)
options = new_buffer_options(buf, typ)
end
+ options = parse_buffer_options(options)
+
if not buffer_utils.highlight_mode_names[options.mode] then
if options.mode ~= nil then
local mode = options.mode
@@ -353,7 +428,7 @@ end
function colorizer.setup(config)
if not vim.opt.termguicolors then
vim.schedule(function()
- vim.notify("Colorizer: Error: &termguicolors must be set", "Error")
+ vim.notify("Colorizer: Error: &termguicolors must be set", 4)
end)
return
end
@@ -369,7 +444,7 @@ function colorizer.setup(config)
SETUP_SETTINGS = {
exclusions = { buf = {}, file = {} },
all = { file = false, buf = false },
- default_options = merge(USER_DEFAULT_OPTIONS, user_default_options),
+ default_options = user_default_options,
}
BUFFER_OPTIONS, BUFFER_LOCAL = {}, {}
@@ -426,7 +501,7 @@ function colorizer.setup(config)
if type(k) == "string" then
value = k
if type(v) ~= "table" then
- vim.notify("colorizer: Invalid option type for " .. typ .. "type" .. value, "ErrorMsg")
+ vim.notify("colorizer: Invalid option type for " .. typ .. "type" .. value, 4)
else
options = merge(SETUP_SETTINGS.default_options, v)
end
@@ -453,7 +528,7 @@ function colorizer.setup(config)
end,
})
elseif tbl then
- vim.notify_once(string.format("colorizer: Invalid type for %stypes %s", typ, vim.inspect(tbl)), "ErrorMsg")
+ vim.notify_once(string.format("colorizer: Invalid type for %stypes %s", typ, vim.inspect(tbl)), 4)
end
end
diff --git a/lua/colorizer/buffer.lua b/lua/colorizer/buffer.lua
index 2035d52..36588af 100644
--- a/lua/colorizer/buffer.lua
+++ b/lua/colorizer/buffer.lua
@@ -142,7 +142,7 @@ function buffer.highlight(buf, ns, line_start, line_end, options, options_local)
-- only update sass varibles when text is changed
if options_local.__event ~= "WinScrolled" and options.sass and options.sass.enable then
table.insert(returns.detach.functions, sass_cleanup)
- sass_update_variables(buf, 0, -1, nil, make_matcher(options.sass.parsers or { css = true }), options, options_local)
+ sass_update_variables(buf, 0, -1, nil, make_matcher(options.sass.parsers), options, options_local)
end
local data = buffer.parse_lines(buf, lines, line_start, options) or {}
@@ -199,6 +199,7 @@ local function getrow(buf)
end
local a = api.nvim_buf_call(buf, function()
+ ---@diagnostic disable-next-line: redundant-return-value
return {
vim.fn.line "w0",
vim.fn.line "w$",
diff --git a/lua/colorizer/matcher.lua b/lua/colorizer/matcher.lua
index d55a673..21d7b4f 100644
--- a/lua/colorizer/matcher.lua
+++ b/lua/colorizer/matcher.lua
@@ -68,15 +68,19 @@ local MATCHER_CACHE = {}
---@param options table: options created in `colorizer.setup`
---@return function|boolean: function which will just parse the line for enabled parsers
function matcher.make(options)
- local enable_names = options.css or options.names
+ if not options then
+ return false
+ end
+
+ local enable_names = options.names
local enable_sass = options.sass and options.sass.enable
local enable_tailwind = options.tailwind
- local enable_RGB = options.css or options.RGB
- local enable_RRGGBB = options.css or options.RRGGBB
- local enable_RRGGBBAA = options.css or options.RRGGBBAA
+ local enable_RGB = options.RGB
+ local enable_RRGGBB = options.RRGGBB
+ local enable_RRGGBBAA = options.RRGGBBAA
local enable_AARRGGBB = options.AARRGGBB
- local enable_rgb = options.css or options.css_fns or options.rgb_fn
- local enable_hsl = options.css or options.css_fns or options.hsl_fn
+ local enable_rgb = options.rgb_fn
+ local enable_hsl = options.hsl_fn
local matcher_key = 0
+ (enable_names and 1 or 0)
diff --git a/lua/colorizer/sass.lua b/lua/colorizer/sass.lua
index 5f08137..02d5701 100644
--- a/lua/colorizer/sass.lua
+++ b/lua/colorizer/sass.lua
@@ -163,10 +163,10 @@ local function sass_parse_lines(buf, line_start, content, name)
if last_modified then
-- grab the full path
v = uv.fs_realpath(v)
- SASS[buf].CURRENT_IMPORTS[name][v] = true
+ SASS[buf].CURRENT_IMPORTS[name][v or ""] = true
if not SASS[buf].WATCH_IMPORTS[name][v] then
- SASS[buf].IMPORTS[name][v] = last_modified
+ SASS[buf].IMPORTS[name][v or ""] = last_modified
local c, ind = {}, 0
for l in io.lines(v) do
ind = ind + 1
@@ -177,6 +177,7 @@ local function sass_parse_lines(buf, line_start, content, name)
local function watch_callback()
local dimen = vim.api.nvim_buf_call(buf, function()
+ ---@diagnostic disable-next-line: redundant-return-value
return { vim.fn.line "w0", vim.fn.line "w$", vim.fn.line "$", vim.api.nvim_win_get_height(0) }
end)
-- todo: Improve this to only refresh highlight for visible lines