Skip to content

Commit 3484e3e

Browse files
chore(refactor): move creating marks to handlers module
## Details Makes the eventual move to async parsing a little easier if all the logic to generate marks is wrapped up in its own module outside the rest of the UI logic. All that's needed is a `context` and the `parser`.
1 parent b2d857c commit 3484e3e

File tree

6 files changed

+85
-59
lines changed

6 files changed

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

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

lua/render-markdown/core/handlers.lua

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
local log = require('render-markdown.core.log')
2+
3+
---@class render.md.handlers.Config
4+
---@field custom table<string, render.md.Handler>
5+
6+
---@class render.md.Handlers
7+
---@field private config render.md.handlers.Config
8+
local M = {}
9+
10+
---@private
11+
---@type table<string, render.md.Handler>
12+
M.builtin = {
13+
html = require('render-markdown.handler.html'),
14+
latex = require('render-markdown.handler.latex'),
15+
markdown = require('render-markdown.handler.markdown'),
16+
markdown_inline = require('render-markdown.handler.markdown_inline'),
17+
}
18+
19+
---called from state on setup
20+
---@param config render.md.handlers.Config
21+
function M.setup(config)
22+
M.config = config
23+
end
24+
25+
---@param context render.md.request.Context
26+
---@param parser vim.treesitter.LanguageTree
27+
---@return render.md.Mark[]
28+
function M.run(context, parser)
29+
local marks = {} ---@type render.md.Mark[]
30+
-- parse markdown after other nodes to get accurate state
31+
local markdown = {} ---@type render.md.handler.Context[]
32+
parser:for_each_tree(function(tree, language_tree)
33+
---@type render.md.handler.Context
34+
local ctx = { buf = context.buf, root = tree:root() }
35+
local language = language_tree:lang()
36+
if language == 'markdown' then
37+
markdown[#markdown + 1] = ctx
38+
else
39+
vim.list_extend(marks, M.tree(context, ctx, language))
40+
end
41+
end)
42+
for _, ctx in ipairs(markdown) do
43+
vim.list_extend(marks, M.tree(context, ctx, 'markdown'))
44+
end
45+
return marks
46+
end
47+
48+
---Run custom & builtin handlers when available. Custom handler is always
49+
---executed, builtin handler is skipped if custom does not specify extends.
50+
---@private
51+
---@param context render.md.request.Context
52+
---@param ctx render.md.handler.Context
53+
---@param language string
54+
---@return render.md.Mark[]
55+
function M.tree(context, ctx, language)
56+
log.buf('debug', 'language', ctx.buf, language)
57+
if not context.view:overlaps(ctx.root) then
58+
return {}
59+
end
60+
local marks = {} ---@type render.md.Mark[]
61+
local custom = M.config.custom[language]
62+
if custom then
63+
log.buf('debug', 'handler', ctx.buf, 'custom')
64+
vim.list_extend(marks, custom.parse(ctx))
65+
if not custom.extends then
66+
return marks
67+
end
68+
end
69+
local builtin = M.builtin[language]
70+
if builtin then
71+
log.buf('debug', 'handler', ctx.buf, 'builtin')
72+
vim.list_extend(marks, builtin.parse(ctx))
73+
end
74+
return marks
75+
end
76+
77+
return M

lua/render-markdown/core/ui.lua

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,12 @@ local Context = require('render-markdown.request.context')
44
local Env = require('render-markdown.lib.env')
55
local Extmark = require('render-markdown.lib.extmark')
66
local Iter = require('render-markdown.lib.iter')
7+
local handlers = require('render-markdown.core.handlers')
78
local log = require('render-markdown.core.log')
89
local state = require('render-markdown.state')
910

10-
---@type table<string, render.md.Handler>
11-
local builtin_handlers = {
12-
html = require('render-markdown.handler.html'),
13-
latex = require('render-markdown.handler.latex'),
14-
markdown = require('render-markdown.handler.markdown'),
15-
markdown_inline = require('render-markdown.handler.markdown_inline'),
16-
}
17-
1811
---@class render.md.ui.Config
1912
---@field on render.md.on.Config
20-
---@field custom_handlers table<string, render.md.Handler>
2113

2214
---@class render.md.Ui
2315
---@field private config render.md.ui.Config
@@ -165,53 +157,8 @@ function M.parse_buffer(buf, win, config, mode)
165157
local context = Context.start(buf, win, config, mode)
166158
-- make sure injections are processed
167159
context.view:parse(parser)
168-
-- parse markdown after other nodes to get accurate state
169-
local marks = {} ---@type render.md.Mark[]
170-
local markdown = {} ---@type render.md.handler.Context[]
171-
parser:for_each_tree(function(tree, language_tree)
172-
local language = language_tree:lang()
173-
---@type render.md.handler.Context
174-
local ctx = { buf = buf, root = tree:root() }
175-
if language == 'markdown' then
176-
markdown[#markdown + 1] = ctx
177-
else
178-
vim.list_extend(marks, M.parse_tree(context, ctx, language))
179-
end
180-
end)
181-
for _, ctx in ipairs(markdown) do
182-
vim.list_extend(marks, M.parse_tree(context, ctx, 'markdown'))
183-
end
160+
local marks = handlers.run(context, parser)
184161
return Iter.list.map(marks, Extmark.new)
185162
end
186163

187-
---Run user & builtin handlers when available. User handler is always executed,
188-
---builtin handler is skipped if user handler does not specify extends.
189-
---@private
190-
---@param context render.md.request.Context
191-
---@param ctx render.md.handler.Context
192-
---@param language string
193-
---@return render.md.Mark[]
194-
function M.parse_tree(context, ctx, language)
195-
log.buf('debug', 'language', ctx.buf, language)
196-
if not context.view:overlaps(ctx.root) then
197-
return {}
198-
end
199-
200-
local marks = {}
201-
local user = M.config.custom_handlers[language]
202-
if user then
203-
log.buf('debug', 'handler', ctx.buf, 'user')
204-
vim.list_extend(marks, user.parse(ctx))
205-
if not user.extends then
206-
return marks
207-
end
208-
end
209-
local builtin = builtin_handlers[language]
210-
if builtin then
211-
log.buf('debug', 'handler', ctx.buf, 'builtin')
212-
vim.list_extend(marks, builtin.parse(ctx))
213-
end
214-
return marks
215-
end
216-
217164
return M

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

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

lua/render-markdown/request/context.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ function Context:percent(value, used)
135135
end
136136
end
137137

138-
---@class render.md.context.Manager
138+
---@class render.md.request.context.Manager
139139
local M = {}
140140

141141
---@private

lua/render-markdown/state.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ function M.setup(config)
1717
M.config = config
1818
M.enabled = config.enabled
1919
M.file_types = config.file_types
20+
require('render-markdown.core.handlers').setup({
21+
custom = config.custom_handlers,
22+
})
2023
require('render-markdown.core.log').setup({
2124
level = config.log_level,
2225
runtime = config.log_runtime,
@@ -34,7 +37,6 @@ function M.setup(config)
3437
})
3538
require('render-markdown.core.ui').setup({
3639
on = config.on,
37-
custom_handlers = config.custom_handlers,
3840
})
3941
require('render-markdown.integ.source').setup({
4042
completions = config.completions,

0 commit comments

Comments
 (0)