Skip to content
This repository has been archived by the owner on Jul 11, 2022. It is now read-only.

Commit

Permalink
feat: use native autocmds on nvim >=0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
ful1e5 committed Apr 10, 2022
1 parent 7b8ab01 commit db5b0f4
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- plugin support [ggandor/lightspeed.nvim](https://github.com/ggandor/lightspeed.nvim) #103
- feat: use native autocmds on nvim >=0.7
- feat: use typing (types.od.HighlightStyle) inside `theme.lua`
- feat: add non-exists highlight from `overrides` option in dev mode (fixed #99)
- enhance: bright & dark colors updated in terminal themes
Expand Down
84 changes: 84 additions & 0 deletions lua/onedark/autocmds.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
---@class od.Autocmds
local autocmds = {}

---Delete the autocmds when the theme changes to something else
autocmds.on_colorscheme = function()
if vim.g.colors_name ~= 'onedark' then
vim.cmd('silent! autocmd! onedark')
vim.cmd('silent! augroup! onedark')
end
end

---@param config od.ConfigSchema
autocmds.viml_cmds = function(config)
vim.cmd('augroup onedark')
vim.cmd('autocmd!')
vim.cmd('autocmd ColorScheme * lua require("onedark.util").on_colorscheme()')
if config.dev then
vim.cmd('autocmd BufWritePost */lua/onedark/** nested colorscheme onedark')
end
for _, sidebar in ipairs(config.sidebars) do
if sidebar == 'terminal' then
vim.cmd('autocmd TermOpen * setlocal winhighlight=Normal:NormalSB,SignColumn:SignColumnSB')
else
vim.cmd(string.format('autocmd FileType %s setlocal winhighlight=Normal:NormalSB,SignColumn:SignColumnSB', sidebar))
end
end
vim.cmd('augroup end')
end

---@param config onedark.ConfigSchema
autocmds.native_cmds = function(config)
local group = vim.api.nvim_create_augroup('onedark', { clear = false })

-- Delete the onedark autocmds when the theme changes to something else
vim.api.nvim_create_autocmd('ColorScheme', {
pattern = '*',
group = group,
callback = function()
if vim.g.colors_name ~= 'onedark' then
vim.api.nvim_del_augroup_by_id(group)
end
end,
})

if config.dev then
-- Enables hot-reloading in onedark.
vim.api.nvim_create_autocmd('BufWritePost', {
pattern = '*/lua/onedark/**',
nested = true,
group = group,
callback = function()
vim.cmd('colorscheme onedark')
end,
})
end

local func_winhightlight = function()
vim.wo.winhighlight = 'Normal:NormalSB,SignColumn:SignColumnSB'
end

for _, sidebar in ipairs(config.sidebars) do
if sidebar == 'terminal' then
-- Set dark color for terminal background.,
vim.api.nvim_create_autocmd('TermOpen', {
pattern = '*',
group = group,
callback = function()
func_winhightlight()
end,
})
else
-- Set dark color for custom sidebars background.
vim.api.nvim_create_autocmd('FileType', {
pattern = sidebar,
group = group,
callback = function()
func_winhightlight()
end,
})
end
end
end

return autocmds
35 changes: 8 additions & 27 deletions lua/onedark/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -148,32 +148,6 @@ util.highlight = function(hi_name, hi)
end
end

--- Delete the autocmds when the theme changes to something else
util.on_colorscheme = function()
if vim.g.colors_name ~= 'onedark' then
vim.cmd('silent! autocmd! onedark')
vim.cmd('silent! augroup! onedark')
end
end

---@param cfg od.ConfigSchema
util.autocmds = function(cfg)
vim.cmd('augroup onedark')
vim.cmd('autocmd!')
vim.cmd('autocmd ColorScheme * lua require("onedark.util").on_colorscheme()')
if cfg.dev then
vim.cmd('autocmd BufWritePost */lua/onedark/** nested colorscheme onedark')
end
for _, sidebar in ipairs(cfg.sidebars) do
if sidebar == 'terminal' then
vim.cmd('autocmd TermOpen * setlocal winhighlight=Normal:NormalSB,SignColumn:SignColumnSB')
else
vim.cmd(string.format('autocmd FileType %s setlocal winhighlight=Normal:NormalSB,SignColumn:SignColumnSB', sidebar))
end
end
vim.cmd('augroup end')
end

---@param base od.Highlights.Base
util.syntax = function(base)
for hi_name, hi in pairs(base) do
Expand Down Expand Up @@ -252,7 +226,14 @@ util.load = function(hi)

-- load base theme
util.syntax(hi.base)
util.autocmds(hi.config)

local autocmds = require('onedark.autocmds')
if vim.fn.has('nvim-0.7') == 1 then
autocmds.native_cmds(hi.config)
else
autocmds.viml_cmds(hi.config)
end

util.terminal(hi.colors)
util.syntax(hi.plugins)
end
Expand Down

0 comments on commit db5b0f4

Please sign in to comment.