diff --git a/README.md b/README.md index 18724791..9a8c2743 100644 --- a/README.md +++ b/README.md @@ -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 } } }) ``` @@ -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 } } }) ``` @@ -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: @@ -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. @@ -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** diff --git a/doc/onedarkpro.nvim.txt b/doc/onedarkpro.nvim.txt index 454b73f8..193e09f1 100644 --- a/doc/onedarkpro.nvim.txt +++ b/doc/onedarkpro.nvim.txt @@ -297,16 +297,16 @@ filetype and plugin -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 } } }) < @@ -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 } } }) < @@ -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 diff --git a/lua/onedarkpro/lib/compile.lua b/lua/onedarkpro/lib/compile.lua index 3801a923..8a83f9ff 100644 --- a/lua/onedarkpro/lib/compile.lua +++ b/lua/onedarkpro/lib/compile.lua @@ -56,9 +56,9 @@ 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 @@ -66,7 +66,8 @@ local function highlight(name, values, theme, custom) 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, @@ -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 diff --git a/tests/config_spec.lua b/tests/config_spec.lua index d074411a..5aaddc61 100644 --- a/tests/config_spec.lua +++ b/tests/config_spec.lua @@ -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) @@ -55,38 +74,17 @@ 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() @@ -94,16 +92,13 @@ describe("Using the theme", function() 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") @@ -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) diff --git a/tests/config_spec.vim b/tests/config_spec.vim index 75eef4ea..980fc702 100644 --- a/tests/config_spec.vim +++ b/tests/config_spec.vim @@ -41,6 +41,9 @@ onedarkpro.setup({ Directory = { style = "bold", }, + ["@keyword"] = { + fg = "${purple}" + }, Repeat = { fg = "${blue}" }, @@ -60,6 +63,7 @@ onedarkpro.setup({ }, Title = { underline = true, + extend = true } }, })