Skip to content

Commit 7493db6

Browse files
feat: allow render_modes to be a boolean
## Details Allow the render modes parameter to be a boolean rather than a list of modes. When it is a boolean the mode of the current buffer stops playing a role and rendering either always occurs or never occurs. Using false does not make sense in this case but by setting it to true one can avoid adding all the modes to the list. Made the main buffer Config a class so that logic to determine things like should render and handling the different types can be centralized.
1 parent 06c8d76 commit 7493db6

File tree

8 files changed

+81
-64
lines changed

8 files changed

+81
-64
lines changed

lua/render-markdown/components.lua renamed to lua/render-markdown/config.lua

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,40 @@
44

55
---@class render.md.buffer.Config: render.md.BufferConfig
66
---@field component render.md.component.Config
7-
8-
---@class render.md.component.Resolver
9-
local M = {}
7+
local Config = {}
8+
Config.__index = Config
109

1110
---@param config render.md.BufferConfig
1211
---@return render.md.buffer.Config
13-
function M.resolve(config)
12+
function Config.new(config)
1413
---@type render.md.component.Config
1514
local component = {
16-
callout = M.normalize(config.callout),
17-
checkbox = M.normalize(config.checkbox.custom),
15+
callout = Config.normalize(config.callout),
16+
checkbox = Config.normalize(config.checkbox.custom),
1817
}
19-
return vim.tbl_deep_extend('force', { component = component }, config)
18+
local instance = vim.tbl_deep_extend('force', { component = component }, config)
19+
return setmetatable(instance, Config)
2020
end
2121

2222
---@private
2323
---@param components table<string, render.md.CustomComponent>
24-
function M.normalize(components)
24+
function Config.normalize(components)
2525
local result = {}
2626
for _, component in pairs(components) do
2727
result[component.raw:lower()] = component
2828
end
2929
return result
3030
end
3131

32-
return M
32+
---@param mode string
33+
---@return boolean
34+
function Config:render(mode)
35+
local modes = self.render_modes
36+
if type(modes) == 'table' then
37+
return vim.tbl_contains(modes, mode)
38+
else
39+
return modes
40+
end
41+
end
42+
43+
return Config

lua/render-markdown/core/ui.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ function M.next_state(config, win)
121121
if not util.view(win).leftcol == 0 then
122122
return 'default'
123123
end
124-
if not vim.tbl_contains(config.render_modes, vim.fn.mode(true)) then
124+
if not config:render(vim.fn.mode(true)) then
125125
return 'default'
126126
end
127127
return 'rendered'

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.0.6'
7+
M.version = '7.0.7'
88

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

lua/render-markdown/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ local M = {}
157157
---@field public enabled? boolean
158158
---@field public max_file_size? number
159159
---@field public debounce? integer
160-
---@field public render_modes? string[]
160+
---@field public render_modes? string[]|boolean
161161
---@field public anti_conceal? render.md.UserAntiConceal
162162
---@field public heading? render.md.UserHeading
163163
---@field public code? render.md.UserCode

lua/render-markdown/manager.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function M.attach(buf)
5959
local config = state.get_config(buf)
6060
local events = { 'BufWinEnter', 'BufLeave', 'CursorHold', 'CursorMoved' }
6161
local change_events = { 'ModeChanged', 'TextChanged' }
62-
if vim.tbl_contains(config.render_modes, 'i') then
62+
if config:render('i') then
6363
vim.list_extend(events, { 'CursorHoldI', 'CursorMovedI' })
6464
vim.list_extend(change_events, { 'TextChangedI' })
6565
end

lua/render-markdown/state.lua

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
local components = require('render-markdown.components')
1+
local Config = require('render-markdown.config')
22
local log = require('render-markdown.core.log')
33
local presets = require('render-markdown.presets')
44
local treesitter = require('render-markdown.core.treesitter')
@@ -78,7 +78,7 @@ function M.get_config(buf)
7878
buf_config = vim.tbl_deep_extend('force', buf_config, override)
7979
end
8080
end
81-
config = components.resolve(buf_config)
81+
config = Config.new(buf_config)
8282
configs[buf] = config
8383
end
8484
return config
@@ -113,45 +113,49 @@ end
113113

114114
---@return string[]
115115
function M.validate()
116-
local errors = {}
116+
---@param types type[]
117+
---@param nilable boolean
118+
---@return string
119+
local function handle_types(types, nilable)
120+
if nilable then
121+
table.insert(types, 'nil')
122+
end
123+
return #types == 0 and '' or (' or type ' .. vim.inspect(types))
124+
end
117125

