Skip to content

Commit

Permalink
fix!: #195 do not extend hl groups by default
Browse files Browse the repository at this point in the history
Instead, give users the option to add `extend = true`
  • Loading branch information
olimorris committed Jun 20, 2023
1 parent 89b919f commit 0b53cb1
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 86 deletions.
98 changes: 54 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,14 @@ require("onedarkpro").setup({

### Configuring highlight groups

The [editor](https://github.com/olimorris/onedarkpro.nvim/tree/main/lua/onedarkpro/highlights/editor.lua), [syntax](https://github.com/olimorris/onedarkpro.nvim/tree/main/lua/onedarkpro/highlights/syntax.lua), [filetype](https://github.com/olimorris/onedarkpro.nvim/tree/main/lua/onedarkpro/highlights/filetypes) and [plugin](https://github.com/olimorris/onedarkpro.nvim/tree/main/lua/onedarkpro/highlights/plugins) files use a large array of highlight groups. There are three ways to customize or _override_ them:
The [editor](https://github.com/olimorris/onedarkpro.nvim/tree/main/lua/onedarkpro/highlights/editor.lua), [syntax](https://github.com/olimorris/onedarkpro.nvim/tree/main/lua/onedarkpro/highlights/syntax.lua), [filetype](https://github.com/olimorris/onedarkpro.nvim/tree/main/lua/onedarkpro/highlights/filetypes) and [plugin](https://github.com/olimorris/onedarkpro.nvim/tree/main/lua/onedarkpro/highlights/plugins) files use a large array of highlight groups. Some examples of how you can customize or _override_ them:

1. Using specific hex colors:
1. Using specific hex colors and styles:

```lua
require("onedarkpro").setup({
highlights = {
Comment = { fg = "#FF0000", bg = "#FFFF00" }
Comment = { fg = "#FF0000", bg = "#FFFF00", italic = true }
}
})
```
Expand All @@ -300,7 +300,7 @@ require("onedarkpro").setup({
```lua
require("onedarkpro").setup({
highlights = {
Comment = { fg = "${my_new_red}" bg = "${yellow}" }
Comment = { fg = "${my_new_red}" bg = "${yellow}", italic = true }
}
})
```
Expand All @@ -315,6 +315,18 @@ require("onedarkpro").setup({
})
```

Existing highlight groups can also be extended:

```lua
require("onedarkpro").setup({
highlights = {
Comment = { underline = true, extend = true }
}
})
```

In the example above, an underline style has been applied to the existing `Comment` highlight group.

#### Creating highlight groups

You can also create your own highlight groups:
Expand Down Expand Up @@ -371,6 +383,44 @@ require("onedarkpro").setup({
})
```

### Configuring styles

> **Note**: For a list of available styles, please refer to the [Neovim documentation](https://neovim.io/doc/user/api.html#nvim_set_hl())
Styles can be applied to highlight groups:

```lua
require("onedarkpro").setup({
highlights = {
Comment = { italic = true },
Directory = { bold = true },
ErrorMsg = { italic = true, bold = true }
}
})
```

Within the theme, collections of highlight groups have been grouped together into `styles`. For users who use monospaced fonts with nice italics, this can go someway to enhancing the aesthetic of a theme with minimal effort. These styles may be configured as shown in the example below:

```lua
require("onedarkpro").setup({
styles = {
types = "NONE",
methods = "NONE",
numbers = "NONE",
strings = "NONE",
comments = "italic",
keywords = "bold,italic",
constants = "NONE",
functions = "italic",
operators = "NONE",
variables = "NONE",
parameters = "NONE",
conditionals = "italic",
virtual_text = "NONE",
}
})
```

### Configuring filetype highlighting

> **Note**: Please see the [Contributing](https://github.com/olimorris/onedarkpro.nvim/blob/main/CONTRIBUTING.md) guide if you would like add support for new filetypes.
Expand Down Expand Up @@ -496,46 +546,6 @@ require("onedarkpro").setup({
})
```

### Configuring styles

> **Note**: See the [Neovim help](https://neovim.io/doc/user/api.html#nvim_set_hl()) for a full list of styles.
Within the theme, collections of highlight groups have been grouped together into `styles`. For users who use monospaced fonts with nice italics, this can go someway to enhancing the aesthetic of a theme. These styles may be configured as shown in the example below:

```lua
require("onedarkpro").setup({
styles = {
types = "NONE",
methods = "NONE",
numbers = "NONE",
strings = "NONE",
comments = "italic",
keywords = "bold,italic",
constants = "NONE",
functions = "italic",
operators = "NONE",
variables = "NONE",
parameters = "NONE",
conditionals = "italic",
virtual_text = "NONE",
}
})
```

Styles can also be applied manually to highlight groups:

```lua
require("onedarkpro").setup({
highlights = {
Comment = { italic = true },
Directory = { bold = true },
ErrorMsg = { italic = true, bold = true }
}
})
```

> **Note**: For a list of available styles, please refer to the [Neovim documentation](https://neovim.io/doc/user/api.html#nvim_set_hl())
### Configuring options

**Cursorline**
Expand Down
20 changes: 15 additions & 5 deletions doc/onedarkpro.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -297,16 +297,16 @@ filetype
<https://github.com/olimorris/onedarkpro.nvim/tree/main/lua/onedarkpro/highlights/filetypes>
and plugin
<https://github.com/olimorris/onedarkpro.nvim/tree/main/lua/onedarkpro/highlights/plugins>
files use a large array of highlight groups. There are three ways to customize
or _override_ them:
files use a large array of highlight groups. Some examples of how you can
customize or _override_ them:


1. Using specific hex colors:
1. Using specific hex colors and styles:

>lua
require("onedarkpro").setup({
highlights = {
Comment = { fg = "FF0000", bg = "FFFF00" }
Comment = { fg = "FF0000", bg = "FFFF00", italic = true }
}
})
<
Expand All @@ -317,7 +317,7 @@ or _override_ them:
>lua
require("onedarkpro").setup({
highlights = {
Comment = { fg = "${my_new_red}" bg = "${yellow}" }
Comment = { fg = "${my_new_red}" bg = "${yellow}", italic = true }
}
})
<
Expand All @@ -333,6 +333,16 @@ or _override_ them:
})
<

You may also wish to extend existing highlight groups:

>lua
require("onedarkpro").setup({
highlights = {
Comment = { underline = true, extend = true }
}
})
<


CREATING HIGHLIGHT GROUPS

Expand Down
9 changes: 5 additions & 4 deletions lua/onedarkpro/lib/compile.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,18 @@ end
---@param name string the highlight group name
---@param values table the highlight group values
---@param theme table the theme
---@param custom? boolean whether the highlight group is custom
---@param opts? table
---@return string
local function highlight(name, values, theme, custom)
local function highlight(name, values, theme, opts)
if values.link then return string.format([[set_hl(0, "%s", { link = "%s" })]], name, values.link) end
if next(values) == nil then return string.format([[set_hl(0, "%s", {})]], name) end

local val = parse_style(values)
val.bg = resolve_value(values.bg, theme)
val.fg = resolve_value(values.fg, theme)

if custom then
if opts and opts.custom and opts.extend then
val.extend = nil
return string.format(
[[set_hl(0, "%s", vim.tbl_extend("force", get_hl(0, { name = "%s" }), %s))]],
name,
Expand Down Expand Up @@ -125,7 +126,7 @@ vim.o.background = "%s"]],
table.insert(lines, "\n-- Custom highlight groups\n")
if type(custom_groups) == "table" and not vim.tbl_isempty(custom_groups) then
for name, values in pairs(custom_groups) do
table.insert(lines, highlight(name, values, theme, true))
table.insert(lines, highlight(name, values, theme, { custom = true, extend = values.extend }))
end
end

Expand Down
71 changes: 38 additions & 33 deletions tests/config_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,35 @@ describe("Using the theme", function()
assert.equals("Normal xxx guifg=#abb2bf guibg=#282c34", output)
end)

it("it should apply styles", function()
local output = vim.api.nvim_get_hl(0, { name = "Identifier" })
assert.equals(true, output.bold)
end)

it("it should be able to create blends", function()
local output = vim.api.nvim_get_hl(0, { name = "Constant" })
assert.equals(hex(output.blend), "#000064")
end)

-- Overwrite colors

it("it should create new colors", function()
local output = vim.api.nvim_get_hl(0, { name = "Statement" })
assert.equals("#ff00ff", hex(output.fg))
end)

it("it should apply styles", function()
local output = vim.api.nvim_get_hl(0, { name = "Identifier" })
assert.equals(true, output.bold)
it("it should be able to overwrite existing colors", function()
local output = vim.api.nvim_get_hl(0, { name = "Label" })
assert.equals("#d55fde", hex(output.fg))
end)

it("it should be able to overwrite generated colors", function()
local output = vim.api.nvim_get_hl(0, { name = "DiffAdd" })
assert.equals("#ff0000", hex(output.bg))
end)

-- Custom highlights

it("it should apply styles via custom highlights", function()
local output = vim.api.nvim_get_hl(0, { name = "Directory" })
assert.equals(true, output.bold)
Expand All @@ -55,55 +74,31 @@ describe("Using the theme", function()
assert.equals(true, output.reverse)
end)

it("it should apply styles via custom highlights without losing the fg color", function()
it("it should be able to extend highlights and apply styles", function()
local output = vim.api.nvim_get_hl(0, { name = "Title" })

assert.equals(true, output.underline)
assert.equals("#89ca78", hex(output.fg))
end)

if util.has_nvim_08 then
it("it should apply options", function()
local output = vim.api.nvim_get_hl(0, { name = "CursorLine" })
assert.equals("#2e323a", hex(output.bg))
end)

it("it should not apply options that are false", function()
local output = vim.api.nvim_get_hl(0, { name = "VertSplit" })
assert.equals("#282c34", hex(output.bg))
end)
end

it("it should be able to overwrite existing colors", function()
local output = vim.api.nvim_get_hl(0, { name = "Label" })
assert.equals("#d55fde", hex(output.fg))
end)

it("it should be able to overwrite generated colors", function()
local output = vim.api.nvim_get_hl(0, { name = "DiffAdd" })
assert.equals("#ff0000", hex(output.bg))
end)

it("it should be able to overwrite existing hlgroups", function()
local output = vim.api.nvim_get_hl(0, { name = "Repeat" })
assert.equals("#61afef", hex(output.fg))
local output = vim.api.nvim_get_hl(0, { name = "@keyword" })
assert.equals("#d55fde", hex(output.fg))
assert.equals(nil, output.link)
end)

it("it should be able to create custom hlgroups", function()
local output = vim.api.nvim_get_hl(0, { name = "TestHighlightGroup" })
assert.equals("#e06c75", hex(output.fg))
end)

it("it should be able to create blends", function()
local output = vim.api.nvim_get_hl(0, { name = "Constant" })
assert.equals(hex(output.blend), "#000064")
end)

it("it should be able to link to other highlight groups", function()
local output = vim.api.nvim_exec("hi TestHighlightGroup2", true)
assert.equals("TestHighlightGroup2 xxx links to Statement", output)
end)

-- Others

it("it should be able to use bg color as a conditional", function()
local output = vim.api.nvim_get_hl(0, { name = "ConditionalByBackground" })
assert.equals(hex(output.fg), "#ff0000")
Expand Down Expand Up @@ -162,6 +157,16 @@ describe("Using the theme", function()
assert.equals(vim.g.terminal_color_0, "#282c34")
end)

it("it should apply options", function()
local output = vim.api.nvim_get_hl(0, { name = "CursorLine" })
assert.equals("#2e333c", hex(output.bg))
end)

it("it should not apply options that are false", function()
local output = vim.api.nvim_get_hl(0, { name = "VertSplit" })
assert.equals("#282c34", hex(output.bg))
end)

it("it changes the theme when the background changes", function()
assert.equals("onedark_vivid", vim.g.colors_name)

Expand Down
4 changes: 4 additions & 0 deletions tests/config_spec.vim
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ onedarkpro.setup({
Directory = {
style = "bold",
},
["@keyword"] = {
fg = "${purple}"
},
Repeat = {
fg = "${blue}"
},
Expand All @@ -60,6 +63,7 @@ onedarkpro.setup({
},
Title = {
underline = true,
extend = true
}
},
})
Expand Down

0 comments on commit 0b53cb1

Please sign in to comment.