Skip to content

Commit

Permalink
feat(keys): allow overriding a keys value to vim.NIL to not add the…
Browse files Browse the repository at this point in the history
… key
  • Loading branch information
folke committed Jan 16, 2023
1 parent 984008f commit fdf0332
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 49 deletions.
35 changes: 14 additions & 21 deletions lua/lazy/core/handler/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

---@return string
function M:key(value)
return value
end
function M:_del(_value) end

---@param plugin LazyPlugin
function M:add(plugin)
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 key = self:key(value)
values[key] = value
values[value] = value
end
return values
end

for key, value in pairs(values) do
---@param plugin LazyPlugin
function M:add(plugin)
for key, value in pairs(self:values(plugin)) do
if not self.active[key] then
self.active[key] = {}
self:_add(value)
Expand All @@ -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
Expand Down
64 changes: 36 additions & 28 deletions lua/lazy/core/handler/keys.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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 })
Expand All @@ -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))
Expand Down

0 comments on commit fdf0332

Please sign in to comment.