diff --git a/lua/submode/init.lua b/lua/submode/init.lua index 77d98ee..d8294a5 100644 --- a/lua/submode/init.lua +++ b/lua/submode/init.lua @@ -1,6 +1,7 @@ local utils = require("submode.utils") local mode = require("submode.mode") local snapshot = require("submode.snapshot") +local validate = require("submode.validate").validate ---Default state of this plugin ---@class SubmodeState @@ -23,9 +24,9 @@ local M = { ---@param opts SubmodeOpts Options of this submode. ---@param default? fun(register: SubmodeDefaultMappingRegister) Default mappings register function M.create(name, opts, default) - vim.validate("name", name, "string") - vim.validate("opts", opts, "table") - vim.validate("default", default, "function", true) + validate("name", name, "string") + validate("opts", opts, "table") + validate("default", default, "function", true) local state = M.state @@ -77,9 +78,9 @@ function M.create(name, opts, default) ---@param rhs string | fun():string? ---@param opts_ vim.keymap.set.Opts? local register = function(lhs, rhs, opts_) - vim.validate("lhs", lhs, "string") - vim.validate("rhs", rhs, { "string", "function" }) - vim.validate("opts", opts_, "table", true) + validate("lhs", lhs, "string") + validate("rhs", rhs, { "string", "function" }) + validate("opts", opts_, "table", true) M.state.submode_to_default_mappings[name][lhs] = { rhs = rhs, @@ -99,10 +100,10 @@ end ---@param rhs string | fun():string? Rhs of mapping. Can be function. ---@param opts? vim.keymap.set.Opts Options of this mapping. Same as `opts` of `vim.keymap.set`. function M.set(name, lhs, rhs, opts) - vim.validate("name", name, "string") - vim.validate("lhs", lhs, "string") - vim.validate("rhs", rhs, { "string", "function" }) - vim.validate("opts", opts, "table", true) + validate("name", name, "string") + validate("lhs", lhs, "string") + validate("rhs", rhs, { "string", "function" }) + validate("opts", opts, "table", true) M.state.submode_to_user_mappings[name][lhs] = { rhs = rhs, @@ -119,9 +120,9 @@ end ---@param lhs string Lhs of target keymap. ---@param opts? vim.keymap.del.Opts Options for this deletion. Same as `opts` in `vim.keymap.del`. function M.del(name, lhs, opts) - vim.validate("name", name, "string") - vim.validate("lhs", lhs, "string") - vim.validate("opts", opts, "table", true) + validate("name", name, "string") + validate("lhs", lhs, "string") + validate("opts", opts, "table", true) if not M.state.submode_to_user_mappings[name][lhs] then return @@ -170,7 +171,7 @@ end ---Enter the submode. ---@param name string Name of submode to enter. function M.enter(name) - vim.validate("name", name, "string") + validate("name", name, "string") local state = M.state local opts = state.submode_to_opts[name] diff --git a/lua/submode/snapshot/keymap.lua b/lua/submode/snapshot/keymap.lua index fd351e4..fd4c3cc 100644 --- a/lua/submode/snapshot/keymap.lua +++ b/lua/submode/snapshot/keymap.lua @@ -17,17 +17,19 @@ ---@field callback function? ---@field replace_keycodes boolean? +local validate = require("submode.validate").validate + ---@param mode ShortenMode ---@param buf integer? Buffer handle or 0 for current buffer, or nil for global. ---@return KeymapInfo[] local function get_keymap(mode, buf) - vim.validate("mode", mode, function(s) + validate("mode", mode, function(s) -- stylua: ignore return vim.list_contains({ "n", "v", "x", "s", "o", "i", "l", "c", "t", "!", "" }, s) end, "n, v, x, s, o, i, l, c, t, ! or ''") - vim.validate("buf", buf, { "number", "nil" }) + validate("buf", buf, { "number", "nil" }) local getter = function(m) if buf then diff --git a/lua/submode/validate.lua b/lua/submode/validate.lua new file mode 100644 index 0000000..a45d1e7 --- /dev/null +++ b/lua/submode/validate.lua @@ -0,0 +1,31 @@ +local M = {} + +--- Thin wrapper for form-1 vim.validate +function M.validate(name, value, validator, optional, message) + if vim.fn.has("nvim-0.11") == 1 then + -- spec-style validate is deprecated in nvim-0.11. + vim.validate(name, value, validator, optional, message) + else + if type(optional) == "boolean" then + if optional then + vim.validate { + [name] = { value, validator, true }, + } + else + vim.validate { + [name] = { value, validator, message }, + } + end + elseif type(optional) == "string" then + vim.validate { + [name] = { value, validator, optional }, + } + else + vim.validate { + [name] = { value, validator }, + } + end + end +end + +return M