Skip to content

Commit 350eccc

Browse files
committed
refactor(ALL): unify error handling and type checking
Details: - Uniformly use same `H.error` helper. - Switch from `vim.validate` for type checking to custom helper. This is mostly due to Neovim>=0.11 changing `vim.validate` signature from a table for multiple targets to several arguments for single taget. Although it is backwards compatible until Neovim 1.0, it already now generates `:checkhealth` warnings. As creating backwards compatible `vim.validate` wrappers is even more lines, using own helper with the similar signature as Neovim>=0.11 seems to be ok. Notes: - As a positive side effect, errors are prepended with "(mini.xxx)" prefix. - In cases when `vim.validate` is used with function predicate, apply it directly and use `H.error` on `false`. This resulted in extra line-removing diff.
1 parent 12ebac8 commit 350eccc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+924
-985
lines changed

lua/mini/ai.lua

+22-22
Original file line numberDiff line numberDiff line change
@@ -1132,29 +1132,24 @@ H.ns_id = {
11321132
-- Helper functionality =======================================================
11331133
-- Settings -------------------------------------------------------------------
11341134
H.setup_config = function(config)
1135-
-- General idea: if some table elements are not present in user-supplied
1136-
-- `config`, take them from default config
1137-
vim.validate({ config = { config, 'table', true } })
1135+
H.check_type('config', config, 'table', true)
11381136
config = vim.tbl_deep_extend('force', vim.deepcopy(H.default_config), config or {})
11391137

1140-
vim.validate({
1141-
custom_textobjects = { config.custom_textobjects, 'table', true },
1142-
mappings = { config.mappings, 'table' },
1143-
n_lines = { config.n_lines, 'number' },
1144-
search_method = { config.search_method, H.is_search_method },
1145-
silent = { config.silent, 'boolean' },
1146-
})
1138+
H.check_type('custom_textobjects', config.custom_textobjects, 'table', true)
11471139

1148-
vim.validate({
1149-
['mappings.around'] = { config.mappings.around, 'string' },
1150-
['mappings.inside'] = { config.mappings.inside, 'string' },
1151-
['mappings.around_next'] = { config.mappings.around_next, 'string' },
1152-
['mappings.inside_next'] = { config.mappings.inside_next, 'string' },
1153-
['mappings.around_last'] = { config.mappings.around_last, 'string' },
1154-
['mappings.inside_last'] = { config.mappings.inside_last, 'string' },
1155-
['mappings.goto_left'] = { config.mappings.goto_left, 'string' },
1156-
['mappings.goto_right'] = { config.mappings.goto_right, 'string' },
1157-
})
1140+
H.check_type('mappings', config.mappings, 'table')
1141+
H.check_type('mappings.around', config.mappings.around, 'string')
1142+
H.check_type('mappings.inside', config.mappings.inside, 'string')
1143+
H.check_type('mappings.around_next', config.mappings.around_next, 'string')
1144+
H.check_type('mappings.inside_next', config.mappings.inside_next, 'string')
1145+
H.check_type('mappings.around_last', config.mappings.around_last, 'string')
1146+
H.check_type('mappings.inside_last', config.mappings.inside_last, 'string')
1147+
H.check_type('mappings.goto_left', config.mappings.goto_left, 'string')
1148+
H.check_type('mappings.goto_right', config.mappings.goto_right, 'string')
1149+
1150+
H.check_type('n_lines', config.n_lines, 'number')
1151+
H.validate_search_method(config.search_method, 'search_method')
1152+
H.check_type('silent', config.silent, 'boolean')
11581153

11591154
return config
11601155
end
@@ -1956,6 +1951,13 @@ H.get_visual_region = function()
19561951
end
19571952

19581953
-- Utilities ------------------------------------------------------------------
1954+
H.error = function(msg) error('(mini.ai) ' .. msg, 0) end
1955+
1956+
H.check_type = function(name, val, ref, allow_nil)
1957+
if type(val) == ref or (ref == 'callable' and vim.is_callable(val)) or (allow_nil and val == nil) then return end
1958+
H.error(string.format('`%s` should be %s, not %s', name, ref, type(val)))
1959+
end
1960+
19591961
H.echo = function(msg, is_important)
19601962
if H.get_config().silent then return end
19611963

@@ -1984,8 +1986,6 @@ end
19841986

19851987
H.message = function(msg) H.echo(msg, true) end
19861988

1987-
H.error = function(msg) error(string.format('(mini.ai) %s', msg), 0) end
1988-
19891989
H.map = function(mode, lhs, rhs, opts)
19901990
if lhs == '' then return end
19911991
opts = vim.tbl_deep_extend('force', { silent = true }, opts or {})

lua/mini/align.lua

+19-29
Original file line numberDiff line numberDiff line change
@@ -1316,23 +1316,21 @@ H.indent_functions = {
13161316
-- Helper functionality =======================================================
13171317
-- Settings -------------------------------------------------------------------
13181318
H.setup_config = function(config)
1319-
-- General idea: if some table elements are not present in user-supplied
1320-
-- `config`, take them from default config
1321-
vim.validate({ config = { config, 'table', true } })
1319+
H.check_type('config', config, 'table', true)
13221320
config = vim.tbl_deep_extend('force', vim.deepcopy(H.default_config), config or {})
13231321

1324-
vim.validate({
1325-
mappings = { config.mappings, 'table' },
1326-
modifiers = { config.modifiers, H.is_valid_modifiers },
1327-
steps = { config.steps, H.is_valid_steps },
1328-
options = { config.options, 'table' },
1329-
silent = { config.silent, 'boolean' },
1330-
})
1322+
H.check_type('mappings', config.mappings, 'table')
1323+
H.check_type('mappings.start', config.mappings.start, 'string')
1324+
H.check_type('mappings.start_with_preview', config.mappings.start_with_preview, 'string')
13311325

1332-
vim.validate({
1333-
['mappings.start'] = { config.mappings.start, 'string' },
1334-
['mappings.start_with_preview'] = { config.mappings.start_with_preview, 'string' },
1335-
})
1326+
H.check_type('modifiers', config.modifiers, 'table')
1327+
for k, v in pairs(config.modifiers) do
1328+
if not vim.is_callable(v) then H.error(string.format('`modifiers[%s]` should be callable.', vim.inspect(k))) end
1329+
end
1330+
1331+
H.validate_steps(config.steps, 'steps')
1332+
H.check_type('options', config.options, 'table')
1333+
H.check_type('silent', config.silent, 'boolean')
13361334

13371335
return config
13381336
end
@@ -1616,19 +1614,6 @@ H.default_action_merge = function(parts, opts)
16161614
end
16171615

16181616
-- Work with modifiers --------------------------------------------------------
1619-
H.is_valid_modifiers = function(x, x_name)
1620-
x_name = x_name or 'config.modifiers'
1621-
1622-
if type(x) ~= 'table' then return false, string.format('`%s` should be table.', x_name) end
1623-
for k, v in pairs(x) do
1624-
if not vim.is_callable(v) then
1625-
return false, string.format('`%s[%s]` should be callable.', x_name, vim.inspect(k))
1626-
end
1627-
end
1628-
1629-
return true
1630-
end
1631-
16321617
H.make_filter_action = function(expr)
16331618
if expr == nil then return nil end
16341619
if expr == '' then expr = 'true' end
@@ -1932,6 +1917,13 @@ H.set_lines = function(start_row, end_row, replacement)
19321917
end
19331918

19341919
-- Utilities ------------------------------------------------------------------
1920+
H.error = function(msg) error('(mini.align) ' .. msg, 0) end
1921+
1922+
H.check_type = function(name, val, ref, allow_nil)
1923+
if type(val) == ref or (ref == 'callable' and vim.is_callable(val)) or (allow_nil and val == nil) then return end
1924+
H.error(string.format('`%s` should be %s, not %s', name, ref, type(val)))
1925+
end
1926+
19351927
H.echo = function(msg, add_to_history)
19361928
if H.get_config().silent then return end
19371929

@@ -1958,8 +1950,6 @@ H.unecho = function()
19581950
if H.cache.msg_shown then vim.cmd([[echo '' | redraw]]) end
19591951
end
19601952

1961-
H.error = function(msg) error(string.format('(mini.align) %s', msg), 0) end
1962-
19631953
H.map = function(mode, lhs, rhs, opts)
19641954
if lhs == '' then return end
19651955
opts = vim.tbl_deep_extend('force', { silent = true }, opts or {})

lua/mini/animate.lua

+32-60
Original file line numberDiff line numberDiff line change
@@ -1199,18 +1199,35 @@ H.animation_done_events = {
11991199
-- Helper functionality =======================================================
12001200
-- Settings -------------------------------------------------------------------
12011201
H.setup_config = function(config)
1202-
-- General idea: if some table elements are not present in user-supplied
1203-
-- `config`, take them from default config
1204-
vim.validate({ config = { config, 'table', true } })
1202+
H.check_type('config', config, 'table', true)
12051203
config = vim.tbl_deep_extend('force', vim.deepcopy(H.default_config), config or {})
12061204

1207-
vim.validate({
1208-
cursor = { config.cursor, H.is_config_cursor },
1209-
scroll = { config.scroll, H.is_config_scroll },
1210-
resize = { config.resize, H.is_config_resize },
1211-
open = { config.open, H.is_config_open },
1212-
close = { config.close, H.is_config_close },
1213-
})
1205+
H.check_type('cursor', config.cursor, 'table')
1206+
H.check_type('cursor.enable', config.cursor.enable, 'boolean')
1207+
H.check_type('cursor.timing', config.cursor.timing, 'callable')
1208+
H.check_type('cursor.path', config.cursor.path, 'callable')
1209+
1210+
H.check_type('scroll', config.scroll, 'table')
1211+
H.check_type('scroll.enable', config.scroll.enable, 'boolean')
1212+
H.check_type('scroll.timing', config.scroll.timing, 'callable')
1213+
H.check_type('scroll.subscroll', config.scroll.subscroll, 'callable')
1214+
1215+
H.check_type('resize', config.resize, 'table')
1216+
H.check_type('resize.enable', config.resize.enable, 'boolean')
1217+
H.check_type('resize.timing', config.resize.timing, 'callable')
1218+
H.check_type('resize.subresize', config.resize.subresize, 'callable')
1219+
1220+
H.check_type('open', config.open, 'table')
1221+
H.check_type('open.enable', config.open.enable, 'boolean')
1222+
H.check_type('open.timing', config.open.timing, 'callable')
1223+
H.check_type('open.winconfig', config.open.winconfig, 'callable')
1224+
H.check_type('open.winblend', config.open.winblend, 'callable')
1225+
1226+
H.check_type('close', config.close, 'table')
1227+
H.check_type('close.enable', config.close.enable, 'boolean')
1228+
H.check_type('close.timing', config.close.timing, 'callable')
1229+
H.check_type('close.winconfig', config.close.winconfig, 'callable')
1230+
H.check_type('close.winblend', config.close.winblend, 'callable')
12141231

12151232
return config
12161233
end
@@ -2065,59 +2082,14 @@ end
20652082

20662083
H.default_winconfig_predicate = function(win_id) return true end
20672084

2068-
-- Predicators ----------------------------------------------------------------
2069-
H.is_config_cursor = function(x)
2070-
if type(x) ~= 'table' then return false, H.msg_config('cursor', 'table') end
2071-
if type(x.enable) ~= 'boolean' then return false, H.msg_config('cursor.enable', 'boolean') end
2072-
if not vim.is_callable(x.timing) then return false, H.msg_config('cursor.timing', 'callable') end
2073-
if not vim.is_callable(x.path) then return false, H.msg_config('cursor.path', 'callable') end
2074-
2075-
return true
2076-
end
2077-
2078-
H.is_config_scroll = function(x)
2079-
if type(x) ~= 'table' then return false, H.msg_config('scroll', 'table') end
2080-
if type(x.enable) ~= 'boolean' then return false, H.msg_config('scroll.enable', 'boolean') end
2081-
if not vim.is_callable(x.timing) then return false, H.msg_config('scroll.timing', 'callable') end
2082-
if not vim.is_callable(x.subscroll) then return false, H.msg_config('scroll.subscroll', 'callable') end
2083-
2084-
return true
2085-
end
2086-
2087-
H.is_config_resize = function(x)
2088-
if type(x) ~= 'table' then return false, H.msg_config('resize', 'table') end
2089-
if type(x.enable) ~= 'boolean' then return false, H.msg_config('resize.enable', 'boolean') end
2090-
if not vim.is_callable(x.timing) then return false, H.msg_config('resize.timing', 'callable') end
2091-
if not vim.is_callable(x.subresize) then return false, H.msg_config('resize.subresize', 'callable') end
2092-
2093-
return true
2094-
end
2095-
2096-
H.is_config_open = function(x)
2097-
if type(x) ~= 'table' then return false, H.msg_config('open', 'table') end
2098-
if type(x.enable) ~= 'boolean' then return false, H.msg_config('open.enable', 'boolean') end
2099-
if not vim.is_callable(x.timing) then return false, H.msg_config('open.timing', 'callable') end
2100-
if not vim.is_callable(x.winconfig) then return false, H.msg_config('open.winconfig', 'callable') end
2101-
if not vim.is_callable(x.winblend) then return false, H.msg_config('open.winblend', 'callable') end
2102-
2103-
return true
2104-
end
2105-
2106-
H.is_config_close = function(x)
2107-
if type(x) ~= 'table' then return false, H.msg_config('close', 'table') end
2108-
if type(x.enable) ~= 'boolean' then return false, H.msg_config('close.enable', 'boolean') end
2109-
if not vim.is_callable(x.timing) then return false, H.msg_config('close.timing', 'callable') end
2110-
if not vim.is_callable(x.winconfig) then return false, H.msg_config('close.winconfig', 'callable') end
2111-
if not vim.is_callable(x.winblend) then return false, H.msg_config('close.winblend', 'callable') end
2085+
-- Utilities ------------------------------------------------------------------
2086+
H.error = function(msg) error('(mini.animate) ' .. msg, 0) end
21122087

2113-
return true
2088+
H.check_type = function(name, val, ref, allow_nil)
2089+
if type(val) == ref or (ref == 'callable' and vim.is_callable(val)) or (allow_nil and val == nil) then return end
2090+
H.error(string.format('`%s` should be %s, not %s', name, ref, type(val)))
21142091
end
21152092

2116-
H.msg_config = function(x_name, msg) return string.format('`%s` should be %s.', x_name, msg) end
2117-
2118-
-- Utilities ------------------------------------------------------------------
2119-
H.error = function(msg) error(string.format('(mini.animate) %s', msg), 0) end
2120-
21212093
H.validate_if = function(predicate, x, x_name)
21222094
local is_valid, msg = predicate(x, x_name)
21232095
if not is_valid then H.error(msg) end

lua/mini/base16.lua

+10-4
Original file line numberDiff line numberDiff line change
@@ -355,15 +355,13 @@ H.default_config = vim.deepcopy(MiniBase16.config)
355355
-- Helper functionality =======================================================
356356
-- Settings -------------------------------------------------------------------
357357
H.setup_config = function(config)
358-
-- General idea: if some table elements are not present in user-supplied
359-
-- `config`, take them from default config
360-
vim.validate({ config = { config, 'table', true } })
358+
H.check_type('config', config, 'table', true)
361359
config = vim.tbl_deep_extend('force', vim.deepcopy(H.default_config), config or {})
362360

363361
-- Validate settings
364362
H.validate_base16_palette(config.palette, 'config.palette')
365363
H.validate_use_cterm(config.use_cterm, 'config.use_cterm')
366-
vim.validate({ plugins = { config.plugins, 'table' } })
364+
H.check_type('plugins', config.plugins, 'table')
367365

368366
return config
369367
end
@@ -1574,4 +1572,12 @@ H.nearest_rgb_id = function(rgb_target, rgb_palette)
15741572
return best_id
15751573
end
15761574

1575+
-- Utilities ------------------------------------------------------------------
1576+
H.error = function(msg) error('(mini.base16) ' .. msg, 0) end
1577+
1578+
H.check_type = function(name, val, ref, allow_nil)
1579+
if type(val) == ref or (ref == 'callable' and vim.is_callable(val)) or (allow_nil and val == nil) then return end
1580+
H.error(string.format('`%s` should be %s, not %s', name, ref, type(val)))
1581+
end
1582+
15771583
return MiniBase16

lua/mini/basics.lua

+21-20
Original file line numberDiff line numberDiff line change
@@ -389,32 +389,26 @@ H.buffer_diagnostic_state = {}
389389
-- Helper functionality =======================================================
390390
-- Settings -------------------------------------------------------------------
391391
H.setup_config = function(config)
392-
-- General idea: if some table elements are not present in user-supplied
393-
-- `config`, take them from default config
394-
vim.validate({ config = { config, 'table', true } })
392+
H.check_type('config', config, 'table', true)
395393
config = vim.tbl_deep_extend('force', vim.deepcopy(H.default_config), config or {})
396394

397-
vim.validate({
398-
options = { config.options, 'table' },
399-
mappings = { config.mappings, 'table' },
400-
autocommands = { config.autocommands, 'table' },
401-
})
395+
H.check_type('options', config.options, 'table')
402396

403-
vim.validate({
404-
['options.basic'] = { config.options.basic, 'boolean' },
405-
['options.extra_ui'] = { config.options.extra_ui, 'boolean' },
406-
['options.win_borders'] = { config.options.win_borders, 'string' },
397+
H.check_type('options.basic', config.options.basic, 'boolean')
398+
H.check_type('options.extra_ui', config.options.extra_ui, 'boolean')
399+
H.check_type('options.win_borders', config.options.win_borders, 'string')
407400

408-
['mappings.basic'] = { config.mappings.basic, 'boolean' },
409-
['mappings.option_toggle_prefix'] = { config.mappings.option_toggle_prefix, 'string' },
410-
['mappings.windows'] = { config.mappings.windows, 'boolean' },
411-
['mappings.move_with_alt'] = { config.mappings.move_with_alt, 'boolean' },
401+
H.check_type('mappings', config.mappings, 'table')
402+
H.check_type('mappings.basic', config.mappings.basic, 'boolean')
403+
H.check_type('mappings.option_toggle_prefix', config.mappings.option_toggle_prefix, 'string')
404+
H.check_type('mappings.windows', config.mappings.windows, 'boolean')
405+
H.check_type('mappings.move_with_alt', config.mappings.move_with_alt, 'boolean')
412406

413-
['autocommands.basic'] = { config.autocommands.basic, 'boolean' },
414-
['autocommands.relnum_in_visual_mode'] = { config.autocommands.relnum_in_visual_mode, 'boolean' },
407+
H.check_type('autocommands', config.autocommands, 'table')
408+
H.check_type('autocommands.basic', config.autocommands.basic, 'boolean')
409+
H.check_type('autocommands.relnum_in_visual_mode', config.autocommands.relnum_in_visual_mode, 'boolean')
415410

416-
['silent'] = { config.silent, 'boolean' },
417-
})
411+
H.check_type('silent', config.silent, 'boolean')
418412

419413
return config
420414
end
@@ -746,6 +740,13 @@ H.apply_autocommands = function(config)
746740
end
747741

748742
-- Utilities ------------------------------------------------------------------
743+
H.error = function(msg) error('(mini.basics) ' .. msg, 0) end
744+
745+
H.check_type = function(name, val, ref, allow_nil)
746+
if type(val) == ref or (ref == 'callable' and vim.is_callable(val)) or (allow_nil and val == nil) then return end
747+
H.error(string.format('`%s` should be %s, not %s', name, ref, type(val)))
748+
end
749+
749750
H.map = function(mode, lhs, rhs, opts)
750751
if lhs == '' then return end
751752
opts = vim.tbl_deep_extend('force', { silent = true }, opts or {})

0 commit comments

Comments
 (0)