Skip to content

Commit

Permalink
feat(config): added styling options
Browse files Browse the repository at this point in the history
* chore: updated code comments
* feat(config): added options to disable text styling
* feat(config): added bold_functions and italic_comments presets
  • Loading branch information
ficcdaf committed Jan 19, 2025
1 parent e1af711 commit 5f68162
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 29 deletions.
40 changes: 39 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ provided settings will be merged with the defaults.
```Lua
-- default settings
{
-- toggle text style options
---@type table<StyleName, boolean>
style = {},
-- toggle group specific settings
style_presets = {
bold_functions = false,
italic_comments = false,
},
--- override palette colors
---@type Palette
---@field [ColorName] HexCode
Expand Down Expand Up @@ -187,6 +195,36 @@ provided settings will be merged with the defaults.

</details>

### Style

You can disable _all_ uses of a certain style with the following setting:

```Lua
opts = {
style = {
bold = false,
italic = false,
-- etc...
},
}
```

### Style Presets

The following presets are available. They are off by default, and you may choose
to enable them in your configuration:

```Lua
opts = {
style_presets = {
bold_functions = true,
italic_comments = true,
},
}


```

### Palette Override

<details>
Expand All @@ -201,7 +239,7 @@ names, please see [colors.lua](./lua/ashen/colors.lua).
Please see the following example:

```Lua
{
opts = {
colors = {
background = "#000000",
red_ember = "#933737"
Expand Down
2 changes: 0 additions & 2 deletions lua/ashen/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ M.load = function()
end
vim.g.colors_name = "ashen"
vim.o.termguicolors = true
-- palette must be overridden before theme is loaded for the first time
-- could this be done in a better way?
palette_override()
local theme = require("ashen.theme")
theme.load()
Expand Down
8 changes: 8 additions & 0 deletions lua/ashen/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ local M = {}
---Settings configuration.
---@class Options table
M.opts = {
-- toggle text style options
---@type table<string, boolean>
style = {},
-- toggle group specific settings
style_presets = {
bold_functions = false,
italic_comments = false,
},
-- override palette
---@type Palette
colors = {},
Expand Down
29 changes: 29 additions & 0 deletions lua/ashen/theme.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ M.map = {
AshenG11 = { "g_11" },
AshenG12 = { "g_12" },
Title = { "red_ember", nil, { bold = true } },
-- Normal = { "g_3", "background" },
Normal = { "g_3", "background" },
ModeMsg = { "g_4" },
CurSearch = { "background", "orange_glow", { bold = true } },
Expand Down Expand Up @@ -211,6 +212,27 @@ M.transparent_bg = {
"LineNr",
}

-- ISSUE: for some reason, the merging only works properly
-- if the fg is set explicitly... will need to fix this later!
local style_presets = {
functions = {
["Function"] = { fg = "g_3", bold = true },
["@function"] = { fg = "g_3", bold = true },
["@function.builtin"] = { fg = "g_3", bold = true },
["@function.call"] = { fg = "g_3", bold = true },
["@function.macro"] = { fg = "g_3", bold = true },
["@lsp.type.function"] = { fg = "g_3", bold = true },
["@method"] = { fg = "g_3", bold = true },
["@method.call"] = { fg = "g_3", bold = true },
["@method.builtin"] = { fg = "g_3", bold = true },
["@lsp.type.method"] = { fg = "g_3", bold = true },
},
comments = {
["@comment"] = { fg = "g_6", italic = true },
["Comment"] = { fg = "g_6", italic = true },
},
}

M.load = function()
local util = require("ashen.util")
local opts = require("ashen.state").opts
Expand All @@ -220,6 +242,13 @@ M.load = function()
M.map[name] = util.remove_bg(M.map[name])
end
end
if opts.style_presets.bold_functions then
opts.hl.merge_override = vim.tbl_deep_extend("force", opts.hl.merge_override, style_presets.functions)
end
if opts.style_presets.italic_comments then
opts.hl.merge_override = vim.tbl_deep_extend("force", opts.hl.merge_override, style_presets.comments)
end

util.map_override(M.map, opts)

-- set theme highlights
Expand Down
73 changes: 47 additions & 26 deletions lua/ashen/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ M.normalize_hl = function(spec)
end

---Recursively calls "callback" on all
---values in given table.
---values in given table. Traverses nested tables.
---@param tbl table
---@param callback fun(k: any, v: any): any
M.deep_traverse = function(tbl, callback)
Expand All @@ -60,23 +60,25 @@ M.is_hex = function(str)
end
end

-- TODO: Add function that applies `opts.groups`
-- overrides to the groups directly in the mapping
-- (not per `util.hl` call)

-- TODO: refactor into general purpose override
-- to enforce `opts.style`

---Function traverses highlight specs and converts
---color names into hex codes.
---@param spec HighlightSpec|HighlightNormalized
M.hexify = function(spec)
---Function traverses highlight specs and
---enforces various HL rules.
---@param spec HighlightNormalized
M.enforce_hl_rules = function(spec)
local c = require("ashen.colors")
M.deep_traverse(spec, function(_, v)
if type(v) == "string" and not M.is_hex(v) then
local t = c[v]
if type(t) == "string" then
return t
local opts = require("ashen.state").opts
M.deep_traverse(spec, function(k, v)
-- check if it's a style parameter set to false
-- in user opts
local val_type = type(v)
if val_type == "boolean" and opts.style[k] == false then
return false
end
-- check if string needs to be converted to hex
if val_type == "string" and not M.is_hex(v) then
local hex = c[v]
if type(hex) == "string" then
return hex
end
end
return v
Expand All @@ -98,7 +100,9 @@ M.hl = function(name, spec)
if not M.is_norm(spec) then
spec = M.normalize_hl(spec)
end
M.hexify(spec)
---spec will always be normalized at this point
---@cast spec HighlightNormalized
M.enforce_hl_rules(spec)

-- check if transparency is enabled
-- this is too costly to do on each hl, though
Expand Down Expand Up @@ -191,22 +195,39 @@ M.is_in = function(list, value)
return false
end

AshenD = {}
AshenE = {}
Norm = {}

local function merge_map(targ, new)
for k, v in pairs(new or {}) do
if targ[k] == nil then
targ[k] = v
else
AshenD[k] = v
-- AshenD[k] = vim.tbl_deep_extend("force", M.normalize_hl(targ[k]), M.normalize_hl(v))
if not M.is_norm(v) then
v = M.normalize_hl(v)
end
if not M.is_norm(targ[k]) then
targ[k] = M.normalize_hl(targ[k])
end
-- targ[k] = vim.tbl_deep_extend("force", targ[k], v)
local temp = vim.tbl_deep_extend("force", targ[k], v)
targ[k] = denormalize(temp)
AshenE[k] = targ[k]
end
end
end

---@param map HighlightMap
---@param opts Options
M.map_override = function(map, opts)
if not map then
return
end
-- TODO: Let users provide palette color names instead of Hex codes
-- and check for it automatically
if opts.hl.merge_override and opts.hl.merge_override ~= {} then
for k, v in pairs(opts.hl.merge_override or {}) do
if map[k] == nil then
map[k] = v
else
map[k] = vim.tbl_deep_extend("force", map[k], v)
end
end
merge_map(map, opts.hl.merge_override)
end
if opts.hl.force_override and opts.hl.force_override ~= {} then
for k, v in pairs(opts.hl.force_override or {}) do
Expand Down

0 comments on commit 5f68162

Please sign in to comment.