Skip to content

Commit

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

- refactor: color types moved to `types/palette.lua`
- chore: terminal colors
- feat: use native autocmds on nvim >=0.7 ( fixed #168 )
- chore: tmux theme's extension changed to `.conf` from `.tmux`
- breaking-change: removed some colors from `colors.lua`
- refactor: implement highlight override function in `util.load`
Expand Down
91 changes: 91 additions & 0 deletions lua/github-theme/autocmds.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
---@class gt.Autocmds
local autocmds = {}

---@type string
autocmds.colors_name = ''

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

---@param config gt.ConfigSchema
---@param colors_name string
autocmds.viml_cmds = function(config, colors_name)
autocmds.colors_name = colors_name
vim.cmd(string.format('augroup %s ', autocmds.colors_name))
vim.cmd('autocmd!')
vim.cmd('autocmd ColorScheme * lua require("github-theme.autocmd").on_colorscheme()')
if config.dev then
vim.cmd(string.format('autocmd BufWritePost */lua/github-theme/** nested colorscheme %s', autocmds.colors_name))
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 gt.ConfigSchema
---@param colors_name string
autocmds.native_cmds = function(config, colors_name)
autocmds.colors_name = colors_name
local group = vim.api.nvim_create_augroup(autocmds.colors_name, { clear = false })

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

if config.dev then
-- Enables hot-reloading in github-nvim-theme.
vim.api.nvim_create_autocmd('BufWritePost', {
pattern = '*/lua/github-theme/**',
nested = true,
group = group,
callback = function()
vim.cmd(string.format('colorscheme %s', autocmds.colors_name))
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
36 changes: 9 additions & 27 deletions lua/github-theme/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local types = require('github-theme.types')
---@class gt.Util
local util = {}

---@type string
util.colors_name = ''

---@type table<number, gt.HexColor>
Expand Down Expand Up @@ -107,32 +108,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 ~= util.colors_name then
vim.cmd('silent! autocmd! ' .. util.colors_name)
vim.cmd('silent! augroup!' .. util.colors_name)
end
end

---@param config gt.ConfigSchema
util.autocmds = function(config)
vim.cmd(string.format('augroup %s ', util.colors_name))
vim.cmd('autocmd!')
vim.cmd('autocmd ColorScheme * lua require("github-theme.util").on_colorscheme()')
if config.dev then
vim.cmd(string.format('autocmd BufWritePost */lua/github-theme/** nested colorscheme %s', util.colors_name))
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 base gt.Highlights.Base
util.syntax = function(base)
for hi_name, hi in pairs(base) do
Expand Down Expand Up @@ -201,7 +176,14 @@ util.load = function(hi)

--Load ColorScheme
util.syntax(hi.base)
util.autocmds(hi.config)

local autocmds = require('github-theme.autocmds')
if vim.fn.has('nvim-0.7') == 1 then
autocmds.native_cmds(hi.config, util.colors_name)
else
autocmds.viml_cmds(hi.config, util.colors_name)
end

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

0 comments on commit 04f5456

Please sign in to comment.