Skip to content

Commit b556210

Browse files
Get mode at time of event instead of at time of render
resolves: #36 # Details Since all rendering and clearing of `extmarks` happens through a call to `vim.schedule` it is possible that a mode change occurred between a triggering event and the response to that event. In most cases this is not really a problem, however some plugins will programmatically cause several mode changes in quick succession which can cause this plugin to be out of sync, i.e. thinking that a render is not necessary since the last mode was already rendered, however when rendering the last mode, the mode had changed and a render did not occur. An example of this is `fzf-lua` which uses `terminal` mode to perform certain actions. This can cause the plugin to fall behind the current user state, however rendering should be fast enough for this not to be a problem. Can revisit this approach if it proves to be a problem.
1 parent c7fda73 commit b556210

File tree

2 files changed

+35
-25
lines changed

2 files changed

+35
-25
lines changed

lua/render-markdown/init.lua

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -226,22 +226,19 @@ function M.setup(opts)
226226
state.inline_query = vim.treesitter.query.parse('markdown_inline', state.config.inline_query)
227227

228228
-- Call immediately to re-render on LazyReload
229-
vim.schedule(function()
230-
ui.refresh(vim.api.nvim_get_current_buf())
231-
end)
229+
ui.schedule_refresh(vim.api.nvim_get_current_buf())
232230

233231
local group = vim.api.nvim_create_augroup('RenderMarkdown', { clear = true })
234232
vim.api.nvim_create_autocmd({ 'ModeChanged' }, {
235233
group = group,
236234
callback = function(event)
237-
local was_rendered = vim.tbl_contains(state.config.render_modes, vim.v.event.old_mode)
238-
local should_render = vim.tbl_contains(state.config.render_modes, vim.v.event.new_mode)
235+
local render_modes = state.config.render_modes
236+
local prev_rendered = vim.tbl_contains(render_modes, string.sub(vim.v.event.old_mode, 1, 1))
237+
local should_render = vim.tbl_contains(render_modes, string.sub(vim.v.event.new_mode, 1, 1))
239238
-- Only need to re-render if render state is changing. I.e. going from normal mode to
240239
-- command mode with the default config, both are rendered, so no point re-rendering
241-
if was_rendered ~= should_render then
242-
vim.schedule(function()
243-
ui.refresh(event.buf)
244-
end)
240+
if prev_rendered ~= should_render then
241+
ui.schedule_refresh(event.buf)
245242
end
246243
end,
247244
})
@@ -250,18 +247,14 @@ function M.setup(opts)
250247
callback = function()
251248
for _, win in ipairs(vim.v.event.windows) do
252249
local buf = util.win_to_buf(win)
253-
vim.schedule(function()
254-
ui.refresh(buf)
255-
end)
250+
ui.schedule_refresh(buf)
256251
end
257252
end,
258253
})
259254
vim.api.nvim_create_autocmd({ 'FileChangedShellPost', 'FileType', 'TextChanged' }, {
260255
group = group,
261256
callback = function(event)
262-
vim.schedule(function()
263-
ui.refresh(event.buf)
264-
end)
257+
ui.schedule_refresh(event.buf)
265258
end,
266259
})
267260

@@ -272,13 +265,11 @@ end
272265
M.toggle = function()
273266
state.enabled = not state.enabled
274267
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
275-
vim.schedule(function()
276-
if state.enabled then
277-
ui.refresh(buf)
278-
else
279-
ui.clear_valid(buf)
280-
end
281-
end)
268+
if state.enabled then
269+
ui.schedule_refresh(buf)
270+
else
271+
ui.schedule_clear(buf)
272+
end
282273
end
283274
end
284275

lua/render-markdown/ui.lua

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,37 @@ local markdown_inline = require('render-markdown.handler.markdown_inline')
55
local state = require('render-markdown.state')
66
local util = require('render-markdown.util')
77

8+
---@class render.md.Ui
89
local M = {}
910

1011
M.namespace = vim.api.nvim_create_namespace('render-markdown.nvim')
1112

1213
---@param buf integer
13-
M.refresh = function(buf)
14+
M.schedule_refresh = function(buf)
15+
local mode = vim.fn.mode()
16+
vim.schedule(function()
17+
M.refresh(buf, mode)
18+
end)
19+
end
20+
21+
---@param buf integer
22+
M.schedule_clear = function(buf)
23+
vim.schedule(function()
24+
M.clear_valid(buf)
25+
end)
26+
end
27+
28+
---@private
29+
---@param buf integer
30+
---@param mode string
31+
M.refresh = function(buf, mode)
1432
if not state.enabled then
1533
return
1634
end
1735
if not M.clear_valid(buf) then
1836
return
1937
end
20-
if not vim.tbl_contains(state.config.render_modes, vim.fn.mode()) then
38+
if not vim.tbl_contains(state.config.render_modes, mode) then
2139
return
2240
end
2341
if util.file_size_mb(buf) > state.config.max_file_size then
@@ -53,7 +71,8 @@ M.refresh = function(buf)
5371
logger.flush()
5472
end
5573

56-
--- Remove existing highlights / virtual text for valid buffers
74+
---Remove existing highlights / virtual text for valid buffers
75+
---@private
5776
---@param buf integer
5877
---@return boolean
5978
M.clear_valid = function(buf)

0 commit comments

Comments
 (0)