Skip to content

Commit 7946707

Browse files
committed
refactor: lua autocmds
1 parent 715c554 commit 7946707

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
- enhance: `overrides` function is now able to add custom highlight in `dev` mode.
1818
- docs: about developer mode
1919
- breaking-change: set lualine theme with `theme="auto"`
20+
- refactor: lua autocmds
2021

2122
### Fixes
2223

lua/github-theme/util.lua

+43-15
Original file line numberDiff line numberDiff line change
@@ -107,30 +107,58 @@ util.highlight = function(hi_name, hi)
107107
end
108108
end
109109

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('autocmd! ' .. util.colors_name)
114-
vim.cmd('augroup!' .. util.colors_name)
115-
end
116-
end
117-
118110
---@param config gt.ConfigSchema
119111
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()')
112+
local group = vim.api.nvim_create_augroup('github-theme', { clear = false })
113+
114+
-- Delete the github-theme autocmds when the theme changes to something else
115+
vim.api.nvim_create_autocmd('ColorScheme', {
116+
pattern = '*',
117+
group = group,
118+
callback = function()
119+
if vim.g.colors_name ~= util.colors_name then
120+
vim.api.nvim_del_augroup_by_id(group)
121+
end
122+
end,
123+
})
124+
123125
if config.dev then
124-
vim.cmd(string.format('autocmd BufWritePost */lua/github-theme/** nested colorscheme %s', util.colors_name))
126+
-- Enables hot-reloading in github-nvim-theme.
127+
vim.api.nvim_create_autocmd('BufWritePost', {
128+
pattern = '*/lua/github-theme/**',
129+
nested = true,
130+
group = group,
131+
callback = function()
132+
vim.cmd(string.format('colorscheme %s', util.colors_name))
133+
end,
134+
})
135+
end
136+
137+
local func_winhightlight = function()
138+
vim.wo.winhighlight = 'Normal:NormalSB,SignColumn:SignColumnSB'
125139
end
140+
126141
for _, sidebar in ipairs(config.sidebars) do
127142
if sidebar == 'terminal' then
128-
vim.cmd('autocmd TermOpen * setlocal winhighlight=Normal:NormalSB,SignColumn:SignColumnSB')
143+
-- Set dark color for terminal background.,
144+
vim.api.nvim_create_autocmd('TermOpen', {
145+
pattern = '*',
146+
group = group,
147+
callback = function()
148+
func_winhightlight()
149+
end,
150+
})
129151
else
130-
vim.cmd(string.format('autocmd FileType %s setlocal winhighlight=Normal:NormalSB,SignColumn:SignColumnSB', sidebar))
152+
-- Set dark color for custom sidebars background.
153+
vim.api.nvim_create_autocmd('FileType', {
154+
pattern = sidebar,
155+
group = group,
156+
callback = function()
157+
func_winhightlight()
158+
end,
159+
})
131160
end
132161
end
133-
vim.cmd('augroup end')
134162
end
135163

136164
---@param base gt.Highlights.Base

0 commit comments

Comments
 (0)