Skip to content

Commit 199cc52

Browse files
feat: reload runtime highlights on color scheme change
## Details Add a ColorScheme autocommand which regenerates cached colors created at runtime. To do this the cache now needs to store all the information needed to re-create the colors rather than just the colors that have already been created. Moves the VimLeave autocommand out of manager and directly into the setup method of the log module. This keeps the manager module coupled to buffer level events and allows the log module to manage its lifecycle in a self contained way.
1 parent 354baf4 commit 199cc52

File tree

6 files changed

+39
-17
lines changed

6 files changed

+39
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- `render_modes` as a boolean [7493db6](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/7493db6d3fe3f6679549e6020498f72e97cd9b73)
1212
- anti conceal selected range in visual mode [#168](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/168)
1313
[5ff191f](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/5ff191f0c7457ede2fd30ecf76ab16c65118b4ee)
14+
[354baf4](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/354baf485370b670bb1c1cd64309438607b0465d)
1415
- disable rendering in diff mode [#169](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/169)
1516
[01b38dc](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/01b38dcf7d0a99620547651fb59a3ba521ba12d5)
1617

doc/render-markdown.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.10.0 Last change: 2024 September 18
1+
*render-markdown.txt* For 0.10.0 Last change: 2024 September 19
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*

lua/render-markdown/colors.lua

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
---@type string[]
2-
local cache = {}
1+
---@class render.md.cache.Colors
2+
---@field combine table<string, { fg: string, bg: string }>
3+
---@field inverse table<string, { hl: string }>
4+
local Cache = {
5+
combine = {},
6+
inverse = {},
7+
}
38

49
---@class render.md.Colors
510
local M = {}
@@ -51,19 +56,34 @@ M.colors = {
5156

5257
---Should only be called from plugin directory
5358
function M.setup()
59+
-- Reload generated colors on color scheme change
60+
vim.api.nvim_create_autocmd('ColorScheme', {
61+
group = vim.api.nvim_create_augroup('RenderMarkdownColors', { clear = true }),
62+
callback = M.reload,
63+
})
5464
for name, link in pairs(M.colors) do
5565
vim.api.nvim_set_hl(0, M.prefix .. name, { link = link, default = true })
5666
end
5767
end
5868

69+
---@private
70+
function M.reload()
71+
for _, color in pairs(Cache.combine) do
72+
M.combine(color.fg, color.bg, true)
73+
end
74+
for _, color in pairs(Cache.inverse) do
75+
M.inverse_bg(color.hl, true)
76+
end
77+
end
78+
5979
---@param foreground string
6080
---@param background string
81+
---@param force? boolean
6182
---@return string
62-
function M.combine(foreground, background)
83+
function M.combine(foreground, background, force)
6384
local name = string.format('%s_%s_%s', M.prefix, foreground, background)
64-
if not vim.tbl_contains(cache, name) then
65-
local fg = M.get_hl(foreground)
66-
local bg = M.get_hl(background)
85+
if Cache.combine[name] == nil or force then
86+
local fg, bg = M.get_hl(foreground), M.get_hl(background)
6787
vim.api.nvim_set_hl(0, name, {
6888
fg = fg.fg,
6989
bg = bg.bg,
@@ -72,23 +92,24 @@ function M.combine(foreground, background)
7292
---@diagnostic disable-next-line: undefined-field
7393
ctermbg = bg.ctermbg,
7494
})
75-
table.insert(cache, name)
95+
Cache.combine[name] = { fg = foreground, bg = background }
7696
end
7797
return name
7898
end
7999

80100
---@param highlight string
101+
---@param force? boolean
81102
---@return string
82-
function M.inverse_bg(highlight)
103+
function M.inverse_bg(highlight, force)
83104
local name = string.format('%s_Inverse_%s', M.prefix, highlight)
84-
if not vim.tbl_contains(cache, name) then
105+
if Cache.inverse[name] == nil or force then
85106
local hl = M.get_hl(highlight)
86107
vim.api.nvim_set_hl(0, name, {
87108
fg = hl.bg,
88109
---@diagnostic disable-next-line: undefined-field
89110
ctermfg = hl.ctermbg,
90111
})
91-
table.insert(cache, name)
112+
Cache.inverse[name] = { hl = highlight }
92113
end
93114
return name
94115
end

lua/render-markdown/core/log.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ local M = {}
1414

1515
---@param level render.md.config.LogLevel
1616
function M.setup(level)
17+
-- Write out any logs before closing
18+
vim.api.nvim_create_autocmd('VimLeave', {
19+
group = vim.api.nvim_create_augroup('RenderMarkdownLog', { clear = true }),
20+
callback = M.flush,
21+
})
1722
M.level = level
1823
M.entries = {}
1924
-- Typically resolves to ~/.local/state/nvim/render-markdown.log

lua/render-markdown/health.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local state = require('render-markdown.state')
44
local M = {}
55

66
---@private
7-
M.version = '7.0.10'
7+
M.version = '7.0.11'
88

99
function M.check()
1010
M.start('version')

lua/render-markdown/manager.lua

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ function M.setup()
3333
end
3434
end,
3535
})
36-
-- Write out any logs before closing
37-
vim.api.nvim_create_autocmd('VimLeave', {
38-
group = M.group,
39-
callback = log.flush,
40-
})
4136
end
4237

4338
---@param enabled boolean

0 commit comments

Comments
 (0)