Skip to content

Commit

Permalink
feat: ✨ custom highlight groups by filetype (#28)
Browse files Browse the repository at this point in the history
* feat ✨ toggle light/dark versions with bg #5

* chore: update README.md

* fix: speed up load time

* chore: update README.md

* feat: ✨ add alacritty and kitty themes #11

* refactor: rename transparent option to transparency

* refactor: rename highlight_cursorline option to cursorline

* feat: add filetype highlight group customization

* fix: 🔨 #23 plugins always set to true

* chore: speed up buffer navigation

This doesn't make a noticable difference on any of my buffers but is a logical inclusion

* chore: update README.md

* feat: on theme reload apply filetype hlgroups

* fix: urls to onedarkpro in README.md

* chore: fix black color in onedark

* fix: filetype highlights do not work with floating windows

* chore: add disclaimer about telescope highlighting
  • Loading branch information
olimorris committed Feb 15, 2022
1 parent 579f0a5 commit 43bfe7d
Show file tree
Hide file tree
Showing 4 changed files with 260 additions and 32 deletions.
129 changes: 105 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<i>
Dark and light themes for Neovim 0.5 and above, written in Lua<br>
<b>Treesitter enabled / Fully customisable / Many popular plugins supported</b><br>
Inspired by VS Code's <a href="https://marketplace.visualstudio.com/items?itemName=zhuangtongfa.Material-theme">One Dark Pro</a>
Inspired by VS Code's <a href="https://binaryify.github.io/OneDark-Pro">One Dark Pro</a>
</i>
</p>

Expand All @@ -30,6 +30,7 @@
- [Configuring styles](#configuring-styles)
- [Configuring colors](#configuring-colors)
- [Configuring highlight groups](#configuring-highlight-groups)
- [Configuring filetype highlight groups](#configuring-filetype-highlight-groups)
- [Configuring options](#configuring-options)
- [Extras](#gift-extras)
- [Terminal themes](#terminal-themes)
Expand All @@ -39,14 +40,15 @@

## :sparkles: Features
- **Dark** and **light** versions - *onedark* and *onelight*
- Supports **custom highlighting across filetypes**
- [Treesitter](https://github.com/nvim-treesitter/nvim-treesitter) support
- Options to specify styles for:
- Comments
- Functions
- Keywords
- Strings
- Variables
- Override default styles, colors and highlight groups
- Override default styles, colors, highlight groups and filetype highlight groups
- Create custom highlight groups
- [LSP](https://github.com/neovim/nvim-lspconfig) diagnostics support
- Support for a large array of [vim-polygot](https://github.com/sheerun/vim-polyglot) packs (pull requests welcome)
Expand Down Expand Up @@ -88,6 +90,8 @@
#### React
<img src="https://user-images.githubusercontent.com/9512444/153010048-8ccae711-7695-48e9-ab4b-e2664f24d9e7.png" alt="Comparison to VS Code - React" />

> **Note:** A greater likeness to `VS Code` can be achieved by using the theme's ability to [customise highlight groups by filetype](#configuring-filetype-highlight-groups)
### Lualine
#### Dark
<img src="https://user-images.githubusercontent.com/9512444/153061323-19fd0bd2-5838-470f-9273-ea85a5813319.png" alt="Lualine Dark" />
Expand Down Expand Up @@ -152,21 +156,22 @@ The theme's default configuration as per the [config.lua](https://github.com/oli
```lua
local onedarkpro = require("onedarkpro")
onedarkpro.setup({
-- Theme can be overwritten with 'onedark' or 'onelight' as a string!
-- Theme can be overwritten with 'onedark' or 'onelight' as a string
theme = function()
if vim.o.background == "dark" then
return "onedark"
else
return "onelight"
end
end,
colors = {}, -- Override default colors. Can specify colors for "onelight" or "onedark" themes by passing in a table
hlgroups = {}, -- Override default highlight groups
colors = nil, -- Override default colors by specifying colors for 'onelight' or 'onedark' themes
hlgroups = nil, -- Override default highlight groups
filetype_hlgroups = nil, -- Override default highlight groups for specific filetypes
plugins = { -- Override which plugins highlight groups are loaded
native_lsp = true,
polygot = true,
treesitter = true,
-- Others omitted for brevity
-- NOTE: Other plugins have been omitted for brevity
},
styles = {
strings = "NONE", -- Style that is applied to strings
Expand All @@ -193,7 +198,7 @@ onedarkpro.load()
Use either `onedark` or `onelight` for the dark and light themes, respectively.

```lua
theme = "onedark", -- [onedark/onelight]
theme = "onedark", -- Either "onedark" or "onelight"
```
If no value is specified, the current value of `vim.o.background` will be used to set the theme.

Expand All @@ -204,21 +209,28 @@ By default, all of the plugins supported by the theme are loaded at runtime. Spe
plugins = {
native_lsp = false,
polygot = false,
treesitter = false,
treesitter = false
}
```

Alternatively, all of the plugins can be disabled at once:
```lua
plugins = {
all = false
}
```
Or, all of the plugins can be disabled with only a select few enabled:
```lua
plugins = {
all = false
native_lsp = true,
treesitter = true
}
```

> **Note:** For a full list of plugins, see the plugins [folder](https://github.com/olimorris/onedarkpro.nvim/tree/main/lua/onedarkpro/plugins)
> **Note:** For a full list of plugins supported in the theme, see the plugins [folder](https://github.com/olimorris/onedarkpro.nvim/tree/main/lua/onedarkpro/plugins)
### Configuring styles
Styles can be set by specifying the highlight group from the [theme.lua](https://github.com/olimorris/onedarkpro.nvim/blob/main/lua/onedarkpro/theme.lua) file alongside any desired styles:
Styles can be set by specifying the highlight group from the [theme.lua](https://github.com/olimorris/onedarkpro.nvim/blob/main/lua/onedarkpro/theme.lua) file (and any plugin files) alongside the desired style(s):

```lua
styles = {
Expand All @@ -244,25 +256,25 @@ colors = {
}
```
#### Specifying new colors
New colors may be specified which will then be merged into the theme's color palette:
New colors may be specified in the configuration which will then be merged into the theme's color palette:
```lua
colors = {
my_new_red = "#f44336"
}
```
> **Note:** Custom colors can be referenced in highlight group overrides
> **Note:** Custom colors can also be referenced when creating custom highlight group overrides in `hlgroups`
#### Specifying colors by theme
It's possible to override default colors within the theme such as the `bg` color. This is a common question for those who wish to have a darker background than the default. Of course it would make sense to have different `bg` colors for the `onedark` and `onelight` themes. This can be achieved by specifying the theme name as a table, followed by the color:

```lua
colors = {
onedark = {
bg = "#FFFF00", -- yellow
bg = "#FFFF00" -- yellow
},
onelight = {
bg = "#00FF00", -- green
},
bg = "#00FF00" -- green
}
}
```

Expand All @@ -276,10 +288,57 @@ The [theme.lua](https://github.com/olimorris/onedarkpro.nvim/tree/main/lua/oneda
hlgroups = { -- Overriding the Comment highlight group
Comment = { fg = "#FF0000", bg = "#FFFF00", style = "italic" }, -- 1
Comment = { fg = "${my_new_red}" bg = "${yellow}", style = "bold,italic" }, -- 2
Comment = { link = "Substitute" }, -- 3
Comment = { link = "Substitute" } -- 3
}
```

### Configuring filetype highlight groups
The original <a href="https://binaryify.github.io/OneDark-Pro">One Dark Pro</a> utilises custom highlights based on filetype to achieve its distinctive look. This can also be achieved within the theme:

```lua
filetype_hlgroups = {
yaml = { -- Use the filetype as per the `set filetype?` command
TSField = { fg = "${red}" }
},
python = {
TSConstructor = { fg = "${bg}", bg = "${red}" }
}
}
```
> **Note:** Please see [this issue](https://github.com/olimorris/onedarkpro.nvim/issues/24) for how other users are configuring their theme by filetype
> **Note:** Currently support for highlighting in Telescope's previewer is unavailable.
#### Ignoring filetypes and buffer types
Filetype highlight groups work by detecting the filetype of the current buffer and checking the user's config to determine if any should be applied. If neccessary, the theme's default highlight groups are reapplied if the buffer filetype has no custom filetype highlights specified.

When using common plugins such as [Telescope](https://github.com/nvim-telescope/telescope.nvim) or [Trouble](https://github.com/folke/trouble.nvim), additional windows with distinct filetypes are opened. This can cause the theme to reapply the default highlight groups since it detects a buffer filetype change. When closing the windows, the user's custom filetype highlight groups are then lost. To prevent this from happening, the theme has a table of filetypes and buffer types to ignore:

```lua
filetype_hlgroups_ignore = {
filetypes = {
"^aerial$",
"^alpha$",
"^fugitive$",
"^fugitiveblame$",
"^help$",
"^NvimTree$",
"^packer$",
"^qf$",
"^startify$",
"^startuptime$",
"^TelescopePrompt$",
"^TelescopeResults$",
"^terminal$",
"^toggleterm$",
"^undotree$"
},
buftypes = {
"^terminal$"
}
```
Additional filetypes and buffer types can be added in the config.

### Configuring options

#### Formatting
Expand All @@ -292,7 +351,7 @@ options = {
bold = true, -- Use the themes opinionated bold styles?
italic = true, -- Use the themes opinionated italic styles?
underline = true, -- Use the themes opinionated underline styles?
undercurl = true, -- Use the themes opinionated undercurl styles?
undercurl = true -- Use the themes opinionated undercurl styles?
}
```

Expand Down Expand Up @@ -343,17 +402,17 @@ options = {
### Terminal themes
The theme comes with [Alacritty](https://github.com/alacritty/alacritty) and [Kitty](https://github.com/kovidgoyal/kitty) themes. These can be found in the [extras](https://github.com/olimorris/onedarkpro.nvim/tree/main/extras) folder.

### Helper functions
### Helpers

#### Theme color tables
To enable plugins to match this theme, a helper function, `get_colors()`, has been included. This returns a table of the theme's current colors.
To enable the theme's colors to be extracted and used elsewhere in the Neovim config, a helper function, `get_colors()`, has been included. This returns a table of the theme's current colors.

```lua
local colors = require("onedarkpro").get_colors("onelight")
print(colors.purple) -- #9a77cf
```

Changing the theme from `onelight` to `onedark` would return a new table of colors.
> **Note:** Changing the theme from `onelight` to `onedark` would return a new table of colors.
> **Note:** Setting a `ColorScheme` autocommand which refreshes the configuration of the relevant plugin will ensure that they are using the correct colors from **One Dark Pro**
Expand All @@ -367,9 +426,33 @@ function ToggleTheme()
else
vim.o.background = "dark"
end
require("onedarkpro").load()
end
```
Alongside a custom autocommand to reload the theme's config on a colorscheme change:
```lua
local autocmds = {
onedarkpro_theme_autocmds = {
{
"ColorScheme",
"*",
"lua require(\"path_to_my_theme_config\").function_to_call()"
}
}
}
require("onedarkpro.utils").create_augroups(autocmds)
```
> **Note:** The autocommand is useful if there is deviation from the theme's default configuration
#### Configuring styles/colors/highlight groups based on the theme
When configuring the theme, it may be useful to apply different colors or styles depending on whether `onedark` or `onelight` is active. This can be achieved by applying a conditional in the configuration:

```lua
hlgroups = {
TSField = {
fg = (vim.o.background == "dark" and "${red}" or "${green}")
}
}
```

## :clap: Credits

Expand All @@ -382,8 +465,6 @@ Thanks to the following contributors for their work on the theme:
The following themes were used as an inspiration:

* [One Dark Pro](https://github.com/Binaryify/OneDark-Pro) - The inspiration for this theme
* [Nightfox](https://github.com/EdenEast/nightfox.nvim) - Originally used the theme's layout and utilities however moved away quite significantly since
* [onedark.vim](https://github.com/joshdick/onedark.vim) - For the application of `onedark` colors in Neovim
* [GitHub nvim theme](https://github.com/projekt0n/github-nvim-theme) - For the logo inspiration :wink:

## :page_with_curl: License
Expand Down
2 changes: 1 addition & 1 deletion lua/onedarkpro/colors/onedark.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function M.load(config)
blue = "#61afef",
purple = "#c678dd",
white = "#abb2bf",
black = "#1e1e1e",
black = "#282c34",
gray = "#5c6370",
highlight = "#e2be7d",
none = "NONE"
Expand Down
23 changes: 21 additions & 2 deletions lua/onedarkpro/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,27 @@ M.config = {
return "onelight"
end
end,
colors = {}, -- Override default colors
hlgroups = {}, -- Override default highlight groups
colors = nil, -- Override default colors
hlgroups = nil, -- Override default highlight groups
filetype_hlgroups = nil, -- Override default highlight groups for specific filetypes
filetype_hlgroups_ignore = { -- Filetypes which are ignored when applying filetype highlight groups
filetypes = {
"^aerial$",
"^alpha$",
"^fugitive$",
"^fugitiveblame$",
"^help$",
"^NvimTree$",
"^packer$",
"^qf$",
"^startify$",
"^startuptime$",
"^terminal$",
"^toggleterm$",
"^undotree$"
},
buftypes = { "^terminal$" }
},
plugins = { -- Enable/Disable specific plugins
aerial = true,
barbar = true,
Expand Down
Loading

1 comment on commit 43bfe7d

@olimorris
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For anyone browsing, a brief explanation as to how filetype highlights have been implemented in the theme:

  • A user set's specific filetype highlights in their config
  • When moving between buffers, an autocmd is triggered and the theme checks if the buffer filetype matches the filetype of the new buffer
  • If it does, then the theme applies the highlights from the user's config
  • If the user moves to a buffer which hasn't been defined by a filetype highlight in the config, then we re-apply the theme's default highlights, being careful to only apply the filetype highlight groups (and to avoid applying hundreds of highlight groups unnecessarily)

Please sign in to comment.