Skip to content

Commit

Permalink
perf: Use optimized validator if available
Browse files Browse the repository at this point in the history
  • Loading branch information
kristijanhusak committed Jan 6, 2025
1 parent bf65774 commit 630f997
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 20 deletions.
5 changes: 3 additions & 2 deletions lua/orgmode/api/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
local OrgFile = require('orgmode.api.file')
local OrgHeadline = require('orgmode.api.headline')
local orgmode = require('orgmode')
local validator = require('orgmode.utils.validator')

---@class OrgApiRefileOpts
---@field source OrgApiHeadline
Expand All @@ -13,7 +14,7 @@ local OrgApi = {}
---@param name? string|string[] specific file names to return (absolute path). If ommitted, returns all loaded files
---@return OrgApiFile|OrgApiFile[]
function OrgApi.load(name)
vim.validate({
validator.validate({
name = { name, { 'string', 'table' }, true },
})
if not name then
Expand Down Expand Up @@ -55,7 +56,7 @@ end
---@param opts OrgApiRefileOpts
---@return boolean
function OrgApi.refile(opts)
vim.validate({
validator.validate({
source = { opts.source, 'table' },
destination = { opts.destination, 'table' },
})
Expand Down
3 changes: 2 additions & 1 deletion lua/orgmode/capture/template/init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local TemplateProperties = require('orgmode.capture.template.template_properties')
local Date = require('orgmode.objects.date')
local utils = require('orgmode.utils')
local validator = require('orgmode.utils.validator')
local Calendar = require('orgmode.objects.calendar')
local Promise = require('orgmode.utils.promise')

Expand Down Expand Up @@ -94,7 +95,7 @@ local Template = {}
function Template:new(opts)
opts = opts or {}

vim.validate({
validator.validate({
description = { opts.description, 'string', true },
template = { opts.template, { 'string', 'table' }, true },
target = { opts.target, 'string', true },
Expand Down
3 changes: 2 additions & 1 deletion lua/orgmode/capture/template/template_properties.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
local validator = require('orgmode.utils.validator')
---@class OrgCaptureTemplateProperties
---@field empty_lines { before: integer, after: integer } | number
local TemplateProperties = {}

function TemplateProperties:new(opts)
opts = opts or {}

vim.validate({
validator.validate({
empty_lines = { opts.empty_lines, { 'table', 'number' }, true },
})

Expand Down
3 changes: 2 additions & 1 deletion lua/orgmode/capture/templates.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local config = require('orgmode.config')
local Template = require('orgmode.capture.template')
local validator = require('orgmode.utils.validator')

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

Expand All @@ -12,7 +13,7 @@ local Templates = {}
function Templates:new(templates)
local opts = {}

vim.validate({
validator.validate({
templates = { templates, 'table', true },
})

Expand Down
4 changes: 3 additions & 1 deletion lua/orgmode/config/mappings/map_entry.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local validator = require('orgmode.utils.validator')

---@class OrgMapEntry
---@field provided_opts table
---@field handler string
Expand Down Expand Up @@ -50,7 +52,7 @@ end
---@param opts? table<string, any>
function MapEntry:new(handler, opts)
opts = opts or {}
vim.validate({
validator.validate({
handler = { handler, { 'string', 'function' } },
modes = { opts.modes, 'table', true },
desc = { opts.desc, 'string', true },
Expand Down
11 changes: 6 additions & 5 deletions lua/orgmode/ui/menu.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local validator = require('orgmode.utils.validator')
local config = require('orgmode.config')

---@class OrgMenuOption
Expand Down Expand Up @@ -36,7 +37,7 @@ end

---@param option OrgMenuOption
function Menu:_validate_option(option)
vim.validate({
validator.validate({
label = { option.label, 'string' },
key = { option.key, 'string' },
action = { option.action, 'function', true },
Expand All @@ -45,7 +46,7 @@ end

---@param items OrgMenuItem[]?
function Menu:_validate_items(items)
vim.validate({
validator.validate({
items = { items, 'table', true },
})
if not items then
Expand All @@ -65,11 +66,11 @@ end

---@param separator OrgMenuSeparator?
function Menu:_validate_separator(separator)
vim.validate({
validator.validate({
separator = { separator, 'table', true },
})
if separator then
vim.validate({
validator.validate({
icon = { separator.icon, 'string', true },
length = { separator.length, 'number', true },
})
Expand All @@ -78,7 +79,7 @@ end

---@param data OrgMenu
function Menu:_validate_data(data)
vim.validate({
validator.validate({
title = { data.title, 'string' },
prompt = { data.prompt, 'string' },
})
Expand Down
19 changes: 10 additions & 9 deletions lua/orgmode/utils/promise.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local validator = require('orgmode.utils.validator')
---@diagnostic disable: undefined-field
-- Taken from https://github.com/notomo/promise.nvim

Expand Down Expand Up @@ -45,7 +46,7 @@ local new_empty_userdata = function()
end

local new_pending = function(on_fullfilled, on_rejected)
vim.validate({
validator.validate({
on_fullfilled = { on_fullfilled, 'function', true },
on_rejected = { on_rejected, 'function', true },
})
Expand Down Expand Up @@ -79,7 +80,7 @@ end
--- @param executor fun(resolve:fun(...:any),reject:fun(...:any))
--- @return OrgPromise
function Promise.new(executor)
vim.validate({ executor = { executor, 'function' } })
validator.validate({ executor = { executor, 'function' } })

local self = new_pending()

Expand Down Expand Up @@ -219,7 +220,7 @@ end
--- @param on_rejected (fun(...:any):any)?: A callback on rejected.
--- @return OrgPromise
function Promise.next(self, on_fullfilled, on_rejected)
vim.validate({
validator.validate({
on_fullfilled = { on_fullfilled, 'function', true },
on_rejected = { on_rejected, 'function', true },
})
Expand Down Expand Up @@ -247,7 +248,7 @@ end
--- @param on_finally fun()
--- @return OrgPromise
function Promise.finally(self, on_finally)
vim.validate({ on_finally = { on_finally, 'function', true } })
validator.validate({ on_finally = { on_finally, 'function', true } })
return self
:next(function(...)
on_finally()
Expand Down Expand Up @@ -307,7 +308,7 @@ end
--- @param list any[]: promise or non-promise values
--- @return OrgPromise
function Promise.all(list)
vim.validate({ list = { list, 'table' } })
validator.validate({ list = { list, 'table' } })
return Promise.new(function(resolve, reject)
local remain = #list
if remain == 0 then
Expand Down Expand Up @@ -338,7 +339,7 @@ end
--- @param concurrency? number: limit number of concurrent items processing
--- @return OrgPromise
function Promise.map(callback, list, concurrency)
vim.validate({
validator.validate({
list = { list, 'table' },
callback = { callback, 'function' },
concurrency = { concurrency, 'number', true },
Expand Down Expand Up @@ -383,7 +384,7 @@ end
--- @param list any[]: promise or non-promise values
--- @return OrgPromise
function Promise.race(list)
vim.validate({ list = { list, 'table' } })
validator.validate({ list = { list, 'table' } })
return Promise.new(function(resolve, reject)
for _, e in ipairs(list) do
Promise.resolve(e)
Expand All @@ -402,7 +403,7 @@ end
--- @param list any[]: promise or non-promise values
--- @return OrgPromise
function Promise.any(list)
vim.validate({ list = { list, 'table' } })
validator.validate({ list = { list, 'table' } })
return Promise.new(function(resolve, reject)
local remain = #list
if remain == 0 then
Expand Down Expand Up @@ -432,7 +433,7 @@ end
--- @param list any[]: promise or non-promise values
--- @return OrgPromise
function Promise.all_settled(list)
vim.validate({ list = { list, 'table' } })
validator.validate({ list = { list, 'table' } })
return Promise.new(function(resolve)
local remain = #list
if remain == 0 then
Expand Down
18 changes: 18 additions & 0 deletions lua/orgmode/utils/validator.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
local M = {}

--- Use the faster validate version if available
-- Taken from: https://github.com/lukas-reineke/indent-blankline.nvim/pull/934/files#diff-09ebcaa8c75cd1e92d25640e377ab261cfecaf8351c9689173fd36c2d0c23d94R16
--- @param spec table<string,[any, vim.validate.Validator, boolean|string]>
function M.validate(spec)
if vim.fn.has('nvim-0.11') == 0 then
return vim.validate(spec)
end
for key, key_spec in pairs(spec) do
local message = type(key_spec[3]) == 'string' and key_spec[3] or nil --[[@as string?]]
local optional = type(key_spec[3]) == 'boolean' and key_spec[3] or nil --[[@as boolean?]]
---@diagnostic disable-next-line:param-type-mismatch, redundant-parameter
vim.validate(key, key_spec[1], key_spec[2], optional, message)
end
end

return M

0 comments on commit 630f997

Please sign in to comment.