Skip to content

Commit 2929054

Browse files
feat(validator): use new vim.validator structure for neovim 0.11
1 parent 145dce4 commit 2929054

File tree

8 files changed

+63
-83
lines changed

8 files changed

+63
-83
lines changed

lua/orgmode/api/init.lua

+3-7
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ local OrgApi = {}
1515
---@param name? string|string[] specific file names to return (absolute path). If ommitted, returns all loaded files
1616
---@return OrgApiFile|OrgApiFile[]
1717
function OrgApi.load(name)
18-
validator.validate({
19-
name = { name, { 'string', 'table' }, true },
20-
})
18+
validator.validate('name', name, { 'string', 'table' }, true)
2119
if not name then
2220
return vim.tbl_map(function(file)
2321
return OrgFile._build_from_internal_file(file)
@@ -57,10 +55,8 @@ end
5755
---@param opts OrgApiRefileOpts
5856
---@return OrgPromise<boolean>
5957
function OrgApi.refile(opts)
60-
validator.validate({
61-
source = { opts.source, 'table' },
62-
destination = { opts.destination, 'table' },
63-
})
58+
validator.validate('source', opts.source, 'table')
59+
validator.validate('destination', opts.destination, 'table')
6460

6561
if getmetatable(opts.source) ~= OrgHeadline then
6662
error('Source must be an OrgApiHeadline', 0)

lua/orgmode/capture/template/init.lua

+9-11
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,15 @@ local Template = {}
9999
function Template:new(opts)
100100
opts = opts or {}
101101

102-
validator.validate({
103-
description = { opts.description, 'string', true },
104-
template = { opts.template, { 'string', 'table' }, true },
105-
target = { opts.target, 'string', true },
106-
regexp = { opts.regexp, 'string', true },
107-
headline = { opts.headline, 'string', true },
108-
properties = { opts.properties, 'table', true },
109-
subtemplates = { opts.subtemplates, 'table', true },
110-
datetree = { opts.datetree, { 'boolean', 'table' }, true },
111-
whole_file = { opts.whole_file, 'boolean', true },
112-
})
102+
validator.validate('description', opts.description, 'string', true)
103+
validator.validate('template', opts.template, { 'string', 'table' }, true)
104+
validator.validate('target', opts.target, 'string', true)
105+
validator.validate('regexp', opts.regexp, 'string', true)
106+
validator.validate('headline', opts.headline, 'string', true)
107+
validator.validate('properties', opts.properties, 'table', true)
108+
validator.validate('subtemplates', opts.subtemplates, 'table', true)
109+
validator.validate('datetree', opts.datetree, { 'boolean', 'table' }, true)
110+
validator.validate('whole_file', opts.whole_file, 'boolean', true)
113111

114112
local this = {}
115113
this.description = opts.description or ''

lua/orgmode/capture/template/template_properties.lua

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ local TemplateProperties = {}
66
function TemplateProperties:new(opts)
77
opts = opts or {}
88

9-
validator.validate({
10-
empty_lines = { opts.empty_lines, { 'table', 'number' }, true },
11-
})
9+
validator.validate('empty_lines', opts.empty_lines, { 'table', 'number' }, true)
1210

1311
local empty_lines = opts.empty_lines or {}
1412
if type(empty_lines) == 'number' then

lua/orgmode/capture/templates.lua

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ local Templates = {}
1313
function Templates:new(templates)
1414
local opts = {}
1515

16-
validator.validate({
17-
templates = { templates, 'table', true },
18-
})
16+
validator.validate('templates', templates, 'table', true)
1917

2018
opts.templates = {}
2119
for key, template in pairs(templates or config.org_capture_templates) do

lua/orgmode/config/mappings/map_entry.lua

+6-7
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,12 @@ end
5252
---@param opts? table<string, any>
5353
function MapEntry:new(handler, opts)
5454
opts = opts or {}
55-
validator.validate({
56-
handler = { handler, { 'string', 'function' } },
57-
modes = { opts.modes, 'table', true },
58-
desc = { opts.desc, 'string', true },
59-
help_desc = { opts.help_desc, 'string', true },
60-
type = { opts.type, 'string', true },
61-
})
55+
validator.validate('handler', handler, { 'string', 'function' })
56+
validator.validate('modes', opts.modes, 'table', true)
57+
validator.validate('desc', opts.desc, 'string', true)
58+
validator.validate('help_desc', opts.help_desc, 'string', true)
59+
validator.validate('type', opts.type, 'string', true)
60+
6261
local data = {}
6362
data.provided_opts = opts
6463
data.handler = handler

lua/orgmode/ui/menu.lua

+9-19
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,14 @@ end
4040

4141
---@param option OrgMenuOption
4242
function Menu:_validate_option(option)
43-
validator.validate({
44-
label = { option.label, 'string' },
45-
key = { option.key, 'string' },
46-
action = { option.action, 'function', true },
47-
})
43+
validator.validate('label', option.label, 'string')
44+
validator.validate('key', option.key, 'string')
45+
validator.validate('action', option.action, 'function', true)
4846
end
4947

5048
---@param items OrgMenuItem[]?
5149
function Menu:_validate_items(items)
52-
validator.validate({
53-
items = { items, 'table', true },
54-
})
50+
validator.validate('items', items, 'table', true)
5551
if not items then
5652
return
5753
end
@@ -69,23 +65,17 @@ end
6965

7066
---@param separator OrgMenuSeparator?
7167
function Menu:_validate_separator(separator)
72-
validator.validate({
73-
separator = { separator, 'table', true },
74-
})
68+
validator.validate('separator', separator, 'table', true)
7569
if separator then
76-
validator.validate({
77-
icon = { separator.icon, 'string', true },
78-
length = { separator.length, 'number', true },
79-
})
70+
validator.validate('icon', separator.icon, 'string', true)
71+
validator.validate('length', separator.length, 'number', true)
8072
end
8173
end
8274

8375
---@param data OrgMenuOpts
8476
function Menu:_validate_data(data)
85-
validator.validate({
86-
title = { data.title, 'string' },
87-
prompt = { data.prompt, 'string' },
88-
})
77+
validator.validate('title', data.title, 'string')
78+
validator.validate('prompt', data.prompt, 'string')
8979
self:_validate_items(data.items)
9080
self:_validate_separator(data.separator)
9181
end

lua/orgmode/utils/promise.lua

+13-19
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,8 @@ local new_empty_userdata = function()
4646
end
4747

4848
local new_pending = function(on_fullfilled, on_rejected)
49-
validator.validate({
50-
on_fullfilled = { on_fullfilled, 'function', true },
51-
on_rejected = { on_rejected, 'function', true },
52-
})
49+
validator.validate('on_fullfilled', on_fullfilled, 'function', true)
50+
validator.validate('on_rejected', on_rejected, 'function', true)
5351
local tbl = {
5452
_status = PromiseStatus.Pending,
5553
_queued = {},
@@ -80,7 +78,7 @@ end
8078
--- @param executor fun(resolve:fun(...:any),reject:fun(...:any))
8179
--- @return OrgPromise
8280
function Promise.new(executor)
83-
validator.validate({ executor = { executor, 'function' } })
81+
validator.validate('executor', executor, 'function')
8482

8583
local self = new_pending()
8684

@@ -220,10 +218,8 @@ end
220218
--- @param on_rejected (fun(...:any):any)?: A callback on rejected.
221219
--- @return OrgPromise
222220
function Promise.next(self, on_fullfilled, on_rejected)
223-
validator.validate({
224-
on_fullfilled = { on_fullfilled, 'function', true },
225-
on_rejected = { on_rejected, 'function', true },
226-
})
221+
validator.validate('on_fullfilled', on_fullfilled, 'function', true)
222+
validator.validate('on_rejected', on_rejected, 'function', true)
227223
local promise = new_pending(on_fullfilled, on_rejected)
228224
table.insert(self._queued, promise)
229225
vim.schedule(function()
@@ -248,7 +244,7 @@ end
248244
--- @param on_finally fun()
249245
--- @return OrgPromise
250246
function Promise.finally(self, on_finally)
251-
validator.validate({ on_finally = { on_finally, 'function', true } })
247+
validator.validate('on_finally', on_finally, 'function', true)
252248
return self
253249
:next(function(...)
254250
on_finally()
@@ -308,7 +304,7 @@ end
308304
--- @param list any[]: promise or non-promise values
309305
--- @return OrgPromise
310306
function Promise.all(list)
311-
validator.validate({ list = { list, 'table' } })
307+
validator.validate('list', list, 'table')
312308
return Promise.new(function(resolve, reject)
313309
local remain = #list
314310
if remain == 0 then
@@ -339,11 +335,9 @@ end
339335
--- @param concurrency? number: limit number of concurrent items processing
340336
--- @return OrgPromise
341337
function Promise.map(callback, list, concurrency)
342-
validator.validate({
343-
list = { list, 'table' },
344-
callback = { callback, 'function' },
345-
concurrency = { concurrency, 'number', true },
346-
})
338+
validator.validate('list', list, 'table')
339+
validator.validate('callback', callback, 'function')
340+
validator.validate('concurrency', concurrency, 'number', true)
347341

348342
local results = {}
349343
local processing = 0
@@ -392,7 +386,7 @@ end
392386
--- @param list any[]: promise or non-promise values
393387
--- @return OrgPromise
394388
function Promise.race(list)
395-
validator.validate({ list = { list, 'table' } })
389+
validator.validate('list', list, 'table')
396390
return Promise.new(function(resolve, reject)
397391
for _, e in ipairs(list) do
398392
Promise.resolve(e)
@@ -411,7 +405,7 @@ end
411405
--- @param list any[]: promise or non-promise values
412406
--- @return OrgPromise
413407
function Promise.any(list)
414-
validator.validate({ list = { list, 'table' } })
408+
validator.validate('list', list, 'table')
415409
return Promise.new(function(resolve, reject)
416410
local remain = #list
417411
if remain == 0 then
@@ -441,7 +435,7 @@ end
441435
--- @param list any[]: promise or non-promise values
442436
--- @return OrgPromise
443437
function Promise.all_settled(list)
444-
validator.validate({ list = { list, 'table' } })
438+
validator.validate('list', list, 'table')
445439
return Promise.new(function(resolve)
446440
local remain = #list
447441
if remain == 0 then

lua/orgmode/utils/validator.lua

+21-14
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
local M = {}
22

3-
-- local has_v_0_11 = vim.fn.has('nvim-0.11') > 0
3+
local has_v_0_11 = vim.fn.has('nvim-0.11') > 0
44

5-
--- Use the faster validate version if available
6-
-- Taken from: https://github.com/lukas-reineke/indent-blankline.nvim/pull/934/files#diff-09ebcaa8c75cd1e92d25640e377ab261cfecaf8351c9689173fd36c2d0c23d94R16
7-
--- @param spec table<string,[any, vim.validate.Validator, boolean|string]>
8-
function M.validate(spec)
9-
-- if not has_v_0_11 then
10-
return vim.validate(spec)
11-
-- end
12-
-- for key, key_spec in pairs(spec) do
13-
-- local message = type(key_spec[3]) == 'string' and key_spec[3] or nil --[[@as string?]]
14-
-- local optional = type(key_spec[3]) == 'boolean' and key_spec[3] or nil --[[@as boolean?]]
15-
-- ---@diagnostic disable-next-line:param-type-mismatch, redundant-parameter
16-
-- vim.validate(key, key_spec[1], key_spec[2], optional, message)
17-
-- end
5+
--- @alias OrgValidateValidor
6+
--- | type
7+
--- | 'callable'
8+
--- | (type|'callable')[]
9+
--- | fun(v:any):boolean, string?
10+
11+
--- @param name string Argument name
12+
--- @param value any Argument value
13+
--- @param validator OrgValidateValidor
14+
--- @param optional? boolean
15+
function M.validate(name, value, validator, optional)
16+
if has_v_0_11 then
17+
return vim.validate(name, value, validator, optional)
18+
else
19+
local val = { value, validator }
20+
if optional then
21+
table.insert(val, optional)
22+
end
23+
return vim.validate({ [name] = val })
24+
end
1825
end
1926

2027
return M

0 commit comments

Comments
 (0)