Skip to content

Commit 91d40c2

Browse files
feat: add on.initial option called before adding marks to a buffer for the first time
## Details Request: #396 Adds `on.initial` function which is called only once for a buffer before marks are added on the first render cycle. Context is provided with the buffer and window id. Modify other callbacks called within the main UI loop to also take a window id, meaning `on.render` & `on.clear` are both provided with `win`, since it is a context table this breaks no existing usage. This can be used to make changes to LSP hover docs based on the users knowledge of how their plugins work but identifying buffers as LSP hover docs and applying changes is left up to the user.
1 parent 05e6a6d commit 91d40c2

File tree

7 files changed

+25
-11
lines changed

7 files changed

+25
-11
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ require('render-markdown').setup({
276276
on = {
277277
-- Called when plugin initially attaches to a buffer.
278278
attach = function() end,
279+
-- Called before adding marks to the buffer for the first time.
280+
initial = function() end,
279281
-- Called after plugin renders a buffer.
280282
render = function() end,
281283
-- Called after plugin clears a buffer.

doc/render-markdown.txt

Lines changed: 3 additions & 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 08
1+
*render-markdown.txt* For 0.11.0 Last change: 2025 April 11
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*
@@ -341,6 +341,8 @@ Default Configuration ~
341341
on = {
342342
-- Called when plugin initially attaches to a buffer.
343343
attach = function() end,
344+
-- Called before adding marks to the buffer for the first time.
345+
initial = function() end,
344346
-- Called after plugin renders a buffer.
345347
render = function() end,
346348
-- Called after plugin clears a buffer.

lua/render-markdown/config/on.lua

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
---@class (exact) render.md.callback.Config
2-
---@field attach fun(ctx: render.md.callback.Context)
3-
---@field render fun(ctx: render.md.callback.Context)
4-
---@field clear fun(ctx: render.md.callback.Context)
2+
---@field attach fun(ctx: render.md.callback.attach.Context)
3+
---@field initial fun(ctx: render.md.callback.render.Context)
4+
---@field render fun(ctx: render.md.callback.render.Context)
5+
---@field clear fun(ctx: render.md.callback.render.Context)
56

6-
---@class (exact) render.md.callback.Context
7+
---@class (exact) render.md.callback.attach.Context
78
---@field buf integer
89

10+
---@class (exact) render.md.callback.render.Context
11+
---@field buf integer
12+
---@field win integer
13+
914
local M = {}
1015

1116
---@param spec render.md.debug.ValidatorSpec
1217
function M.validate(spec)
1318
spec:type('attach', 'function')
19+
spec:type('initial', 'function')
1420
spec:type('render', 'function')
1521
spec:type('clear', 'function')
1622
spec:check()

lua/render-markdown/core/ui.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ function M.run_update(buf, win, change)
164164
local extmarks = buffer:get_marks()
165165
if initial then
166166
Compat.lsp_window_height(win, extmarks)
167+
state.on.initial({ buf = buf, win = win })
167168
end
168169
for _, extmark in ipairs(extmarks) do
169170
if extmark:get().conceal and extmark:inside(hidden) then
@@ -172,10 +173,10 @@ function M.run_update(buf, win, change)
172173
extmark:show(M.ns, buf)
173174
end
174175
end
175-
state.on.render({ buf = buf })
176+
state.on.render({ buf = buf, win = win })
176177
else
177178
M.clear(buf, buffer)
178-
state.on.clear({ buf = buf })
179+
state.on.clear({ buf = buf, win = win })
179180
end
180181
end
181182

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

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

lua/render-markdown/init.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ M.default = {
144144
on = {
145145
-- Called when plugin initially attaches to a buffer.
146146
attach = function() end,
147+
-- Called before adding marks to the buffer for the first time.
148+
initial = function() end,
147149
-- Called after plugin renders a buffer.
148150
render = function() end,
149151
-- Called after plugin clears a buffer.

lua/render-markdown/types.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,10 @@
199199
---@field highlight? string
200200

201201
---@class (exact) render.md.callback.UserConfig
202-
---@field attach? fun(ctx: render.md.callback.Context)
203-
---@field render? fun(ctx: render.md.callback.Context)
204-
---@field clear? fun(ctx: render.md.callback.Context)
202+
---@field attach? fun(ctx: render.md.callback.attach.Context)
203+
---@field initial? fun(ctx: render.md.callback.render.Context)
204+
---@field render? fun(ctx: render.md.callback.render.Context)
205+
---@field clear? fun(ctx: render.md.callback.render.Context)
205206

206207
---@class (exact) render.md.overrides.UserConfig
207208
---@field buflisted? table<boolean, render.md.buffer.UserConfig>

0 commit comments

Comments
 (0)