Skip to content

Commit

Permalink
Feat: Improve changing of highlights with on_highlight (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
5-pebbles authored Jun 2, 2024
1 parent a567a10 commit b0a748e
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 24 deletions.
63 changes: 46 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,16 @@ require 'lualine' .setup {
}
```

To get the palette in lua:

```lua
local palette = require 'nordic.colors'
```

# ⚙️ Configuration

Nordic will use the default values, unless `setup` is called. Below is the default configuration.

```lua
require 'nordic' .setup {
require('nordic').setup({
-- This callback can be used to override the colors used in the palette.
on_palette = function(palette) end,
-- This callback can be used to override highlights before they are applied.
on_highlight = function(highlights, palette) end,
-- Enable bold keywords.
bold_keywords = false,
-- Enable italic comments.
Expand All @@ -103,8 +99,6 @@ require 'nordic' .setup {
reduced_blue = true,
-- Swap the dark background with the normal one.
swap_backgrounds = false,
-- Override the styling of any highlight group.
override = {},
-- Cursorline options. Also includes visual/selection.
cursorline = {
-- Bold font in cursorline.
Expand Down Expand Up @@ -132,27 +126,62 @@ require 'nordic' .setup {
-- Enables dark background for treesitter-context window
dark_background = true,
}
}
})
```

An example of overriding the `TelescopePromptTitle` colors:
**Examples:**

<details>
<summary><b><code>on_palette</code></b></summary>
&nbsp;

An example of overriding colors in the base palette:
```lua
require('nordic').setup({
on_palette = function(palette)
palette.black0 = "#BF616A"
palette.green.base = palette.cyan.base
end,
})
```

</details>


<details>
<summary><b><code>on_highlight</code></b></summary>
&nbsp;

An example of overriding the `TelescopePromptTitle` colors:
```lua
local palette = require 'nordic.colors'
require 'nordic' .setup {
override = {
TelescopePromptTitle = {
require('nordic').setup({
on_highlight = function(highlights, palette)
highlights.TelescopePromptTitle = {
fg = palette.red.bright,
bg = palette.green.base,
italic = true,
underline = true,
sp = palette.yellow.dim,
undercurl = false
}
}
}
end,
})
```

And an example of disabling all italics:
```lua
require('nordic').setup({
on_highlight = function(highlights, _palette)
for _, highlight in pairs(highlights) do
highlight.italic = false
end
end
})
```

</details>


# 🗒️ Supported Plugins and Platforms

For the list of supported plugins, please take a look at [this file](https://github.com/AlexvZyl/nordic.nvim/blob/main/lua/nordic/groups/integrations.lua). For the list of supported platforms, please take a look at [this directory](https://github.com/AlexvZyl/nordic.nvim/tree/main/platforms).
Expand Down
5 changes: 4 additions & 1 deletion lua/nordic/colors/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local P = require 'nordic.colors.nordic'
local C = {}

function C.build_palette()
-- override all values from the base palette
-- Override all values from the base palette.
U.merge_inplace(C, P)

local options = require('nordic.config').options
Expand Down Expand Up @@ -96,4 +96,7 @@ function C.build_palette()
C.comment = C.gray4
end

-- Build the first palette.
C.build_palette()

return C
23 changes: 23 additions & 0 deletions lua/nordic/compatibility.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
local U = require("nordic.utils");

local function compatability(options)
-- All backwards compatibility
-- While an option is deprecated it should still work but be overridden by its replacement

-- Log level
local level = vim.log.levels.WARN
Expand All @@ -15,12 +18,32 @@ local function compatability(options)
level,
message_options
)

options.transparent = {
bg = options.transparent_bg,
float = options.transparent_bg,
}
end

-- override
if options.override ~= nil then
vim.notify_once(
'Nordic.nvim: config.override is deprecated, use config.on_highlight instead',
level,
message_options
)

local users_on_highlight = options.on_highlight;
-- Create a new on_highlight that will apply `override` and then the users `on_highlight`
options.on_highlight = function(highlights, palette)
U.merge_inplace(highlights, options.override)
-- This nil check is required because we have not been given default values yet
if users_on_highlight ~= nil then
users_on_highlight(highlights, palette)
end
end
end

return options
end

Expand Down
4 changes: 2 additions & 2 deletions lua/nordic/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ local M = {}
local defaults = {
-- This callback can be used to override the colors used in the palette.
on_palette = function(palette) end,
-- This callback can be used to override highlights before they are applied.
on_highlight = function(highlights, palette) end,
-- Enable bold keywords.
bold_keywords = false,
-- Enable italic comments.
Expand All @@ -20,8 +22,6 @@ local defaults = {
reduced_blue = true,
-- Swap the dark background with the normal one.
swap_backgrounds = false,
-- Override the styling of any highlight group.
override = {},
-- Cursorline options. Also includes visual/selection.
cursorline = {
-- Bold font in cursorline.
Expand Down
12 changes: 9 additions & 3 deletions lua/nordic/groups/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ local merge = require('nordic.utils').merge
local M = {}

function M.get_groups()
local groups =
merge(require('nordic.groups.native').get_groups(), require('nordic.groups.integrations').get_groups())
local native = require('nordic.groups.native').get_groups()
local integrations = require('nordic.groups.integrations').get_groups()
local groups = merge(native, integrations)

return merge(groups, require('nordic.config').options.override)
-- Apply on_highlight
local palette = require('nordic.colors')
local options = require("nordic.config").options
options.on_highlight(groups, palette)

return groups
end

function M.set_term_colors()
Expand Down
12 changes: 11 additions & 1 deletion lua/nordic/tests/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,17 @@ load(config)

config.on_palette = function(palette)
palette.black0 = '#000000'
return palette
end

config.on_highlight = function(highlights, palette)
highlights.TelescopePromptTitle = {
fg = palette.red.bright,
bg = palette.green.base,
italic = true,
underline = true,
sp = palette.yellow.dim,
undercurl = false
}
end

-- Flip all fields
Expand Down

0 comments on commit b0a748e

Please sign in to comment.