Skip to content

Commit d369af6

Browse files
authored
refactor: rename themes options to highlights (#102)
1 parent 75c0b13 commit d369af6

File tree

8 files changed

+80
-48
lines changed

8 files changed

+80
-48
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ require("one_monokai").setup({
5555
| ------------- | -------------------------------- | -------------------------- | ---------------- |
5656
| `transparent` | Enables a transparent background | `boolean` | N/A |
5757
| `colors` | Custom color definitions | `table<string, string>` | N/A |
58-
| `themes` | Custom highlight groups | `function(colors): tables` | `:h nvim_set_hl` |
58+
| `highlights` | Custom highlight groups | `function(colors): table` | `:h nvim_set_hl` |
5959
| `italics` | Enables italic | `boolean` | N/A |
6060

6161
### Default Configuration
@@ -64,7 +64,7 @@ require("one_monokai").setup({
6464
require("one_monokai").setup({
6565
transparent = false,
6666
colors = {},
67-
themes = function(colors)
67+
highlights = function(colors)
6868
return {}
6969
end,
7070
italics = true,
@@ -73,10 +73,10 @@ require("one_monokai").setup({
7373

7474
### Customization Examples
7575

76-
#### Available Colors and Themes
76+
#### Available Colors and Highlights
7777

7878
1. [Colors](lua/one_monokai/colors.lua)
79-
2. [Themes](lua/one_monokai/themes/groups.lua)
79+
2. [Highlights](lua/one_monokai/highlights/groups.lua)
8080

8181
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:
8282

@@ -94,7 +94,7 @@ require("one_monokai").setup({
9494
lmao = "#ffffff", -- Define a new color
9595
pink = "#ec6075", -- Override a default color
9696
},
97-
themes = function(colors)
97+
highlights = function(colors)
9898
-- Customize highlight groups
9999
-- The key-value pairs are passed to "nvim_set_hl"
100100
return {

lua/one_monokai/colors.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
---@class colors
21
local colors = {}
32

4-
---@type colors
3+
---@class colors
54
local defaults = {
65
fg = "#abb2bf",
76
bg = "#282c34",
@@ -28,6 +27,7 @@ local defaults = {
2827
none = "NONE",
2928
}
3029

30+
---@type colors
3131
colors = vim.deepcopy(defaults)
3232

3333
---Convert hex value to rgb
@@ -81,7 +81,7 @@ local function get_hex_value(name, value)
8181
})
8282

8383
if not type_ok then
84-
logs.error.notify(err)
84+
logs.notify.error(err)
8585

8686
return defaults[name]
8787
end
@@ -93,7 +93,7 @@ local function get_hex_value(name, value)
9393
local rgb = vim.api.nvim_get_color_by_name(value)
9494

9595
if rgb == -1 then
96-
logs.error.notify("colors(%s): %q is not a valid color", name, value)
96+
logs.notify.error("colors(%s): %q is not a valid color", name, value)
9797

9898
return defaults[name]
9999
end

lua/one_monokai/config.lua

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,46 @@ local config = {}
33
---@class options
44
---@field transparent boolean #Whether to enable transparent background
55
---@field colors colors #Custom colors
6-
---@field themes fun(colors:colors):groups #Custom highlight groups
7-
---@field italics boolean #Whether to italicize some highlight groups
6+
---@field highlights? fun(colors:colors):groups #Custom highlight groups
7+
---@field italics boolean #Whether to apply italics to certain highlight groups
88
local defaults = {
99
transparent = false,
1010
colors = {},
11-
themes = function(_)
12-
return {}
13-
end,
11+
---@deprecated
12+
themes = nil,
13+
highlights = nil,
1414
italics = true,
1515
}
1616

17-
config.options = vim.deepcopy(defaults)
17+
---@type options
18+
local options = vim.deepcopy(defaults)
1819

1920
---Extend default with user's config
2021
---@param opts options
21-
function config:extend(opts)
22-
self.options = vim.tbl_deep_extend("force", self.options, opts or {})
22+
function config.extend(opts)
23+
if not opts or vim.tbl_isempty(opts) then
24+
return
25+
end
26+
27+
local logs = require "one_monokai.logs"
28+
29+
if opts.themes then
30+
logs.notify.warning(
31+
"config: %q option has been deprecated and will be removed soon. Please update your config to use %q instead.",
32+
"themes",
33+
"highlights"
34+
)
35+
36+
opts.highlights = opts.themes
37+
end
38+
39+
options = vim.tbl_deep_extend("force", options, opts)
2340
end
2441

25-
return setmetatable(config, {
26-
__index = function(_, key)
27-
return config.options[key]
42+
setmetatable(config, {
43+
__index = function(_, k)
44+
return options[k]
2845
end,
2946
})
47+
48+
return config

lua/one_monokai/themes/groups.lua renamed to lua/one_monokai/highlights/groups.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
---@class groups
21
local groups = {}
32

43
local colors = require "one_monokai.colors"
54
local config = require "one_monokai.config"
65

7-
---@type groups
6+
---@class groups
87
local defaults = {
98
Boolean = { fg = colors.cyan },
109
Character = { fg = colors.yellow },
@@ -582,6 +581,7 @@ for _, semantic_group in ipairs(vim.fn.getcompletion("@lsp", "highlight")) do
582581
end
583582
end
584583

584+
---@type groups
585585
groups = vim.deepcopy(defaults)
586586

587587
return groups
Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
local themes = {}
1+
local highlights = {}
22

33
---Set highlight groups
4-
---@param groups groups
4+
---@param groups groups Highlight groups
55
local function set_highlight(groups)
66
local logs = require "one_monokai.logs"
77
local set_hl = vim.api.nvim_set_hl
@@ -10,18 +10,22 @@ local function set_highlight(groups)
1010
local status_ok, err = pcall(set_hl, 0, name, attrs)
1111

1212
if not status_ok then
13-
logs.error.notify("themes(%s): %s", name, err)
13+
logs.notify.error("highlights(%s): %s", name, err)
1414
end
1515
end
1616
end
1717

18-
function themes.load()
18+
---Load all highlight groups
19+
function highlights.load()
1920
local config = require "one_monokai.config"
2021
local colors = require "one_monokai.colors"
21-
local default = require "one_monokai.themes.groups"
22+
local default = require "one_monokai.highlights.groups"
2223

2324
set_highlight(default)
24-
set_highlight(config.themes(colors))
25+
26+
if config.highlights then
27+
set_highlight(config.highlights(colors))
28+
end
2529
end
2630

27-
return themes
31+
return highlights

lua/one_monokai/init.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ local M = {}
33
function M.setup(opts)
44
local set = vim.cmd
55
local config = require "one_monokai.config"
6-
local themes = require "one_monokai.themes"
6+
local highlights = require "one_monokai.highlights"
77

88
if vim.g.colors_name then
99
set.hi "clear"
@@ -17,8 +17,8 @@ function M.setup(opts)
1717
vim.o.termguicolors = true
1818
vim.g.colors_name = "one_monokai"
1919

20-
config:extend(opts)
21-
themes.load()
20+
config.extend(opts)
21+
highlights.load()
2222
end
2323

2424
return M

lua/one_monokai/logs.lua

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
1+
---@class logs
12
local logs = {}
23

3-
logs.error = {
4-
---Display error message.
5-
---
6-
---NOTE: This function receives params that will be passed to `string.format`.
7-
---@param s string
8-
---@param ... any
9-
notify = function(s, ...)
10-
local message = string.format(s, ...)
4+
---Display a notification to the user
5+
---@param log_level string
6+
---@param s string
7+
local function notify(log_level, s, ...)
8+
local message = string.format(s, ...)
119

12-
vim.notify_once(message, vim.log.levels.ERROR, {
13-
title = "One Monokai",
14-
})
10+
vim.notify_once(message, log_level, {
11+
title = "One Monokai",
12+
})
13+
end
14+
15+
---@class notify
16+
---@field error fun(s:string, ...) #Show error message
17+
---@field warning fun(s: string, ...) #Show warning message
18+
logs.notify = {
19+
error = function(s, ...)
20+
notify(vim.log.levels.ERROR, s, ...)
21+
end,
22+
warning = function(s, ...)
23+
notify(vim.log.levels.WARNING, s, ...)
1524
end,
1625
}
1726

tests/spec/config_spec.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe("Override config", function()
1313
pink = "#61afef",
1414
lmao = "#dedeff",
1515
},
16-
themes = function(c)
16+
highlights = function(c)
1717
return {
1818
Normal = { fg = c.lmao, italic = true },
1919
}
@@ -28,7 +28,7 @@ describe("Override config", function()
2828
it("should change the default config", function()
2929
assert.is_false(config.transparent)
3030
assert.are.same(expected.colors, config.colors)
31-
assert.are.same(expected.themes(colors), config.themes(colors))
31+
assert.are.same(expected.highlights(colors), config.highlights(colors))
3232
end)
3333

3434
it("should change default colors", function()
@@ -37,7 +37,7 @@ describe("Override config", function()
3737
assert.equal(expected.colors.lmao, colors.lmao)
3838
end)
3939

40-
it("should change default themes", function()
40+
it("should change default highlights", function()
4141
local hl = vim.api.nvim_get_hl(0, { name = "Normal" })
4242

4343
assert.equal(expected.colors.lmao, ("#%06x"):format(hl.fg))
@@ -58,7 +58,7 @@ describe("Override wrong config", function()
5858
colors = {
5959
aqua = true,
6060
},
61-
themes = function()
61+
highlights = function()
6262
return {
6363
Normal = { fg = "lmao", italic = true },
6464
}
@@ -78,7 +78,7 @@ describe("Override wrong config", function()
7878
end)
7979

8080
it("should fallback to default highlights", function()
81-
local default = require "one_monokai.themes.groups"
81+
local default = require "one_monokai.highlights.groups"
8282
local hl = vim.api.nvim_get_hl(0, { name = "Normal" })
8383

8484
assert.equal(default.Normal.fg, ("#%06x"):format(hl.fg))

0 commit comments

Comments
 (0)