diff --git a/README.md b/README.md index 4a40772..779b901 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ require("one_monokai").setup({ | ------------- | -------------------------------- | -------------------------- | ---------------- | | `transparent` | Enables a transparent background | `boolean` | N/A | | `colors` | Custom color definitions | `table` | N/A | -| `themes` | Custom highlight groups | `function(colors): tables` | `:h nvim_set_hl` | +| `highlights` | Custom highlight groups | `function(colors): table` | `:h nvim_set_hl` | | `italics` | Enables italic | `boolean` | N/A | ### Default Configuration @@ -64,7 +64,7 @@ require("one_monokai").setup({ require("one_monokai").setup({ transparent = false, colors = {}, - themes = function(colors) + highlights = function(colors) return {} end, italics = true, @@ -73,10 +73,10 @@ require("one_monokai").setup({ ### Customization Examples -#### Available Colors and Themes +#### Available Colors and Highlights 1. [Colors](lua/one_monokai/colors.lua) -2. [Themes](lua/one_monokai/themes/groups.lua) +2. [Highlights](lua/one_monokai/highlights/groups.lua) The color module provides two methods, `darken` and `lighten`, to blend colors based on an alpha value (between 0 and 1). You can use them as follows: @@ -94,7 +94,7 @@ require("one_monokai").setup({ lmao = "#ffffff", -- Define a new color pink = "#ec6075", -- Override a default color }, - themes = function(colors) + highlights = function(colors) -- Customize highlight groups -- The key-value pairs are passed to "nvim_set_hl" return { diff --git a/lua/one_monokai/colors.lua b/lua/one_monokai/colors.lua index d502bb1..1296fc4 100644 --- a/lua/one_monokai/colors.lua +++ b/lua/one_monokai/colors.lua @@ -1,7 +1,6 @@ ----@class colors local colors = {} ----@type colors +---@class colors local defaults = { fg = "#abb2bf", bg = "#282c34", @@ -28,6 +27,7 @@ local defaults = { none = "NONE", } +---@type colors colors = vim.deepcopy(defaults) ---Convert hex value to rgb @@ -81,7 +81,7 @@ local function get_hex_value(name, value) }) if not type_ok then - logs.error.notify(err) + logs.notify.error(err) return defaults[name] end @@ -93,7 +93,7 @@ local function get_hex_value(name, value) local rgb = vim.api.nvim_get_color_by_name(value) if rgb == -1 then - logs.error.notify("colors(%s): %q is not a valid color", name, value) + logs.notify.error("colors(%s): %q is not a valid color", name, value) return defaults[name] end diff --git a/lua/one_monokai/config.lua b/lua/one_monokai/config.lua index d2ca6e9..b3e6fd9 100644 --- a/lua/one_monokai/config.lua +++ b/lua/one_monokai/config.lua @@ -3,27 +3,46 @@ local config = {} ---@class options ---@field transparent boolean #Whether to enable transparent background ---@field colors colors #Custom colors ----@field themes fun(colors:colors):groups #Custom highlight groups ----@field italics boolean #Whether to italicize some highlight groups +---@field highlights? fun(colors:colors):groups #Custom highlight groups +---@field italics boolean #Whether to apply italics to certain highlight groups local defaults = { transparent = false, colors = {}, - themes = function(_) - return {} - end, + ---@deprecated + themes = nil, + highlights = nil, italics = true, } -config.options = vim.deepcopy(defaults) +---@type options +local options = vim.deepcopy(defaults) ---Extend default with user's config ---@param opts options -function config:extend(opts) - self.options = vim.tbl_deep_extend("force", self.options, opts or {}) +function config.extend(opts) + if not opts or vim.tbl_isempty(opts) then + return + end + + local logs = require "one_monokai.logs" + + if opts.themes then + logs.notify.warning( + "config: %q option has been deprecated and will be removed soon. Please update your config to use %q instead.", + "themes", + "highlights" + ) + + opts.highlights = opts.themes + end + + options = vim.tbl_deep_extend("force", options, opts) end -return setmetatable(config, { - __index = function(_, key) - return config.options[key] +setmetatable(config, { + __index = function(_, k) + return options[k] end, }) + +return config diff --git a/lua/one_monokai/themes/groups.lua b/lua/one_monokai/highlights/groups.lua similarity index 100% rename from lua/one_monokai/themes/groups.lua rename to lua/one_monokai/highlights/groups.lua index 592a610..b43430d 100644 --- a/lua/one_monokai/themes/groups.lua +++ b/lua/one_monokai/highlights/groups.lua @@ -1,10 +1,9 @@ ----@class groups local groups = {} local colors = require "one_monokai.colors" local config = require "one_monokai.config" ----@type groups +---@class groups local defaults = { Boolean = { fg = colors.cyan }, Character = { fg = colors.yellow }, @@ -582,6 +581,7 @@ for _, semantic_group in ipairs(vim.fn.getcompletion("@lsp", "highlight")) do end end +---@type groups groups = vim.deepcopy(defaults) return groups diff --git a/lua/one_monokai/themes/init.lua b/lua/one_monokai/highlights/init.lua similarity index 55% rename from lua/one_monokai/themes/init.lua rename to lua/one_monokai/highlights/init.lua index d0f662a..92efa34 100644 --- a/lua/one_monokai/themes/init.lua +++ b/lua/one_monokai/highlights/init.lua @@ -1,7 +1,7 @@ -local themes = {} +local highlights = {} ---Set highlight groups ----@param groups groups +---@param groups groups Highlight groups local function set_highlight(groups) local logs = require "one_monokai.logs" local set_hl = vim.api.nvim_set_hl @@ -10,18 +10,22 @@ local function set_highlight(groups) local status_ok, err = pcall(set_hl, 0, name, attrs) if not status_ok then - logs.error.notify("themes(%s): %s", name, err) + logs.notify.error("highlights(%s): %s", name, err) end end end -function themes.load() +---Load all highlight groups +function highlights.load() local config = require "one_monokai.config" local colors = require "one_monokai.colors" - local default = require "one_monokai.themes.groups" + local default = require "one_monokai.highlights.groups" set_highlight(default) - set_highlight(config.themes(colors)) + + if config.highlights then + set_highlight(config.highlights(colors)) + end end -return themes +return highlights diff --git a/lua/one_monokai/init.lua b/lua/one_monokai/init.lua index df3cc04..8efb0f0 100644 --- a/lua/one_monokai/init.lua +++ b/lua/one_monokai/init.lua @@ -3,7 +3,7 @@ local M = {} function M.setup(opts) local set = vim.cmd local config = require "one_monokai.config" - local themes = require "one_monokai.themes" + local highlights = require "one_monokai.highlights" if vim.g.colors_name then set.hi "clear" @@ -17,8 +17,8 @@ function M.setup(opts) vim.o.termguicolors = true vim.g.colors_name = "one_monokai" - config:extend(opts) - themes.load() + config.extend(opts) + highlights.load() end return M diff --git a/lua/one_monokai/logs.lua b/lua/one_monokai/logs.lua index e090e70..545046d 100644 --- a/lua/one_monokai/logs.lua +++ b/lua/one_monokai/logs.lua @@ -1,17 +1,26 @@ +---@class logs local logs = {} -logs.error = { - ---Display error message. - --- - ---NOTE: This function receives params that will be passed to `string.format`. - ---@param s string - ---@param ... any - notify = function(s, ...) - local message = string.format(s, ...) +---Display a notification to the user +---@param log_level string +---@param s string +local function notify(log_level, s, ...) + local message = string.format(s, ...) - vim.notify_once(message, vim.log.levels.ERROR, { - title = "One Monokai", - }) + vim.notify_once(message, log_level, { + title = "One Monokai", + }) +end + +---@class notify +---@field error fun(s:string, ...) #Show error message +---@field warning fun(s: string, ...) #Show warning message +logs.notify = { + error = function(s, ...) + notify(vim.log.levels.ERROR, s, ...) + end, + warning = function(s, ...) + notify(vim.log.levels.WARNING, s, ...) end, } diff --git a/tests/spec/config_spec.lua b/tests/spec/config_spec.lua index b5f5be7..6243494 100644 --- a/tests/spec/config_spec.lua +++ b/tests/spec/config_spec.lua @@ -13,7 +13,7 @@ describe("Override config", function() pink = "#61afef", lmao = "#dedeff", }, - themes = function(c) + highlights = function(c) return { Normal = { fg = c.lmao, italic = true }, } @@ -28,7 +28,7 @@ describe("Override config", function() it("should change the default config", function() assert.is_false(config.transparent) assert.are.same(expected.colors, config.colors) - assert.are.same(expected.themes(colors), config.themes(colors)) + assert.are.same(expected.highlights(colors), config.highlights(colors)) end) it("should change default colors", function() @@ -37,7 +37,7 @@ describe("Override config", function() assert.equal(expected.colors.lmao, colors.lmao) end) - it("should change default themes", function() + it("should change default highlights", function() local hl = vim.api.nvim_get_hl(0, { name = "Normal" }) assert.equal(expected.colors.lmao, ("#%06x"):format(hl.fg)) @@ -58,7 +58,7 @@ describe("Override wrong config", function() colors = { aqua = true, }, - themes = function() + highlights = function() return { Normal = { fg = "lmao", italic = true }, } @@ -78,7 +78,7 @@ describe("Override wrong config", function() end) it("should fallback to default highlights", function() - local default = require "one_monokai.themes.groups" + local default = require "one_monokai.highlights.groups" local hl = vim.api.nvim_get_hl(0, { name = "Normal" }) assert.equal(default.Normal.fg, ("#%06x"):format(hl.fg))