Skip to content

Commit 630f997

Browse files
perf: Use optimized validator if available
1 parent bf65774 commit 630f997

File tree

8 files changed

+46
-20
lines changed

8 files changed

+46
-20
lines changed

lua/orgmode/api/init.lua

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
local OrgFile = require('orgmode.api.file')
33
local OrgHeadline = require('orgmode.api.headline')
44
local orgmode = require('orgmode')
5+
local validator = require('orgmode.utils.validator')
56

67
---@class OrgApiRefileOpts
78
---@field source OrgApiHeadline
@@ -13,7 +14,7 @@ local OrgApi = {}
1314
---@param name? string|string[] specific file names to return (absolute path). If ommitted, returns all loaded files
1415
---@return OrgApiFile|OrgApiFile[]
1516
function OrgApi.load(name)
16-
vim.validate({
17+
validator.validate({
1718
name = { name, { 'string', 'table' }, true },
1819
})
1920
if not name then
@@ -55,7 +56,7 @@ end
5556
---@param opts OrgApiRefileOpts
5657
---@return boolean
5758
function OrgApi.refile(opts)
58-
vim.validate({
59+
validator.validate({
5960
source = { opts.source, 'table' },
6061
destination = { opts.destination, 'table' },
6162
})

lua/orgmode/capture/template/init.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local TemplateProperties = require('orgmode.capture.template.template_properties')
22
local Date = require('orgmode.objects.date')
33
local utils = require('orgmode.utils')
4+
local validator = require('orgmode.utils.validator')
45
local Calendar = require('orgmode.objects.calendar')
56
local Promise = require('orgmode.utils.promise')
67

@@ -94,7 +95,7 @@ local Template = {}
9495
function Template:new(opts)
9596
opts = opts or {}
9697

97-
vim.validate({
98+
validator.validate({
9899
description = { opts.description, 'string', true },
99100
template = { opts.template, { 'string', 'table' }, true },
100101
target = { opts.target, 'string', true },

lua/orgmode/capture/template/template_properties.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
local validator = require('orgmode.utils.validator')
12
---@class OrgCaptureTemplateProperties
23
---@field empty_lines { before: integer, after: integer } | number
34
local TemplateProperties = {}
45

56
function TemplateProperties:new(opts)
67
opts = opts or {}
78

8-
vim.validate({
9+
validator.validate({
910
empty_lines = { opts.empty_lines, { 'table', 'number' }, true },
1011
})
1112

lua/orgmode/capture/templates.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local config = require('orgmode.config')
22
local Template = require('orgmode.capture.template')
3+
local validator = require('orgmode.utils.validator')
34

45
---@see https://orgmode.org/manual/Capture-templates.html
56

@@ -12,7 +13,7 @@ local Templates = {}
1213
function Templates:new(templates)
1314
local opts = {}
1415

15-
vim.validate({
16+
validator.validate({
1617
templates = { templates, 'table', true },
1718
})
1819

lua/orgmode/config/mappings/map_entry.lua

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
local validator = require('orgmode.utils.validator')
2+
13
---@class OrgMapEntry
24
---@field provided_opts table
35
---@field handler string
@@ -50,7 +52,7 @@ end
5052
---@param opts? table<string, any>
5153
function MapEntry:new(handler, opts)
5254
opts = opts or {}
53-
vim.validate({
55+
validator.validate({
5456
handler = { handler, { 'string', 'function' } },
5557
modes = { opts.modes, 'table', true },
5658
desc = { opts.desc, 'string', true },

lua/orgmode/ui/menu.lua

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local validator = require('orgmode.utils.validator')
12
local config = require('orgmode.config')
23

34
---@class OrgMenuOption
@@ -36,7 +37,7 @@ end
3637

3738
---@param option OrgMenuOption
3839
function Menu:_validate_option(option)
39-
vim.validate({
40+
validator.validate({
4041
label = { option.label, 'string' },
4142
key = { option.key, 'string' },
4243
action = { option.action, 'function', true },
@@ -45,7 +46,7 @@ end
4546

4647
---@param items OrgMenuItem[]?
4748
function Menu:_validate_items(items)
48-
vim.validate({
49+
validator.validate({
4950
items = { items, 'table', true },
5051
})
5152
if not items then
@@ -65,11 +66,11 @@ end
6566

6667
---@param separator OrgMenuSeparator?
6768
function Menu:_validate_separator(separator)
68-
vim.validate({
69+
validator.validate({
6970
separator = { separator, 'table', true },
7071
})
7172
if separator then
72-
vim.validate({
73+
validator.validate({
7374
icon = { separator.icon, 'string', true },
7475
length = { separator.length, 'number', true },
7576
})
@@ -78,7 +79,7 @@ end
7879

7980
---@param data OrgMenu
8081
function Menu:_validate_data(data)
81-
vim.validate({
82+
validator.validate({
8283
title = { data.title, 'string' },
8384
prompt = { data.prompt, 'string' },
8485
})

lua/orgmode/utils/promise.lua

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local validator = require('orgmode.utils.validator')
12
---@diagnostic disable: undefined-field
23
-- Taken from https://github.com/notomo/promise.nvim
34

@@ -45,7 +46,7 @@ local new_empty_userdata = function()
4546
end
4647

4748
local new_pending = function(on_fullfilled, on_rejected)
48-
vim.validate({
49+
validator.validate({
4950
on_fullfilled = { on_fullfilled, 'function', true },
5051
on_rejected = { on_rejected, 'function', true },
5152
})
@@ -79,7 +80,7 @@ end
7980
--- @param executor fun(resolve:fun(...:any),reject:fun(...:any))
8081
--- @return OrgPromise
8182
function Promise.new(executor)
82-
vim.validate({ executor = { executor, 'function' } })
83+
validator.validate({ executor = { executor, 'function' } })
8384

8485
local self = new_pending()
8586

@@ -219,7 +220,7 @@ end
219220
--- @param on_rejected (fun(...:any):any)?: A callback on rejected.
220221
--- @return OrgPromise
221222
function Promise.next(self, on_fullfilled, on_rejected)
222-
vim.validate({
223+
validator.validate({
223224
on_fullfilled = { on_fullfilled, 'function', true },
224225
on_rejected = { on_rejected, 'function', true },
225226
})
@@ -247,7 +248,7 @@ end
247248
--- @param on_finally fun()
248249
--- @return OrgPromise
249250
function Promise.finally(self, on_finally)
250-
vim.validate({ on_finally = { on_finally, 'function', true } })
251+
validator.validate({ on_finally = { on_finally, 'function', true } })
251252
return self
252253
:next(function(...)
253254
on_finally()
@@ -307,7 +308,7 @@ end
307308
--- @param list any[]: promise or non-promise values
308309
--- @return OrgPromise
309310
function Promise.all(list)
310-
vim.validate({ list = { list, 'table' } })
311+
validator.validate({ list = { list, 'table' } })
311312
return Promise.new(function(resolve, reject)
312313
local remain = #list
313314
if remain == 0 then
@@ -338,7 +339,7 @@ end
338339
--- @param concurrency? number: limit number of concurrent items processing
339340
--- @return OrgPromise
340341
function Promise.map(callback, list, concurrency)
341-
vim.validate({
342+
validator.validate({
342343
list = { list, 'table' },
343344
callback = { callback, 'function' },
344345
concurrency = { concurrency, 'number', true },
@@ -383,7 +384,7 @@ end
383384
--- @param list any[]: promise or non-promise values
384385
--- @return OrgPromise
385386
function Promise.race(list)
386-
vim.validate({ list = { list, 'table' } })
387+
validator.validate({ list = { list, 'table' } })
387388
return Promise.new(function(resolve, reject)
388389
for _, e in ipairs(list) do
389390
Promise.resolve(e)
@@ -402,7 +403,7 @@ end
402403
--- @param list any[]: promise or non-promise values
403404
--- @return OrgPromise
404405
function Promise.any(list)
405-
vim.validate({ list = { list, 'table' } })
406+
validator.validate({ list = { list, 'table' } })
406407
return Promise.new(function(resolve, reject)
407408
local remain = #list
408409
if remain == 0 then
@@ -432,7 +433,7 @@ end
432433
--- @param list any[]: promise or non-promise values
433434
--- @return OrgPromise
434435
function Promise.all_settled(list)
435-
vim.validate({ list = { list, 'table' } })
436+
validator.validate({ list = { list, 'table' } })
436437
return Promise.new(function(resolve)
437438
local remain = #list
438439
if remain == 0 then

lua/orgmode/utils/validator.lua

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
local M = {}
2+
3+
--- Use the faster validate version if available
4+
-- Taken from: https://github.com/lukas-reineke/indent-blankline.nvim/pull/934/files#diff-09ebcaa8c75cd1e92d25640e377ab261cfecaf8351c9689173fd36c2d0c23d94R16
5+
--- @param spec table<string,[any, vim.validate.Validator, boolean|string]>
6+
function M.validate(spec)
7+
if vim.fn.has('nvim-0.11') == 0 then
8+
return vim.validate(spec)
9+
end
10+
for key, key_spec in pairs(spec) do
11+
local message = type(key_spec[3]) == 'string' and key_spec[3] or nil --[[@as string?]]
12+
local optional = type(key_spec[3]) == 'boolean' and key_spec[3] or nil --[[@as boolean?]]
13+
---@diagnostic disable-next-line:param-type-mismatch, redundant-parameter
14+
vim.validate(key, key_spec[1], key_spec[2], optional, message)
15+
end
16+
end
17+
18+
return M

0 commit comments

Comments
 (0)