Skip to content

Commit ca5c29f

Browse files
feat: rely on buffer enabled value rather than state enabled
## Details Previously we used the global `state.enabled` value for the following: - preventing autocommands callbacks from running - enabling / disabling all attached buffers - setting render state to default These responsibilities have all been moved to the individual buffer state. So autocommands are gated by that enabled value, same with toggling buffers. The main change in behavior is that disabled buffers now are attached to since we need to attach to the buffer to listen for potential changes to its state and trigger rendering. This allows users to disable the plugin globally with the top level `enabled` value, then render only the specific current buffer using the `buf_toggle` API. Previously this would still not render buffers as the global state remained disabled. We still keep the `state.enabled` to determine what happens when the `toggle` API is called since that modifies all attached buffers. The behavior of this is not to toggle each buffer based on its state, instead all buffers will end up being enabled or disabled, ignoring individual changes using buffer level APIs.
1 parent dac01bd commit ca5c29f

File tree

9 files changed

+30
-33
lines changed

9 files changed

+30
-33
lines changed

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 NVIM v0.11.1 Last change: 2025 May 21
1+
*render-markdown.txt* For NVIM v0.11.1 Last change: 2025 May 22
22

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

lua/render-markdown/api.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@ function M.enable()
66
end
77

88
function M.buf_enable()
9-
require('render-markdown.core.manager').set_current(true)
9+
require('render-markdown.core.manager').set_buf(nil, true)
1010
end
1111

1212
function M.disable()
1313
require('render-markdown.core.manager').set_all(false)
1414
end
1515

1616
function M.buf_disable()
17-
require('render-markdown.core.manager').set_current(false)
17+
require('render-markdown.core.manager').set_buf(nil, false)
1818
end
1919

2020
function M.toggle()
21-
require('render-markdown.core.manager').set_all()
21+
require('render-markdown.core.manager').set_all(nil)
2222
end
2323

2424
function M.buf_toggle()
25-
require('render-markdown.core.manager').set_current()
25+
require('render-markdown.core.manager').set_buf(nil, nil)
2626
end
2727

2828
function M.log()

lua/render-markdown/core/manager.lua

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,12 @@ function M.init()
4343
vim.api.nvim_create_autocmd('WinResized', {
4444
group = M.group,
4545
callback = function(args)
46-
if not state.enabled then
47-
return
48-
end
4946
for _, win in ipairs(vim.v.event.windows) do
5047
local buf = Env.win.buf(win)
5148
if M.attached(buf) then
52-
ui.update(buf, win, args.event, true)
49+
if state.get(buf).enabled then
50+
ui.update(buf, win, args.event, true)
51+
end
5352
end
5453
end
5554
end,
@@ -74,13 +73,14 @@ function M.set_all(enabled)
7473
state.enabled = not state.enabled
7574
end
7675
for _, buf in ipairs(M.buffers) do
77-
M.update(buf, 'UserCommand')
76+
M.set_buf(buf, state.enabled)
7877
end
7978
end
8079

80+
---@param buf? integer
8181
---@param enabled? boolean
82-
function M.set_current(enabled)
83-
local buf = Env.buf.current()
82+
function M.set_buf(buf, enabled)
83+
buf = buf or Env.buf.current()
8484
if M.attached(buf) then
8585
local config = state.get(buf)
8686
if enabled ~= nil then
@@ -130,7 +130,7 @@ function M.attach(buf)
130130
group = M.group,
131131
buffer = buf,
132132
callback = function(args)
133-
if not state.enabled then
133+
if not state.get(buf).enabled then
134134
return
135135
end
136136
local win, windows = Env.win.current(), Env.buf.windows(buf)
@@ -143,7 +143,9 @@ function M.attach(buf)
143143
end,
144144
})
145145

146-
M.update(buf, 'Initial')
146+
if config.enabled then
147+
M.update(buf, 'Initial')
148+
end
147149
end
148150

