Skip to content

Commit 04f5456

Browse files
committed
feat: use native autocmds on nvim >=0.7 ( fixed #168 )
1 parent 425f9de commit 04f5456

File tree

3 files changed

+101
-27
lines changed

3 files changed

+101
-27
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- refactor: color types moved to `types/palette.lua`
1313
- chore: terminal colors
14+
- feat: use native autocmds on nvim >=0.7 ( fixed #168 )
1415
- chore: tmux theme's extension changed to `.conf` from `.tmux`
1516
- breaking-change: removed some colors from `colors.lua`
1617
- refactor: implement highlight override function in `util.load`

lua/github-theme/autocmds.lua

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
---@class gt.Autocmds
2+
local autocmds = {}
3+
4+
---@type string
5+
autocmds.colors_name = ''
6+
7+
---Delete the autocmds when the theme changes to something else
8+
autocmds.on_colorscheme = function()
9+
if vim.g.colors_name ~= autocmds.colors_name then
10+
vim.cmd('silent! autocmd! ' .. autocmds.colors_name)
11+
vim.cmd('silent! augroup!' .. autocmds.colors_name)
12+
end
13+
end
14+
15+
---@param config gt.ConfigSchema
16+
---@param colors_name string
17+
autocmds.viml_cmds = function(config, colors_name)
18+
autocmds.colors_name = colors_name
19+
vim.cmd(string.format('augroup %s ', autocmds.colors_name))
20+
vim.cmd('autocmd!')
21+
vim.cmd('autocmd ColorScheme * lua require("github-theme.autocmd").on_colorscheme()')
22+
if config.dev then
23+
vim.cmd(string.format('autocmd BufWritePost */lua/github-theme/** nested colorscheme %s', autocmds.colors_name))
24+
end
25+
for _, sidebar in ipairs(config.sidebars) do
26+
if sidebar == 'terminal' then
27+
vim.cmd('autocmd TermOpen * setlocal winhighlight=Normal:NormalSB,SignColumn:SignColumnSB')
28+
else
29+
vim.cmd(string.format('autocmd FileType %s setlocal winhighlight=Normal:NormalSB,SignColumn:SignColumnSB', sidebar))
30+
end
31+
end
32+
vim.cmd('augroup end')
33+
end
34+
35+
---@param config gt.ConfigSchema
36+
---@param colors_name string
37+
autocmds.native_cmds = function(config, colors_name)
38+
autocmds.colors_name = colors_name
39+
local group = vim.api.nvim_create_augroup(autocmds.colors_name, { clear = false })
40+
41+
-- Delete the github-theme autocmds when the theme changes to something else
42+
vim.api.nvim_create_autocmd('ColorScheme', {
43+
pattern = '*',
44+
group = group,
45+
callback = function()
46+
if vim.g.colors_name ~= autocmds.colors_name then
47+
vim.api.nvim_del_augroup_by_id(group)
48+
end
49+
end,
50+
})
51+
52+
if config.dev then
53+
-- Enables hot-reloading in github-nvim-theme.
54+
vim.api.nvim_create_autocmd('BufWritePost', {
55+
pattern = '*/lua/github-theme/**',
56+
nested = true,
57+
group = group,
58+
callback = function()
59+
vim.cmd(string.format('colorscheme %s', autocmds.colors_name))
60+
end,
61+
})
62+
end
63+
64+
local func_winhightlight = function()
65+
vim.wo.winhighlight = 'Normal:NormalSB,SignColumn:SignColumnSB'
66+
end
67+
68+
for _, sidebar in ipairs(config.sidebars) do
69+
if sidebar == 'terminal' then
70+
-- Set dark color for terminal background.,
71+
vim.api.nvim_create_autocmd('TermOpen', {
72+
pattern = '*',
73+
group = group,
74+
callback = function()
75+
func_winhightlight()
76+
end,
77+
})
78+
else
79+
-- Set dark color for custom sidebars background.
80+
vim.api.nvim_create_autocmd('FileType', {
81+
pattern = sidebar,
82+
group = group,
83+
callback = function()
84+
func_winhightlight()
85+
end,
86+
})
87+
end
88+
end
89+
end
90+
91+
return autocmds

lua/github-theme/util.lua

+9-27
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local types = require('github-theme.types')
33
---@class gt.Util
44
local util = {}
55

6+
---@type string
67
util.colors_name = ''
78

89
---@type table<number, gt.HexColor>
@@ -107,32 +108,6 @@ util.highlight = function(hi_name, hi)
107108
end
108109
end
109110

110-
---Delete the autocmds when the theme changes to something else
111-
util.on_colorscheme = function()
112-
if vim.g.colors_name ~= util.colors_name then
113-
vim.cmd('silent! autocmd! ' .. util.colors_name)
114-
vim.cmd('silent! augroup!' .. util.colors_name)
115-
end
116-
end
117-
118-
---@param config gt.ConfigSchema
119-
util.autocmds = function(config)
120-
vim.cmd(string.format('augroup %s ', util.colors_name))
121-
vim.cmd('autocmd!')
122-
vim.cmd('autocmd ColorScheme * lua require("github-theme.util").on_colorscheme()')
123-
if config.dev then
124-
vim.cmd(string.format('autocmd BufWritePost */lua/github-theme/** nested colorscheme %s', util.colors_name))
125-
end
126-
for _, sidebar in ipairs(config.sidebars) do
127-
if sidebar == 'terminal' then
128-
vim.cmd('autocmd TermOpen * setlocal winhighlight=Normal:NormalSB,SignColumn:SignColumnSB')
129-
else
130-
vim.cmd(string.format('autocmd FileType %s setlocal winhighlight=Normal:NormalSB,SignColumn:SignColumnSB', sidebar))
131-
end
132-
end
133-
vim.cmd('augroup end')
134-
end
135-
136111
---@param base gt.Highlights.Base
137112
util.syntax = function(base)
138113
for hi_name, hi in pairs(base) do
@@ -201,7 +176,14 @@ util.load = function(hi)
201176

202177
--Load ColorScheme
203178
util.syntax(hi.base)
204-
util.autocmds(hi.config)
179+
180+
local autocmds = require('github-theme.autocmds')
181+
if vim.fn.has('nvim-0.7') == 1 then
182+
autocmds.native_cmds(hi.config, util.colors_name)
183+
else
184+
autocmds.viml_cmds(hi.config, util.colors_name)
185+
end
186+
205187
util.terminal(hi.colors)
206188
util.syntax(hi.plugins)
207189
end

0 commit comments

Comments
 (0)