diff options
author | akianonymus <anonymus.aki@gmail.com> | 2023-02-22 18:43:01 +0530 |
---|---|---|
committer | Akshay <anonymus.aki@gmail.com> | 2023-02-26 11:02:44 +0530 |
commit | 7e1f3b2a040e37895cc6797e5fb6032f9165e5c9 (patch) | |
tree | 5961df8743282bc7a157afd5c68923daf8135cc7 /lua | |
parent | feat: 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
Diffstat (limited to 'lua')
-rw-r--r-- | lua/colorizer.lua | 83 | ||||
-rw-r--r-- | lua/colorizer/buffer.lua | 3 | ||||
-rw-r--r-- | lua/colorizer/matcher.lua | 16 | ||||
-rw-r--r-- | lua/colorizer/sass.lua | 5 |
4 files changed, 94 insertions, 13 deletions
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 |