149151
---@private
@@ -177,11 +179,6 @@ function M.should_attach(buf)
177179
end
178180

179181
local config = state.get(buf)
180-
if not config.enabled then
181-
log.buf('info', 'attach', buf, 'skip', 'state disabled')
182-
return false
183-
end
184-
185182
local file_size, max_file_size = Env.file_size_mb(buf), config.max_file_size
186183
if file_size > max_file_size then
187184
local reason = ('%f > %f'):format(file_size, max_file_size)

lua/render-markdown/core/ui.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ function M.run_update(buf, win, change)
8282
local mode = Env.mode.get()
8383
local row = Env.row.get(buf, win)
8484

85-
local render = state.enabled
86-
and config.enabled
85+
local render = config.enabled
8786
and config:render(mode)
8887
and not Env.win.get(win, 'diff')
8988
and Env.win.view(win).leftcol == 0

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.4.4'
8+
M.version = '8.4.5'
99

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

lua/render-markdown/lib/config.lua

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ local Config = {}
1414
Config.__index = Config
1515

1616
---@param root render.md.Config
17+
---@param enabled boolean
1718
---@param buf integer
1819
---@return render.md.main.Config
19-
function Config.new(root, buf)
20+
function Config.new(root, enabled, buf)
2021
---@type render.md.buffer.Config
2122
local config = {
22-
enabled = true,
23+
enabled = enabled,
2324
render_modes = root.render_modes,
2425
max_file_size = root.max_file_size,
2526
debounce = root.debounce,
@@ -96,12 +97,12 @@ end
9697

9798
---@private
9899
---@generic T: render.md.callout.Config|render.md.checkbox.custom.Config
99-
---@param components table<string, T>
100+
---@param component table<string, T>
100101
---@return table<string, T>
101-
function Config.normalize(components)
102+
function Config.normalize(component)
102103
local result = {}
103-
for _, component in pairs(components) do
104-
result[component.raw:lower()] = component
104+
for _, value in pairs(component) do
105+
result[value.raw:lower()] = value
105106
end
106107
return result
107108
end
@@ -131,7 +132,7 @@ end
131132

132133
---@param destination string
133134
---@param icon render.md.mark.Text
134-
function Config:link_text(destination, icon)
135+
function Config:set_link_text(destination, icon)
135136
local options = Iter.table.filter(self.link.custom, function(custom)
136137
if custom.kind == 'suffix' then
137138
return vim.endswith(destination, custom.pattern)

lua/render-markdown/render/inline/link.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ function Render:setup()
3030
elseif self.node.type == 'inline_link' then
3131
local destination = self.node:child('link_destination')
3232
if destination then
33-
self.context.config:link_text(destination.text, icon)
33+
self.context.config:set_link_text(destination.text, icon)
3434
end
3535
elseif self.node.type == 'uri_autolink' then
3636
local destination = self.node.text:sub(2, -2)
37-
self.context.config:link_text(destination, icon)
37+
self.context.config:set_link_text(destination, icon)
3838
autolink = true
3939
end
4040
self.data = { icon = icon, autolink = autolink }

lua/render-markdown/render/inline/shortcut.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function Render:wiki_link()
6262
self:hide(ctx.end_col - 1, 1)
6363
---@type render.md.mark.Text
6464
local icon = { config.icon, config.highlight }
65-
self.context.config:link_text(ctx.destination, icon)
65+
self.context.config:set_link_text(ctx.destination, icon)
6666
local body = config.body(ctx)
6767
if not body then
6868
-- add icon

lua/render-markdown/state.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ end
5050
function M.get(buf)
5151
local result = M.cache[buf]
5252
if not result then
53-
result = Config.new(M.config, buf)
53+
result = Config.new(M.config, M.enabled, buf)
5454
M.cache[buf] = result
5555
end
5656
return result

0 commit comments

Comments
 (0)