feat: LazyKeysBase Support opts and rhs_maker

opts, a table of key options for vim.keymap.set.
rhs_maker, a function that returns a rhs. run after plugin loads, it could produce rhs dynamically.
This commit is contained in:
HC 2023-11-04 06:10:38 +08:00
commit b1213d426f
2 changed files with 24 additions and 3 deletions

View file

@ -8,6 +8,8 @@ local Util = require("lazy.core.util")
---@field expr? boolean
---@field nowait? boolean
---@field ft? string|string[]
---@field rhs_maker? fun():string|fun()
---@field opts? table
---@class LazyKeysSpec: LazyKeysBase
---@field [1] string lhs
@ -24,7 +26,7 @@ local Util = require("lazy.core.util")
---@class LazyKeysHandler:LazyHandler
local M = {}
local skip = { mode = true, id = true, ft = true, rhs = true, lhs = true }
local skip = { mode = true, id = true, ft = true, rhs = true, lhs = true , opts=true, rhs_maker=true}
---@param value string|LazyKeysSpec
---@param mode? string
@ -94,6 +96,13 @@ function M.opts(keys)
opts[k] = v
end
end
if keys.opts then
opts=vim.tbl_deep_extend("force",opts,keys.opts)
if opts.ft then
keys.ft=opts.ft
opts.ft=nil
end
end
return opts
end
@ -114,7 +123,15 @@ function M:_add(keys)
if plugins then
local name = M.to_string(keys)
Util.track({ keys = name })
if keys.rhs_maker then
Loader.load(plugins, { keys = name }, nil, function()
keys.rhs=keys.rhs_maker()
keys.rhs_maker=nil
self:_set(keys, buf)
end)
else
Loader.load(plugins, { keys = name })
end
Util.track()
end

View file

@ -167,7 +167,8 @@ end
---@param plugins string|LazyPlugin|string[]|LazyPlugin[]
---@param reason {[string]:string}
---@param opts? {force:boolean} when force is true, we skip the cond check
function M.load(plugins, reason, opts)
---@param afterload? function a function that runs after load
function M.load(plugins, reason, opts, afterload)
---@diagnostic disable-next-line: cast-local-type
plugins = (type(plugins) == "string" or plugins.name) and { plugins } or plugins
---@cast plugins (string|LazyPlugin)[]
@ -185,6 +186,9 @@ function M.load(plugins, reason, opts)
end
if plugin and not plugin._.loaded then
M._load(plugin, reason, opts)
if afterload then
afterload()
end
end
end
end