Skip to content

Commit 5eaa6c2

Browse files
chore(refactor): add config module for custom handlers, reduce global state
## Details Add `custom_handlers` module under config directory for validation and setting default value. Moves `on`, `completions`, `ignore`, and `change_events` out of the global state module. Leaves only `enabled` & `file_types` available globally, I think those will have to stick around.
1 parent 85edcba commit 5eaa6c2

File tree

12 files changed

+86
-41
lines changed

12 files changed

+86
-41
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -850,9 +850,10 @@ require('render-markdown').setup({
850850
-- Override for different filetype values, @see :h 'filetype'.
851851
filetype = {},
852852
},
853-
-- Mapping from treesitter language to user defined handlers.
854-
-- @see [Custom Handlers](doc/custom-handlers.md)
855-
custom_handlers = {},
853+
custom_handlers = {
854+
-- Mapping from treesitter language to user defined handlers.
855+
-- @see [Custom Handlers](doc/custom-handlers.md)
856+
},
856857
})
857858
```
858859

doc/render-markdown.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -915,9 +915,10 @@ Default Configuration ~
915915
-- Override for different filetype values, @see :h 'filetype'.
916916
filetype = {},
917917
},
918-
-- Mapping from treesitter language to user defined handlers.
919-
-- @see [Custom Handlers](doc/custom-handlers.md)
920-
custom_handlers = {},
918+
custom_handlers = {
919+
-- Mapping from treesitter language to user defined handlers.
920+
-- @see [Custom Handlers](doc/custom-handlers.md)
921+
},
921922
})
922923
<
923924

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---@class render.md.custom.handlers
2+
local M = {}
3+
4+
---@type table<string, render.md.Handler>
5+
M.default = {
6+
-- Mapping from treesitter language to user defined handlers.
7+
-- @see [Custom Handlers](doc/custom-handlers.md)
8+
}
9+
10+
---@param spec render.md.debug.ValidatorSpec
11+
function M.validate(spec)
12+
spec:each(function(handler)
13+
handler:type('extends', { 'boolean', 'nil' })
14+
handler:type('parse', 'function')
15+
handler:check()
16+
end)
17+
spec:check()
18+
end
19+
20+
return M

lua/render-markdown/core/ui.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ function Cache.get(buf)
4141
end
4242

4343
---@class render.md.ui.Config
44+
---@field on render.md.callback.Config
4445
---@field custom_handlers table<string, render.md.Handler>
4546

4647
---@class render.md.Ui
@@ -152,7 +153,7 @@ function M.run_update(buf, win, change)
152153
local extmarks = buffer:get_marks()
153154
if initial then
154155
Compat.fix_lsp_window(buf, win, extmarks)
155-
state.on.initial({ buf = buf, win = win })
156+
M.config.on.initial({ buf = buf, win = win })
156157
end
157158
for _, extmark in ipairs(extmarks) do
158159
if extmark:get().conceal and extmark:overlaps(hidden) then
@@ -161,10 +162,10 @@ function M.run_update(buf, win, change)
161162
extmark:show(M.ns, buf)
162163
end
163164
end
164-
state.on.render({ buf = buf, win = win })
165+
M.config.on.render({ buf = buf, win = win })
165166
else
166167
M.clear(buf, buffer)
167-
state.on.clear({ buf = buf, win = win })
168+
M.config.on.clear({ buf = buf, win = win })
168169
end
169170
end
170171

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

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

lua/render-markdown/init.lua

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,7 @@ M.default = {
100100
html = require('render-markdown.config.html').default,
101101
win_options = require('render-markdown.config.win_options').default,
102102
overrides = require('render-markdown.config.overrides').default,
103-
-- Mapping from treesitter language to user defined handlers.
104-
-- @see [Custom Handlers](doc/custom-handlers.md)
105-
custom_handlers = {},
103+
custom_handlers = require('render-markdown.config.custom_handlers').default,
106104
}
107105

108106
---@param opts? render.md.UserConfig

lua/render-markdown/integ/blink.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---@module 'blink.cmp'
22

33
local source = require('render-markdown.integ.source')
4-
local state = require('render-markdown.state')
54

65
---@class render.md.blink.Source: blink.cmp.Source
76
local Source = {}
@@ -61,6 +60,7 @@ function Source.setup()
6160
name = 'RenderMarkdown',
6261
module = 'render-markdown.integ.blink',
6362
})
63+
local state = require('render-markdown.state')
6464
for _, file_type in ipairs(state.file_types) do
6565
pcall(blink.add_filetype_source, file_type, id)
6666
end

lua/render-markdown/integ/source.lua

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,19 @@ local markers = {
1010
block_quote_marker = '>',
1111
}
1212

13+
---@class render.md.source.Config
14+
---@field completions render.md.completions.Config
15+
1316
---@class render.md.Source
17+
---@field private config render.md.source.Config
1418
local M = {}
1519

20+
---called from state on setup
21+
---@param config render.md.source.Config
22+
function M.setup(config)
23+
M.config = config
24+
end
25+
1626
---@return boolean
1727
function M.enabled()
1828
return manager.attached(Env.buf.current())
@@ -47,7 +57,7 @@ function M.items(buf, row, col)
4757

4858
local items = {}
4959
local config = state.get(buf)
50-
local filter = state.completions.filter
60+
local filter = M.config.completions.filter
5161
local prefix = Str.spaces('end', marker) == 0 and ' ' or ''
5262
if node:type() == 'block_quote' then
5363
for _, value in pairs(config.callout) do

lua/render-markdown/manager.lua

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,25 @@ local ui = require('render-markdown.core.ui')
66
---@type integer[]
77
local buffers = {}
88

9+
---@class render.md.manager.Config
10+
---@field ignore fun(buf: integer): boolean
11+
---@field change_events string[]
12+
---@field on render.md.callback.Config
13+
---@field completions render.md.completions.Config
14+
915
---@class render.md.Manager
16+
---@field private config render.md.manager.Config
1017
local M = {}
1118

1219
---@private
1320
M.group = vim.api.nvim_create_augroup('RenderMarkdown', {})
1421

22+
---called from state on setup
23+
---@param config render.md.manager.Config
24+
function M.setup(config)
25+
M.config = config
26+
end
27+
1528
---called from plugin directory
1629
function M.init()
1730
-- lazy loading: ignores current buffer as FileType event already executed
@@ -86,13 +99,13 @@ function M.attach(buf)
8699
end
87100

88101
local config = state.get(buf)
89-
state.on.attach({ buf = buf })
102+
M.config.on.attach({ buf = buf })
90103
require('render-markdown.integ.ts').init()
91-
if state.completions.lsp.enabled then
104+
if M.config.completions.lsp.enabled then
92105
require('render-markdown.integ.lsp').setup()
93-
elseif state.completions.blink.enabled then
106+
elseif M.config.completions.blink.enabled then
94107
require('render-markdown.integ.blink').setup()
95-
elseif state.completions.coq.enabled then
108+
elseif M.config.completions.coq.enabled then
96109
require('render-markdown.integ.coq').setup()
97110
else
98111
require('render-markdown.integ.cmp').setup()
@@ -107,7 +120,7 @@ function M.attach(buf)
107120
'WinScrolled',
108121
}
109122
local change_events = { 'DiffUpdated', 'ModeChanged', 'TextChanged' }
110-
vim.list_extend(change_events, state.change_events)
123+
vim.list_extend(change_events, M.config.change_events)
111124
if config:render('i') then
112125
vim.list_extend(events, { 'CursorHoldI', 'CursorMovedI' })
113126
vim.list_extend(change_events, { 'TextChangedI' })
@@ -175,7 +188,7 @@ function M.should_attach(buf)
175188
return false
176189
end
177190

178-
if state.ignore(buf) then
191+
if M.config.ignore(buf) then
179192
log.buf('info', 'attach', buf, 'skip', 'user ignore')
180193
return false
181194
end

lua/render-markdown/state.lua

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ local Cache = {}
88
---@field private config render.md.Config
99
---@field enabled boolean
1010
---@field file_types string[]
11-
---@field ignore fun(buf: integer): boolean
12-
---@field change_events string[]
13-
---@field on render.md.callback.Config
14-
---@field completions render.md.completions.Config
1511
local M = {}
1612

1713
---called from init on setup
@@ -20,17 +16,23 @@ function M.setup(config)
2016
M.config = config
2117
M.enabled = config.enabled
2218
M.file_types = config.file_types
23-
M.ignore = config.ignore
24-
M.change_events = config.change_events
25-
M.on = config.on
26-
M.completions = config.completions
19+
require('render-markdown.manager').setup({
20+
ignore = config.ignore,
21+
change_events = config.change_events,
22+
on = config.on,
23+
completions = config.completions,
24+
})
2725
require('render-markdown.core.log').setup({
2826
level = config.log_level,
2927
runtime = config.log_runtime,
3028
})
3129
require('render-markdown.core.ui').setup({
30+
on = config.on,
3231
custom_handlers = config.custom_handlers,
3332
})
33+
require('render-markdown.integ.source').setup({
34+
completions = config.completions,
35+
})
3436
require('render-markdown.integ.ts').setup({
3537
file_types = config.file_types,
3638
injections = config.injections,
@@ -132,14 +134,7 @@ function M.validate()
132134
spec:nested('on', require('render-markdown.config.on').validate)
133135
spec:nested('completions', require('render-markdown.config.completions').validate)
134136
spec:nested('overrides', require('render-markdown.config.overrides').validate)
135-
spec:nested('custom_handlers', function(handlers)
136-
handlers:each(function(handler)
137-
handler:type('extends', { 'boolean', 'nil' })
138-
handler:type('parse', 'function')
139-
handler:check()
140-
end)
141-
handlers:check()
142-
end)
137+
spec:nested('custom_handlers', require('render-markdown.config.custom_handlers').validate)
143138
spec:check()
144139
return validator:get_errors()
145140
end

0 commit comments

Comments
 (0)