Skip to content

Commit 0fe0f08

Browse files
Skip debounce when <= 0
## Details Main functional change is to directly skip debounce when it is set to 0. This should be the behavior when using timers anyway but this makes it a more obvious behavior. Also adds a check for vim.uv and falls back to vim.loop when checking file size. Minor other change, add a `log_runtime` option to allow us to print runtime of update function. Easier to see performance changes when developing if they just get printed. Users are welcome to it as well if they are curious.
1 parent ffbe9f2 commit 0fe0f08

File tree

9 files changed

+44
-13
lines changed

9 files changed

+44
-13
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ require('render-markdown').setup({
193193
-- The level of logs to write to file: vim.fn.stdpath('state') .. '/render-markdown.log'
194194
-- Only intended to be used for plugin development / debugging
195195
log_level = 'error',
196+
-- Print runtime of main update method
197+
-- Only intended to be used for plugin development / debugging
198+
log_runtime = false,
196199
-- Filetypes this plugin will run on
197200
file_types = { 'markdown' },
198201
-- Out of the box language injections for known filetypes that allow markdown to be

benches/util.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ function M.insert_mode()
3434
end
3535

3636
---@private
37-
---@param f fun()
37+
---@param callback fun()
3838
---@return number
39-
function M.time(f)
39+
function M.time(callback)
4040
local start = vim.uv.hrtime()
41-
f()
41+
callback()
4242
vim.wait(0)
4343
return (vim.uv.hrtime() - start) / 1e+6
4444
end

doc/render-markdown.txt

Lines changed: 4 additions & 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 26
1+
*render-markdown.txt* For 0.10.0 Last change: 2024 September 27
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*
@@ -241,6 +241,9 @@ Default Configuration ~
241241
-- The level of logs to write to file: vim.fn.stdpath('state') .. '/render-markdown.log'
242242
-- Only intended to be used for plugin development / debugging
243243
log_level = 'error',
244+
-- Print runtime of main update method
245+
-- Only intended to be used for plugin development / debugging
246+
log_runtime = false,
244247
-- Filetypes this plugin will run on
245248
file_types = { 'markdown' },
246249
-- Out of the box language injections for known filetypes that allow markdown to be

lua/render-markdown/core/ui.lua

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,20 @@ function M.debounce_update(buf, win, event, change)
8181

8282
local config, buffer_state = state.get(buf), Cache.get(buf)
8383

84-
if not change and Context.contains_range(buf, win) then
85-
vim.schedule(function()
86-
M.update(buf, win, false)
87-
end)
84+
-- Need to parse when things change or we have not parsed the visible range yet
85+
local parse = change or not Context.contains_range(buf, win)
86+
87+
local update = function()
88+
M.update(buf, win, parse)
89+
end
90+
if parse and state.log_runtime then
91+
update = util.wrap_runtime(update)
92+
end
93+
94+
if parse and config.debounce > 0 then
95+
buffer_state:debounce(config.debounce, update)
8896
else
89-
buffer_state:debounce(config.debounce, function()
90-
M.update(buf, win, true)
91-
end)
97+
vim.schedule(update)
9298
end
9399
end
94100

lua/render-markdown/core/util.lua

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,23 @@ end
9090
---@return number
9191
function M.file_size_mb(file)
9292
local ok, stats = pcall(function()
93-
return vim.uv.fs_stat(file)
93+
return (vim.uv or vim.loop).fs_stat(file)
9494
end)
9595
if not (ok and stats) then
9696
return 0
9797
end
9898
return stats.size / (1024 * 1024)
9999
end
100100

101+
---@param callback fun()
102+
---@return fun()
103+
function M.wrap_runtime(callback)
104+
return function()
105+
local start_time = (vim.uv or vim.loop).hrtime()
106+
callback()
107+
local end_time = (vim.uv or vim.loop).hrtime()
108+
vim.print(string.format('Runtime (ms): %.1f', (end_time - start_time) / 1e+6))
109+
end
110+
end
111+
101112
return M

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.2.0'
7+
M.version = '7.2.1'
88

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

lua/render-markdown/init.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ local M = {}
188188
---@field public markdown_quote_query? string
189189
---@field public inline_query? string
190190
---@field public log_level? render.md.config.LogLevel
191+
---@field public log_runtime? boolean
191192
---@field public file_types? string[]
192193
---@field public injections? table<string, render.md.UserInjection>
193194
---@field public latex? render.md.UserLatex
@@ -264,6 +265,9 @@ M.default_config = {
264265
-- The level of logs to write to file: vim.fn.stdpath('state') .. '/render-markdown.log'
265266
-- Only intended to be used for plugin development / debugging
266267
log_level = 'error',
268+
-- Print runtime of main update method
269+
-- Only intended to be used for plugin development / debugging
270+
log_runtime = false,
267271
-- Filetypes this plugin will run on
268272
file_types = { 'markdown' },
269273
-- Out of the box language injections for known filetypes that allow markdown to be

lua/render-markdown/state.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ local configs = {}
1010
---@class render.md.State
1111
---@field private config render.md.Config
1212
---@field enabled boolean
13+
---@field log_runtime boolean
1314
---@field file_types string[]
1415
---@field latex render.md.Latex
1516
---@field custom_handlers table<string, render.md.Handler>
@@ -42,6 +43,7 @@ function M.setup(default_config, user_config)
4243

4344
M.config = config
4445
M.enabled = config.enabled
46+
M.log_runtime = config.log_runtime
4547
M.file_types = config.file_types
4648
M.latex = config.latex
4749
M.custom_handlers = config.custom_handlers
@@ -245,6 +247,7 @@ function M.validate()
245247
:type({ 'anti_conceal', 'padding', 'heading', 'code', 'dash', 'bullet', 'checkbox' }, 'table')
246248
:type({ 'quote', 'pipe_table', 'callout', 'link', 'sign', 'indent', 'win_options' }, 'table')
247249
:list('render_modes', 'string', 'boolean')
250+
:type('log_runtime', 'boolean')
248251
:type({ 'markdown_query', 'markdown_quote_query', 'inline_query' }, 'string')
249252
:type({ 'injections', 'latex', 'overrides', 'custom_handlers' }, 'table')
250253
:list('file_types', 'string')

lua/render-markdown/types.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@
160160
---@field public markdown_quote_query string
161161
---@field public inline_query string
162162
---@field public log_level render.md.config.LogLevel
163+
---@field public log_runtime boolean
163164
---@field public file_types string[]
164165
---@field public injections table<string, render.md.Injection>
165166
---@field public latex render.md.Latex

0 commit comments

Comments
 (0)