118126
---@param value any
119-
---@param valid_values string[]
120-
---@param valid_types type[]
127+
---@param values string[]
128+
---@param types type[]
121129
---@param nilable boolean
122130
---@return vim.validate.Spec
123-
local function one_of(value, valid_values, valid_types, nilable)
124-
if nilable then
125-
table.insert(valid_types, 'nil')
126-
end
131+
local function one_of(value, values, types, nilable)
132+
local suffix = handle_types(types, nilable)
127133
return {
128134
value,
129135
function(v)
130-
return vim.tbl_contains(valid_values, v) or vim.tbl_contains(valid_types, type(v))
136+
return vim.tbl_contains(values, v) or vim.tbl_contains(types, type(v))
131137
end,
132-
'one of ' .. vim.inspect(valid_values) .. ' or type ' .. vim.inspect(valid_types),
138+
'one of ' .. vim.inspect(values) .. suffix,
133139
}
134140
end
135141

136142
---@param value any
137-
---@param valid_values string[]
143+
---@param values string[]
144+
---@param types type[]
138145
---@param nilable boolean
139146
---@return vim.validate.Spec
140-
local function one_or_array_of(value, valid_values, nilable)
141-
local description = 'one or array of ' .. vim.inspect(valid_values)
142-
if nilable then
143-
description = description .. ' or nil'
144-
end
147+
local function one_or_array_of(value, values, types, nilable)
148+
local suffix = handle_types(types, nilable)
145149
return {
146150
value,
147151
function(v)
148-
if v == nil then
149-
return nilable
152+
if vim.tbl_contains(types, type(v)) then
153+
return true
150154
elseif type(v) == 'string' then
151-
return vim.tbl_contains(valid_values, v)
155+
return vim.tbl_contains(values, v)
152156
elseif type(v) == 'table' then
153157
for i, item in ipairs(v) do
154-
if not vim.tbl_contains(valid_values, item) then
158+
if not vim.tbl_contains(values, item) then
155159
return false, string.format('Index %d is %s', i, item)
156160
end
157161
end
@@ -160,23 +164,21 @@ function M.validate()
160164
return false
161165
end
162166
end,
163-
description,
167+
'one or array of ' .. vim.inspect(values) .. suffix,
164168
}
165169
end
166170

167-
---@param value string[]
171+
---@param value any
172+
---@param types type[]
168173
---@param nilable boolean
169174
---@return vim.validate.Spec
170-
local function string_array(value, nilable)
171-
local description = 'string array'
172-
if nilable then
173-
description = description .. ' or nil'
174-
end
175+
local function string_array(value, types, nilable)
176+
local suffix = handle_types(types, nilable)
175177
return {
176178
value,
177179
function(v)
178-
if v == nil then
179-
return nilable
180+
if vim.tbl_contains(types, type(v)) then
181+
return true
180182
elseif type(v) == 'table' then
181183
for i, item in ipairs(v) do
182184
if type(item) ~= 'string' then
@@ -188,10 +190,12 @@ function M.validate()
188190
return false
189191
end
190192
end,
191-
description,
193+
'string array' .. suffix,
192194
}
193195
end
194196

197+
local errors = {}
198+
195199
---@param suffix string
196200
---@param input table<string, any>
197201
---@param opts table<string, vim.validate.Spec>
@@ -227,18 +231,18 @@ function M.validate()
227231
enabled = { heading.enabled, 'boolean', nilable },
228232
sign = { heading.sign, 'boolean', nilable },
229233
position = one_of(heading.position, { 'overlay', 'inline' }, {}, nilable),
230-
icons = string_array(heading.icons, nilable),
231-
signs = string_array(heading.signs, nilable),
232-
width = one_or_array_of(heading.width, { 'full', 'block' }, nilable),
234+
icons = string_array(heading.icons, {}, nilable),
235+
signs = string_array(heading.signs, {}, nilable),
236+
width = one_or_array_of(heading.width, { 'full', 'block' }, {}, nilable),
233237
left_pad = { heading.left_pad, 'number', nilable },
234238
right_pad = { heading.right_pad, 'number', nilable },
235239
min_width = { heading.min_width, 'number', nilable },
236240
border = { heading.border, 'boolean', nilable },
237241
border_prefix = { heading.border_prefix, 'boolean', nilable },
238242
above = { heading.above, 'string', nilable },
239243
below = { heading.below, 'string', nilable },
240-
backgrounds = string_array(heading.backgrounds, nilable),
241-
foregrounds = string_array(heading.foregrounds, nilable),
244+
backgrounds = string_array(heading.backgrounds, {}, nilable),
245+
foregrounds = string_array(heading.foregrounds, {}, nilable),
242246
})
243247
end
244248

