Skip to content

Commit

Permalink
fix(interactive): use unique augroup for live_colors()
Browse files Browse the repository at this point in the history
  • Loading branch information
tmillr committed Aug 3, 2024
1 parent bbab6a4 commit f610b73
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 28 deletions.
21 changes: 12 additions & 9 deletions lua/github-theme/interactive.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
local util = require('github-theme.util')
local api = vim.api
local api, ts = vim.api, vim.treesitter
local cmd = util.is_nvim and vim.cmd or vim.command
local augroup = 'github-theme.interactive'
local augroup_live_colors = augroup .. '.live_colors'
local M = {}

api.nvim_create_augroup(augroup, { clear = true })
api.nvim_create_augroup(augroup_live_colors, { clear = true })

-- TODO: move to util module?
---@param tbl table the target search in
---@param keypath string[]|string
Expand Down Expand Up @@ -30,7 +35,7 @@ function M.attach()
vim.g.github_theme_debug = true

return api.nvim_create_autocmd('BufWritePost', {
group = api.nvim_create_augroup('github-theme.interactive', { clear = true }),
group = api.nvim_create_augroup(augroup, { clear = true }),
buffer = 0,
desc = 'Reloads user config when the buffer is written',
nested = true,
Expand All @@ -56,15 +61,12 @@ end
---TODO: support other files as well, expose via cmd?, etc.
---@param enable? boolean
function M.live_colors(enable)
local ts = vim.treesitter
local ns = api.nvim_create_namespace('github-theme.interactive')
local augroup = api.nvim_create_augroup('github-theme.interactive', { clear = true })
local ns = api.nvim_create_namespace(augroup_live_colors)

local function get_nodes(node, src, typ, cb)
local stack = {}

-- {{{ Queries

local q1 = ts.query.parse(
'lua',
[[
Expand Down Expand Up @@ -122,8 +124,6 @@ function M.live_colors(enable)
]]
)

-- }}}

-- Map capture names to their ID
local cap1, cap2, cap3 = {}, {}, {}
for _, v in ipairs({
Expand All @@ -135,6 +135,7 @@ function M.live_colors(enable)
v[2][name] = id
end
end
-- }}}

local function matched_node(match, cap_id)
vim.validate({
Expand Down Expand Up @@ -306,6 +307,8 @@ function M.live_colors(enable)
end
end

api.nvim_create_augroup(augroup_live_colors, { clear = true })

if enable == false then
for _, buf in ipairs(api.nvim_list_bufs()) do
api.nvim_buf_clear_namespace(buf, ns, 0, -1)
Expand All @@ -315,7 +318,7 @@ function M.live_colors(enable)
end

api.nvim_create_autocmd({ 'ColorScheme', 'BufNew', 'BufWritePost' }, {
group = augroup,
group = augroup_live_colors,
pattern = '*.lua',
desc = 'Refresh live-displayed colors',
nested = true,
Expand Down
27 changes: 8 additions & 19 deletions lua/github-theme/util/reload.lua
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
---@param force? boolean
local function reload(force)
---Reloads github-theme Lua modules, skipping those which don't typically need to be
---reloaded. Does not clear config unless `force` is true.
---@param force? boolean reload ALL github-theme Lua modules (also clears config)
return function(force)
local pat = vim.regex([==[\.config\|\.deprecation\|\.override\|\.interactive]==])

for name, _ in pairs(_G.package.loaded) do
if name:find('^github%-theme') then
if
force
or (
not name:find('config')
and not name:find('deprecation')
and not name:find('override')
)
then
_G.package.loaded[name] = nil
end
if name:find('^github%-theme') and (force or not pat:match_str(name)) then
_G.package.loaded[name] = nil
end
end
end

return setmetatable({}, {
__call = function(_, ...)
reload(...)
end,
})

0 comments on commit f610b73

Please sign in to comment.