feat(keys): allow overriding a keys value to `vim.NIL` to not add the key

This commit is contained in:
Folke Lemaitre 2023-01-16 10:16:35 +01:00
parent 984008f7ae
commit fdf0332fe1
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
2 changed files with 51 additions and 50 deletions

View File

@ -62,28 +62,28 @@ function M.new(type)
return self
end
---@param value string
---@param _value string
---@protected
function M:_add(value) end
function M:_add(_value) end
---@param value string
---@param _value string
---@protected
function M:_del(value) end
function M:_del(_value) end
---@return string
function M:key(value)
return value
---@param plugin LazyPlugin
function M:values(plugin)
---@type table<string,any>
local values = {}
---@diagnostic disable-next-line: no-unknown
for _, value in ipairs(plugin[self.type] or {}) do
values[value] = value
end
return values
end
---@param plugin LazyPlugin
function M:add(plugin)
local values = {}
for _, value in ipairs(plugin[self.type] or {}) do
local key = self:key(value)
values[key] = value
end
for key, value in pairs(values) do
for key, value in pairs(self:values(plugin)) do
if not self.active[key] then
self.active[key] = {}
self:_add(value)
@ -94,14 +94,7 @@ end
---@param plugin LazyPlugin
function M:del(plugin)
local values = {}
for _, value in ipairs(plugin[self.type] or {}) do
local key = self:key(value)
values[key] = value
end
for key, value in pairs(values) do
local key = self:key(value)
for key, value in pairs(self:values(plugin)) do
if self.active[key] and self.active[key][plugin.name] then
self.active[key][plugin.name] = nil
if vim.tbl_isempty(self.active[key]) then

View File

@ -9,6 +9,7 @@ local Loader = require("lazy.core.loader")
---@field noremap? boolean
---@field remap? boolean
---@field expr? boolean
---@field id string
---@class LazyKeysHandler:LazyHandler
local M = {}
@ -54,48 +55,56 @@ function M.parse(value)
local ret = vim.deepcopy(value)
ret = type(ret) == "string" and { ret } or ret --[[@as LazyKeys]]
ret.mode = ret.mode or "n"
ret.id = (ret[1] or "")
if ret.mode then
local mode = ret.mode
if type(mode) == "table" then
---@cast mode string[]
table.sort(mode)
ret.id = ret.id .. " (" .. table.concat(mode, ", ") .. ")"
elseif mode ~= "n" then
ret.id = ret.id .. " (" .. mode .. ")"
end
end
return ret
end
---@param plugin LazyPlugin
function M:values(plugin)
---@type table<string,any>
local values = {}
---@diagnostic disable-next-line: no-unknown
for _, value in ipairs(plugin[self.type] or {}) do
local keys = M.parse(value)
if keys[2] == vim.NIL then
values[keys.id] = nil
else
values[keys.id] = keys
end
end
return values
end
function M.opts(keys)
local opts = {}
for k, v in pairs(keys) do
if type(k) ~= "number" and k ~= "mode" then
if type(k) ~= "number" and k ~= "mode" and k ~= "id" then
opts[k] = v
end
end
return opts
end
---@return string
function M:key(value)
if type(value) == "string" then
return value
end
local mode = value.mode or { "n" }
if type(mode) == "string" then
mode = { mode }
end
---@type string
local ret = value[1]
if #mode > 0 then
ret = table.concat(mode, ",") .. ": " .. ret
end
return ret
end
---@param value string|LazyKeys
function M:_add(value)
local keys = M.parse(value)
---@param keys LazyKeys
function M:_add(keys)
local lhs = keys[1]
local opts = M.opts(keys)
vim.keymap.set(keys.mode, lhs, function()
local key = self:key(value)
local plugins = self.active[key]
local plugins = self.active[keys.id]
-- always delete the mapping immediately to prevent recursive mappings
self:_del(value)
self.active[key] = nil
self:_del(keys)
self.active[keys.id] = nil
Util.track({ keys = lhs })
Loader.load(plugins, { keys = lhs })
@ -104,9 +113,8 @@ function M:_add(value)
end, opts)
end
---@param value string|LazyKeys
function M:_del(value)
local keys = M.parse(value)
---@param keys LazyKeys
function M:_del(keys)
pcall(vim.keymap.del, keys.mode, keys[1])
if keys[2] then
vim.keymap.set(keys.mode, keys[1], keys[2], M.opts(keys))