From 04f5456d68efef76060a633de637c6789caad95b Mon Sep 17 00:00:00 2001 From: ful1e5 <24286590+ful1e5@users.noreply.github.com> Date: Fri, 8 Apr 2022 15:40:52 +0530 Subject: [PATCH] feat: use native autocmds on nvim >=0.7 ( fixed #168 ) --- CHANGELOG.md | 1 + lua/github-theme/autocmds.lua | 91 +++++++++++++++++++++++++++++++++++ lua/github-theme/util.lua | 36 ++++---------- 3 files changed, 101 insertions(+), 27 deletions(-) create mode 100644 lua/github-theme/autocmds.lua diff --git a/CHANGELOG.md b/CHANGELOG.md index 785897e6..b20eae87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` diff --git a/lua/github-theme/autocmds.lua b/lua/github-theme/autocmds.lua new file mode 100644 index 00000000..ff4117fc --- /dev/null +++ b/lua/github-theme/autocmds.lua @@ -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 diff --git a/lua/github-theme/util.lua b/lua/github-theme/util.lua index 00d1646e..011697c8 100644 --- a/lua/github-theme/util.lua +++ b/lua/github-theme/util.lua @@ -3,6 +3,7 @@ local types = require('github-theme.types') ---@class gt.Util local util = {} +---@type string util.colors_name = '' ---@type table @@ -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 @@ -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