Skip to content

Commit

Permalink
perf: use faster 0.11 vim.validate (#868)
Browse files Browse the repository at this point in the history
* perf: use faster 0.11 vim.validate

* remove vim.fn.has

* feat: check for nvim 0.11 and benchmark

---------

Co-authored-by: iguanacucumber <[email protected]>
Co-authored-by: Liam Dyer <[email protected]>
  • Loading branch information
3 people authored Jan 3, 2025
1 parent 8ca8ca4 commit a8957ba
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lua/blink/cmp/config/keymap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ function keymap.validate(config)
}
end
end
vim.validate(validation_schema)
require('blink.cmp.config.utils')._validate(validation_schema)
end

return keymap
28 changes: 23 additions & 5 deletions lua/blink/cmp/config/utils.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
local utils = {}

---@param path string The path to the field being validated
---@param tbl table The table to validate
---@param source table The original table that we're validating against
---@see vim.validate
-- Code taken from @MariaSolOs in a indent-blankline.nvim PR:
-- https://github.com/lukas-reineke/indent-blankline.nvim/pull/934/files#diff-09ebcaa8c75cd1e92d25640e377ab261cfecaf8351c9689173fd36c2d0c23d94R16
-- Saves a whopping 20% compared to vim.validate (0.8ms -> 0.64ms)

--- Use the faster validate version if available
--- @param spec table<string, {[1]:any, [2]:function|string, [3]:string|true|nil}>
--- NOTE: We disable some Lua diagnostics here since lua_ls isn't smart enough to
--- realize that we're using an overloaded function.
function utils._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

--- @param path string The path to the field being validated
--- @param tbl table The table to validate
--- @param source table The original table that we're validating against
--- @see vim.validate
function utils.validate(path, tbl, source)
-- validate
local _, err = pcall(vim.validate, tbl)
local _, err = pcall(utils._validate, tbl)
if err then error(path .. '.' .. err) end

-- check for erroneous fields
Expand Down
4 changes: 2 additions & 2 deletions lua/blink/cmp/sources/luasnip.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ local defaults_config = {

function source.new(opts)
local config = vim.tbl_deep_extend('keep', opts or {}, defaults_config)
vim.validate({
require('blink.cmp.config.utils').validate('sources.providers.luasnip', {
use_show_condition = { config.use_show_condition, 'boolean' },
show_autosnippets = { config.show_autosnippets, 'boolean' },
})
}, config)

local self = setmetatable({}, { __index = source })
self.config = config
Expand Down
7 changes: 3 additions & 4 deletions lua/blink/cmp/sources/path/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
--- @field get_cwd fun(context: blink.cmp.Context): string
--- @field show_hidden_files_by_default boolean

local regex = require('blink.cmp.sources.path.regex')

--- @class blink.cmp.Source
--- @field opts blink.cmp.PathOpts
local path = {}

function path.new(opts)
Expand All @@ -22,12 +21,12 @@ function path.new(opts)
get_cwd = function(context) return vim.fn.expand(('#%d:p:h'):format(context.bufnr)) end,
show_hidden_files_by_default = false,
})
vim.validate({
require('blink.cmp.config.utils').validate('sources.providers.path', {
trailing_slash = { opts.trailing_slash, 'boolean' },
label_trailing_slash = { opts.label_trailing_slash, 'boolean' },
get_cwd = { opts.get_cwd, 'function' },
show_hidden_files_by_default = { opts.show_hidden_files_by_default, 'boolean' },
})
}, opts)

self.opts = opts
return self
Expand Down

0 comments on commit a8957ba

Please sign in to comment.