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 return self
end end
---@param value string ---@param _value string
---@protected ---@protected
function M:_add(value) end function M:_add(_value) end
---@param value string ---@param _value string
---@protected ---@protected
function M:_del(value) end function M:_del(_value) end
---@return string ---@param plugin LazyPlugin
function M:key(value) function M:values(plugin)
return value ---@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 end
---@param plugin LazyPlugin ---@param plugin LazyPlugin
function M:add(plugin) function M:add(plugin)
local values = {} for key, value in pairs(self:values(plugin)) do
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
if not self.active[key] then if not self.active[key] then
self.active[key] = {} self.active[key] = {}
self:_add(value) self:_add(value)
@ -94,14 +94,7 @@ end
---@param plugin LazyPlugin ---@param plugin LazyPlugin
function M:del(plugin) function M:del(plugin)
local values = {} for key, value in pairs(self:values(plugin)) do
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)
if self.active[key] and self.active[key][plugin.name] then if self.active[key] and self.active[key][plugin.name] then
self.active[key][plugin.name] = nil self.active[key][plugin.name] = nil
if vim.tbl_isempty(self.active[key]) then if vim.tbl_isempty(self.active[key]) then

View File

@ -9,6 +9,7 @@ local Loader = require("lazy.core.loader")
---@field noremap? boolean ---@field noremap? boolean
---@field remap? boolean ---@field remap? boolean
---@field expr? boolean ---@field expr? boolean
---@field id string
---@class LazyKeysHandler:LazyHandler ---@class LazyKeysHandler:LazyHandler
local M = {} local M = {}
@ -54,48 +55,56 @@ function M.parse(value)
local ret = vim.deepcopy(value) local ret = vim.deepcopy(value)
ret = type(ret) == "string" and { ret } or ret --[[@as LazyKeys]] ret = type(ret) == "string" and { ret } or ret --[[@as LazyKeys]]
ret.mode = ret.mode or "n" 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 return ret
end 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) function M.opts(keys)
local opts = {} local opts = {}
for k, v in pairs(keys) do 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 opts[k] = v
end end
end end
return opts return opts
end end
---@return string ---@param keys LazyKeys
function M:key(value) function M:_add(keys)
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)
local lhs = keys[1] local lhs = keys[1]
local opts = M.opts(keys) local opts = M.opts(keys)
vim.keymap.set(keys.mode, lhs, function() vim.keymap.set(keys.mode, lhs, function()
local key = self:key(value) local plugins = self.active[keys.id]
local plugins = self.active[key]
-- always delete the mapping immediately to prevent recursive mappings -- always delete the mapping immediately to prevent recursive mappings
self:_del(value) self:_del(keys)
self.active[key] = nil self.active[keys.id] = nil
Util.track({ keys = lhs }) Util.track({ keys = lhs })
Loader.load(plugins, { keys = lhs }) Loader.load(plugins, { keys = lhs })
@ -104,9 +113,8 @@ function M:_add(value)
end, opts) end, opts)
end end
---@param value string|LazyKeys ---@param keys LazyKeys
function M:_del(value) function M:_del(keys)
local keys = M.parse(value)
pcall(vim.keymap.del, keys.mode, keys[1]) pcall(vim.keymap.del, keys.mode, keys[1])
if keys[2] then if keys[2] then
vim.keymap.set(keys.mode, keys[1], keys[2], M.opts(keys)) vim.keymap.set(keys.mode, keys[1], keys[2], M.opts(keys))