From db5b0f441d4bc47bc95593c59a724be62a23851c Mon Sep 17 00:00:00 2001 From: ful1e5 <24286590+ful1e5@users.noreply.github.com> Date: Sun, 10 Apr 2022 14:29:06 +0530 Subject: [PATCH] feat: use native autocmds on nvim >=0.7 --- CHANGELOG.md | 1 + lua/onedark/autocmds.lua | 84 ++++++++++++++++++++++++++++++++++++++++ lua/onedark/util.lua | 35 ++++------------- 3 files changed, 93 insertions(+), 27 deletions(-) create mode 100644 lua/onedark/autocmds.lua diff --git a/CHANGELOG.md b/CHANGELOG.md index fda6683..43fd7a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lua/onedark/autocmds.lua b/lua/onedark/autocmds.lua new file mode 100644 index 0000000..8ae204e --- /dev/null +++ b/lua/onedark/autocmds.lua @@ -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 diff --git a/lua/onedark/util.lua b/lua/onedark/util.lua index 24e6478..c53ecd3 100644 --- a/lua/onedark/util.lua +++ b/lua/onedark/util.lua @@ -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 @@ -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