@@ -250,7 +254,7 @@ function M.validate()
250254
style = one_of(code.style, { 'full', 'normal', 'language', 'none' }, {}, nilable),
251255
position = one_of(code.position, { 'left', 'right' }, {}, nilable),
252256
language_pad = { code.language_pad, 'number', nilable },
253-
disable_background = string_array(code.disable_background, nilable),
257+
disable_background = string_array(code.disable_background, {}, nilable),
254258
width = one_of(code.width, { 'full', 'block' }, {}, nilable),
255259
left_pad = { code.left_pad, 'number', nilable },
256260
right_pad = { code.right_pad, 'number', nilable },
@@ -277,7 +281,7 @@ function M.validate()
277281
if bullet ~= nil then
278282
append_errors(path .. '.bullet', bullet, {
279283
enabled = { bullet.enabled, 'boolean', nilable },
280-
icons = string_array(bullet.icons, nilable),
284+
icons = string_array(bullet.icons, {}, nilable),
281285
left_pad = { bullet.left_pad, 'number', nilable },
282286
right_pad = { bullet.right_pad, 'number', nilable },
283287
highlight = { bullet.highlight, 'string', nilable },
@@ -336,7 +340,7 @@ function M.validate()
336340
style = one_of(pipe_table.style, { 'full', 'normal', 'none' }, {}, nilable),
337341
cell = one_of(pipe_table.cell, { 'padded', 'raw', 'overlay' }, {}, nilable),
338342
min_width = { pipe_table.min_width, 'number', nilable },
339-
border = string_array(pipe_table.border, nilable),
343+
border = string_array(pipe_table.border, {}, nilable),
340344
alignment_indicator = { pipe_table.alignment_indicator, 'string', nilable },
341345
head = { pipe_table.head, 'string', nilable },
342346
row = { pipe_table.row, 'string', nilable },
@@ -408,7 +412,7 @@ function M.validate()
408412
enabled = { config.enabled, 'boolean' },
409413
max_file_size = { config.max_file_size, 'number' },
410414
debounce = { config.debounce, 'number' },
411-
render_modes = string_array(config.render_modes, false),
415+
render_modes = string_array(config.render_modes, { 'boolean' }, false),
412416
anti_conceal = { config.anti_conceal, 'table' },
413417
heading = { config.heading, 'table' },
414418
code = { config.code, 'table' },
@@ -427,7 +431,7 @@ function M.validate()
427431
markdown_quote_query = { config.markdown_quote_query, 'string' },
428432
inline_query = { config.inline_query, 'string' },
429433
log_level = one_of(config.log_level, { 'debug', 'error' }, {}, false),
430-
file_types = string_array(config.file_types, false),
434+
file_types = string_array(config.file_types, {}, false),
431435
injections = { config.injections, 'table' },
432436
latex = { config.latex, 'table' },
433437
overrides = { config.overrides, 'table' },
@@ -465,7 +469,7 @@ function M.validate()
465469
enabled = { override.enabled, 'boolean', true },
466470
max_file_size = { override.max_file_size, 'number', true },
467471
debounce = { override.debounce, 'number', true },
468-
render_modes = string_array(override.render_modes, true),
472+
render_modes = string_array(override.render_modes, {}, true),
469473
anti_conceal = { override.anti_conceal, 'table', true },
470474
heading = { override.heading, 'table', true },
471475
code = { override.code, 'table', true },

lua/render-markdown/types.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132
---@field public enabled boolean
133133
---@field public max_file_size number
134134
---@field public debounce integer
135-
---@field public render_modes string[]
135+
---@field public render_modes string[]|boolean
136136
---@field public anti_conceal render.md.AntiConceal
137137
---@field public heading render.md.Heading
138138
---@field public code render.md.Code

tests/state_spec.lua

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,24 @@ describe('state', function()
7070
)
7171

7272
eq(
73-
{ prefix .. '.log_level: expected one of { "debug", "error" } or type {}, got invalid' },
73+
{ prefix .. '.log_level: expected one of { "debug", "error" }, got invalid' },
7474
---@diagnostic disable-next-line: assign-type-mismatch
7575
validate({ log_level = 'invalid' })
7676
)
7777

7878
eq(
79-
{ prefix .. '.render_modes: expected string array, got true' },
79+
{ prefix .. '.render_modes: expected string array or type { "boolean" }, got invalid' },
8080
---@diagnostic disable-next-line: assign-type-mismatch
81-
validate({ render_modes = true })
81+
validate({ render_modes = 'invalid' })
8282
)
8383

84-
---@diagnostic disable-next-line: assign-type-mismatch
85-
local errors = validate({ render_modes = { 1, 2 } })
86-
eq(1, #errors)
87-
eq(true, vim.startswith(errors[1], prefix .. '.render_modes: expected string array, got '))
88-
eq(true, vim.endswith(errors[1], 'Info: Index 1 is number'))
84+
local int_render_modes = { 1, 2 }
85+
eq({
86+
prefix
87+
.. '.render_modes: expected string array or type { "boolean" }, got '
88+
.. tostring(int_render_modes)
89+
.. '. Info: Index 1 is number',
90+
}, validate({ render_modes = int_render_modes }))
8991

9092
eq(
9193
{ prefix .. '.callout.new.highlight: expected string, got nil' },

0 commit comments

Comments
 (0)