Skip to content

Commit b75f681

Browse files
Only reparse tree if invalid, check file size before attaching to buffer
1 parent 249c999 commit b75f681

File tree

2 files changed

+26
-30
lines changed

2 files changed

+26
-30
lines changed

lua/render-markdown/manager.lua

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function M.setup()
3131
for _, win in ipairs(vim.v.event.windows) do
3232
local buf = util.win_to_buf(win)
3333
if vim.tbl_contains(data.buffers, buf) then
34-
ui.schedule_refresh(buf, true)
34+
ui.schedule_render(buf, true)
3535
end
3636
end
3737
end,
@@ -44,7 +44,7 @@ function M.set_all(enabled)
4444
M.attach(vim.api.nvim_get_current_buf())
4545
state.enabled = enabled
4646
for _, buf in ipairs(data.buffers) do
47-
ui.schedule_refresh(buf, true)
47+
ui.schedule_render(buf, true)
4848
end
4949
end
5050

@@ -57,25 +57,28 @@ function M.attach(buf)
5757
if vim.tbl_contains(state.config.exclude.buftypes, util.get_buf(buf, 'buftype')) then
5858
return
5959
end
60+
if util.file_size_mb(buf) > state.config.max_file_size then
61+
return
62+
end
6063
if vim.tbl_contains(data.buffers, buf) then
6164
return
6265
end
6366
table.insert(data.buffers, buf)
6467
-- Events that do not imply modifications to buffer so can avoid re-parsing
65-
-- This relies on the ui parsing the buffer anyway if it is the first refresh
68+
-- This relies on the ui parsing the buffer anyway if it is the first time it is seen
6669
vim.api.nvim_create_autocmd({ 'BufWinEnter', 'BufLeave' }, {
6770
group = M.group,
6871
buffer = buf,
6972
callback = function()
70-
ui.schedule_refresh(buf, false)
73+
ui.schedule_render(buf, false)
7174
end,
7275
})
7376
-- Events that imply modifications to buffer so require re-parsing
7477
vim.api.nvim_create_autocmd({ 'TextChanged' }, {
7578
group = M.group,
7679
buffer = buf,
7780
callback = function()
78-
ui.schedule_refresh(buf, true)
81+
ui.schedule_render(buf, true)
7982
end,
8083
})
8184
-- Use information specific to this event to determine whether to render or not
@@ -91,7 +94,7 @@ function M.attach(buf)
9194
if prev_rendered ~= should_render then
9295
-- Since we do not listen to changes that happen while the user is in insert mode
9396
-- we must assume changes were made and re-parse the buffer
94-
ui.schedule_refresh(buf, true)
97+
ui.schedule_render(buf, true)
9598
end
9699
end,
97100
})
@@ -101,7 +104,7 @@ function M.attach(buf)
101104
buffer = buf,
102105
callback = function()
103106
-- Moving cursor should not result in modifications to buffer so can avoid re-parsing
104-
ui.schedule_refresh(buf, false)
107+
ui.schedule_render(buf, false)
105108
end,
106109
})
107110
end

lua/render-markdown/ui.lua

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ end
2828

2929
---@param buf integer
3030
---@param parse boolean
31-
function M.schedule_refresh(buf, parse)
31+
function M.schedule_render(buf, parse)
3232
local mode = vim.fn.mode(true)
3333
vim.schedule(function()
3434
if state.config.profile then
3535
profiler.profile(buf, function()
36-
return M.refresh(buf, mode, parse)
36+
return M.render(buf, mode, parse)
3737
end)
3838
else
39-
M.refresh(buf, mode, parse)
39+
M.render(buf, mode, parse)
4040
end
4141
end)
4242
end
@@ -46,20 +46,15 @@ end
4646
---@param mode string
4747
---@param parse boolean
4848
---@return 'invalid'|'disable'|'parsed'|'movement'
49-
function M.refresh(buf, mode, parse)
50-
-- Remove any existing marks if buffer is valid
51-
if not vim.api.nvim_buf_is_valid(buf) then
52-
return 'invalid'
53-
end
54-
vim.api.nvim_buf_clear_namespace(buf, M.namespace, 0, -1)
55-
56-
-- Check that buffer is associated with a valid window before window operations
49+
function M.render(buf, mode, parse)
50+
-- Check that buffer and associated window are valid
5751
local win = util.buf_to_win(buf)
58-
if not vim.api.nvim_win_is_valid(win) then
52+
if not vim.api.nvim_buf_is_valid(buf) or not vim.api.nvim_win_is_valid(win) then
5953
return 'invalid'
6054
end
55+
vim.api.nvim_buf_clear_namespace(buf, M.namespace, 0, -1)
6156

62-
if not M.should_render(buf, win, mode) then
57+
if not M.should_render(win, mode) then
6358
-- Set window options back to default
6459
for name, value in pairs(state.config.win_options) do
6560
util.set_win(win, name, value.default)
@@ -100,11 +95,10 @@ function M.refresh(buf, mode, parse)
10095
end
10196

10297
---@private
103-
---@param buf integer
10498
---@param win integer
10599
---@param mode string
106100
---@return boolean
107-
function M.should_render(buf, win, mode)
101+
function M.should_render(win, mode)
108102
if not state.enabled then
109103
return false
110104
end
@@ -114,23 +108,22 @@ function M.should_render(buf, win, mode)
114108
if not vim.tbl_contains(state.config.render_modes, mode) then
115109
return false
116110
end
117-
if util.file_size_mb(buf) > state.config.max_file_size then
118-
return false
119-
end
120111
return true
121112
end
122113

123114
---@private
124115
---@param buf integer
125116
---@return render.md.Mark[]
126117
function M.parse_buffer(buf)
127-
local marks = {}
128118
-- Make sure injections are processed
129119
local parser = vim.treesitter.get_parser(buf)
130-
parser:parse(true)
131-
-- Parse and cache marks
120+
if not parser:is_valid() then
121+
parser:parse(true)
122+
end
123+
-- Parse marks
124+
local marks = {}
132125
parser:for_each_tree(function(tree, language_tree)
133-
vim.list_extend(marks, M.parse(buf, language_tree:lang(), tree:root()))
126+
vim.list_extend(marks, M.parse_tree(buf, language_tree:lang(), tree:root()))
134127
end)
135128
return marks
136129
end
@@ -142,7 +135,7 @@ end
142135
---@param language string
143136
---@param root TSNode
144137
---@return render.md.Mark[]
145-
function M.parse(buf, language, root)
138+
function M.parse_tree(buf, language, root)
146139
logger.debug('language', language)
147140

148141
local marks = {}

0 commit comments

Comments
 (0)