Skip to content

Commit

Permalink
fix: respect neovim version for calling vim.validate (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
pogyomo authored Jan 6, 2025
1 parent fdc87db commit 013e9ba
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 16 deletions.
29 changes: 15 additions & 14 deletions lua/submode/init.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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]
Expand Down
6 changes: 4 additions & 2 deletions lua/submode/snapshot/keymap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions lua/submode/validate.lua
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 013e9ba

Please sign in to comment.