Skip to content

Commit 0d1b065

Browse files
chore(dev): allow the plugin to work with Lazy reload
## Details Using `Lazy reload render-markdown.nvim` would be convenient for development, however how lazy handles the reload results in any created autocommands being deleted, which basically stops the plugin from working. To fix this we essentially need to re-run the plugin directory on reload, which lazy does do, however the value of `vim.g.loaded_render_markdown` persists which results in early termination. To fix this use a lua variable, we'll call it enabled, which does persist the value locally, but does get reverted back to `false` when lazy does it's module unloading thing. This will result in the autocommands being re-created solving the main problem. I've renamed the various `setup` functions to `init` to distinguish better between initialization and configuration. The `log` module now has an `init` and a `setup` function, with the autocommand being created in the new `init` function. Move all cache invalidation logic to `state` module, results in an even more simplified `init` module and `state` is probably a more fitting place for it.
1 parent 642222a commit 0d1b065

File tree

17 files changed

+96
-84
lines changed

17 files changed

+96
-84
lines changed

demo/run.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from PIL import Image
66

7-
INFO: dict[str, tuple[int, str]] = dict(
7+
DEMOS: dict[str, tuple[int, str]] = dict(
88
heading_code=(550, "## Heading 2"),
99
list_table=(550, ""),
1010
box_dash_quote=(250, ""),
@@ -26,7 +26,7 @@ def create_gif(name: str, file: Path) -> None:
2626
if gif.exists():
2727
gif.unlink()
2828

29-
height, content = INFO[name]
29+
height, content = DEMOS[name]
3030

3131
tape = Path("demo/demo.tape")
3232
tape.write_text(tape_content(file, gif, height, content))
@@ -91,6 +91,6 @@ def get_move(file: Path) -> str:
9191

9292
if __name__ == "__main__":
9393
parser = argparse.ArgumentParser(description="Generate a demo recording using vhs")
94-
parser.add_argument("--name", type=str, required=True, choices=INFO.keys())
94+
parser.add_argument("--name", type=str, required=True, choices=DEMOS.keys())
9595
args = parser.parse_args()
9696
main(args.name)

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.11.0 Last change: 2025 April 22
1+
*render-markdown.txt* For 0.11.0 Last change: 2025 April 23
22

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

lua/render-markdown/colors.lua

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,19 @@ M.colors = {
6262
Error = 'DiagnosticError',
6363
}
6464

65-
---Should only be called from plugin directory
66-
function M.setup()
67-
-- Reload generated colors on color scheme change
68-
vim.api.nvim_create_autocmd('ColorScheme', {
69-
group = vim.api.nvim_create_augroup('RenderMarkdownColors', {}),
70-
callback = M.reload,
71-
})
65+
---called from plugin directory
66+
function M.init()
7267
for name, link in pairs(M.colors) do
7368
vim.api.nvim_set_hl(0, M.prefix .. name, {
7469
link = link,
7570
default = true,
7671
})
7772
end
73+
-- reload generated colors on color scheme change
74+
vim.api.nvim_create_autocmd('ColorScheme', {
75+
group = vim.api.nvim_create_augroup('RenderMarkdownColors', {}),
76+
callback = M.reload,
77+
})
7878
end
7979

8080
---@private

lua/render-markdown/command.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ M.name = 'RenderMarkdown'
99
---@private
1010
M.plugin = 'render-markdown.nvim'
1111

12-
---Should only be called from plugin directory
13-
function M.setup()
12+
---called from plugin directory
13+
function M.init()
1414
vim.api.nvim_create_user_command(M.name, M.command, {
1515
nargs = '*',
1616
desc = M.plugin .. ' commands',

lua/render-markdown/core/log.lua

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,31 @@ local Env = require('render-markdown.lib.env')
77
---@field message string
88

99
---@class render.md.Log
10+
---@field private file string
1011
---@field private level render.md.config.LogLevel
1112
---@field private entries render.md.log.Entry[]
12-
---@field private file string
1313
local M = {}
1414

15-
---Should only be called from state on setup
16-
---@param level render.md.config.LogLevel
17-
function M.setup(level)
18-
-- Write out any logs before closing
15+
---called from plugin directory
16+
function M.init()
17+
-- typically resolves to ~/.local/state/nvim/render-markdown.log
18+
M.file = vim.fn.stdpath('state') .. '/render-markdown.log'
19+
-- clear the file contents if it is too big
20+
if Env.file_size_mb(M.file) > 5 then
21+
assert(io.open(M.file, 'w')):close()
22+
end
23+
-- write out any logs before closing
1924
vim.api.nvim_create_autocmd('VimLeave', {
2025
group = vim.api.nvim_create_augroup('RenderMarkdownLog', {}),
2126
callback = M.flush,
2227
})
28+
end
29+
30+
---called from state on setup
31+
---@param level render.md.config.LogLevel
32+
function M.setup(level)
2333
M.level = level
2434
M.entries = {}
25-
-- Typically resolves to ~/.local/state/nvim/render-markdown.log
26-
M.file = vim.fn.stdpath('state') .. '/render-markdown.log'
27-
-- Clear the file contents if it is too big
28-
if Env.file_size_mb(M.file) > 5 then
29-
assert(io.open(M.file, 'w')):close()
30-
end
3135
end
3236

3337
function M.open()

lua/render-markdown/health.lua

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

77
---@private
8-
M.version = '8.3.5'
8+
M.version = '8.3.6'
99

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

lua/render-markdown/init.lua

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ local M = {}
4343
---@field win_options table<string, render.md.window.Config>
4444

4545
---@private
46+
---@type boolean
4647
M.initialized = false
4748

4849
---@type render.md.Config
@@ -713,10 +714,7 @@ function M.setup(opts)
713714
return
714715
end
715716
M.initialized = true
716-
717717
require('render-markdown.state').setup(opts or {})
718-
require('render-markdown.state').invalidate_cache()
719-
require('render-markdown.core.ui').invalidate_cache()
720718
end
721719

722720
return setmetatable(M, {

lua/render-markdown/integ/blink.lua

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,29 @@ function Source:get_completions(context, callback)
4040
end
4141

4242
---@class render.md.integ.Blink
43-
---@field private id string
44-
---@field private registered boolean
45-
local M = {
46-
id = 'markdown',
47-
registered = false,
48-
}
43+
local M = {}
4944

50-
---Should only be called from manager on initial buffer attach
45+
---@private
46+
---@type boolean
47+
M.initialized = false
48+
49+
---called from manager on buffer attach
5150
function Source.setup()
52-
if M.registered then
51+
if M.initialized then
5352
return
5453
end
55-
M.registered = true
54+
M.initialized = true
5655
local has_blink, blink = pcall(require, 'blink.cmp')
5756
if not has_blink or blink == nil then
5857
return
5958
end
60-
pcall(blink.add_source_provider, M.id, {
59+
local id = 'markdown'
60+
pcall(blink.add_source_provider, id, {
6161
name = 'RenderMarkdown',
6262
module = 'render-markdown.integ.blink',
6363
})
6464
for _, file_type in ipairs(state.file_types) do
65-
pcall(blink.add_filetype_source, file_type, M.id)
65+
pcall(blink.add_filetype_source, file_type, id)
6666
end
6767
end
6868

lua/render-markdown/integ/cmp.lua

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,18 @@ function Source:complete(params, callback)
3030
end
3131

3232
---@class render.md.integ.Cmp
33-
---@field private registered boolean
34-
local M = {
35-
registered = false,
36-
}
33+
local M = {}
3734

38-
---Should only be called from manager on initial buffer attach
35+
---@private
36+
---@type boolean
37+
M.initialized = false
38+
39+
---called from manager on buffer attach
3940
function M.setup()
40-
if M.registered then
41+
if M.initialized then
4142
return
4243
end
43-
M.registered = true
44+
M.initialized = true
4445
local has_cmp, cmp = pcall(require, 'cmp')
4546
if not has_cmp or cmp == nil then
4647
return

lua/render-markdown/integ/coq.lua

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
local source = require('render-markdown.integ.source')
22

33
---@class render.md.cmp.Coq
4-
---@field private registered boolean
5-
local M = {
6-
registered = false,
7-
}
4+
local M = {}
85

9-
---Should only be called from manager on initial buffer attach
10-
---or by a user to enable the integration
6+
---@private
7+
---@type boolean
8+
M.initialized = false
9+
10+
---called from manager on buffer attach or directly by user
1111
function M.setup()
12-
if M.registered then
12+
if M.initialized then
1313
return
1414
end
15-
M.registered = true
15+
M.initialized = true
1616
local has_coq = pcall(require, 'coq')
1717
if not has_coq then
1818
return

0 commit comments

Comments
 (0)