Skip to content

Commit

Permalink
feat(keys): refactor code and allow disabling keymaps per mode. mode …
Browse files Browse the repository at this point in the history
…no longer needs to be exactly the same in order to disable.
  • Loading branch information
folke committed Oct 8, 2023
1 parent 62745a7 commit b79099c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 35 deletions.
3 changes: 3 additions & 0 deletions lua/lazy/core/handler/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local Config = require("lazy.core.config")
---@field type LazyHandlerTypes
---@field extends? LazyHandler
---@field active table<string,table<string,string>>
---@field managed table<string,string>
---@field super LazyHandler
local M = {}

Expand Down Expand Up @@ -64,6 +65,7 @@ function M.new(type)
local self = setmetatable({}, { __index = setmetatable(handler, { __index = super }) })
self.super = super
self.active = {}
self.managed = {}
self.type = type
return self
end
Expand Down Expand Up @@ -94,6 +96,7 @@ function M:add(plugin)
self.active[key] = {}
self:_add(value)
end
self.managed[key] = self.managed[key]
self.active[key][plugin.name] = plugin.name
end
end
Expand Down
96 changes: 63 additions & 33 deletions lua/lazy/core/handler/keys.lua
Original file line number Diff line number Diff line change
@@ -1,60 +1,89 @@
local Util = require("lazy.core.util")
local Loader = require("lazy.core.loader")

---@class LazyKeys
---@field [1] string lhs
---@field [2]? string|fun()|false rhs
---@class LazyKeysBase
---@field desc? string
---@field mode? string|string[]
---@field noremap? boolean
---@field remap? boolean
---@field expr? boolean
---@field nowait? boolean
---@field ft? string|string[]

---@class LazyKeysSpec: LazyKeysBase
---@field [1] string lhs
---@field [2]? string|fun()|false rhs
---@field mode? string|string[]

---@class LazyKeys: LazyKeysBase
---@field lhs string lhs
---@field rhs? string|fun() rhs
---@field mode? string
---@field id string

---@class LazyKeysHandler:LazyHandler
local M = {}

---@param value string|LazyKeys
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 = vim.api.nvim_replace_termcodes(ret[1] or "", true, true, true)
if ret.mode then
local mode = ret.mode
if type(mode) == "table" then
---@cast mode string[]
table.sort(mode)
mode = table.concat(mode, ", ")
end
if mode ~= "n" then
ret.id = ret.id .. " (" .. mode .. ")"
end
---@param value string|LazyKeysSpec
---@param mode? string
---@return LazyKeys
function M.parse(value, mode)
value = type(value) == "string" and { value } or value --[[@as LazyKeysSpec]]
local ret = vim.deepcopy(value) --[[@as LazyKeys]]
ret.lhs = ret[1] or ""
ret.rhs = ret[2]
---@diagnostic disable-next-line: no-unknown
ret[1] = nil
---@diagnostic disable-next-line: no-unknown
ret[2] = nil
ret.mode = mode or "n"
ret.id = vim.api.nvim_replace_termcodes(ret.lhs, true, true, true)
if ret.mode ~= "n" then
ret.id = ret.id .. " (" .. ret.mode .. ")"
end
return ret
end

---@param lhs string
---@param mode? string
function M:have(lhs, mode)
local keys = M.parse(lhs, mode)
return self.managed[keys.id]
end

---@param plugin LazyPlugin
function M:values(plugin)
---@type table<string,any>
return M.resolve(plugin.keys)
end

---@param spec? (string|LazyKeysSpec)[]
function M.resolve(spec)
---@type LazyKeys[]
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 or keys[2] == false then
values[keys.id] = nil
else
values[keys.id] = keys
for _, value in ipairs(spec or {}) do
value = type(value) == "string" and { value } or value --[[@as LazyKeysSpec]]
value.mode = value.mode or "n"
local modes = (type(value.mode) == "table" and value.mode or { value.mode }) --[=[@as string[]]=]
for _, mode in ipairs(modes) do
local keys = M.parse(value, mode)
if keys.rhs == vim.NIL or keys.rhs == false then
values[keys.id] = nil
else
values[keys.id] = keys
end
end
end
return values
end

---@param keys LazyKeys
function M.opts(keys)
local opts = {}
local skip = { mode = true, id = true, ft = true, rhs = true, lhs = true }
local opts = {} ---@type LazyKeysBase
---@diagnostic disable-next-line: no-unknown
for k, v in pairs(keys) do
if type(k) ~= "number" and k ~= "mode" and k ~= "id" and k ~= "ft" then
if type(k) ~= "number" and not skip[k] then
---@diagnostic disable-next-line: no-unknown
opts[k] = v
end
end
Expand All @@ -63,7 +92,7 @@ end

---@param keys LazyKeys
function M:_add(keys)
local lhs = keys[1]
local lhs = keys.lhs
local opts = M.opts(keys)

---@param buf? number
Expand Down Expand Up @@ -121,7 +150,7 @@ end
-- mapping when needed
---@param keys LazyKeys
function M:_del(keys)
pcall(vim.keymap.del, keys.mode, keys[1])
pcall(vim.keymap.del, keys.mode, keys.lhs)
-- make sure to create global mappings when needed
-- buffer-local mappings are managed by lazy
if not keys.ft then
Expand All @@ -133,10 +162,11 @@ end
---@param keys LazyKeys
---@param buf number?
function M:_set(keys, buf)
if keys[2] then
if keys.rhs then
local opts = M.opts(keys)
---@diagnostic disable-next-line: inject-field
opts.buffer = buf
vim.keymap.set(keys.mode, keys[1], keys[2], opts)
vim.keymap.set(keys.mode, keys.lhs, keys.rhs, opts)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lua/lazy/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
---@field event? string[]
---@field cmd? string[]
---@field ft? string[]
---@field keys? (string|LazyKeys)[]
---@field keys? (string|LazyKeysSpec)[]
---@field module? false

---@class LazyPluginRef
Expand Down
2 changes: 1 addition & 1 deletion lua/lazy/view/render.lua
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ function M:reason(reason, opts)
value = value:match("User (.*)") or value
end
if key == "keys" then
value = type(value) == "string" and value or value[1]
value = type(value) == "string" and value or value.lhs
end
local hl = "LazyReason" .. key:sub(1, 1):upper() .. key:sub(2)
local icon = Config.options.ui.icons[key]
Expand Down

0 comments on commit b79099c

Please sign in to comment.