utils: Improve mapping function | Properly lazy load whichkey
just add desc with normal vim.keymap.set function whichkey will automatically use the descriptions
This commit is contained in:
parent
01b17d2ad6
commit
a20fc560f3
3 changed files with 57 additions and 89 deletions
|
@ -15,32 +15,15 @@ M.close_buffer = function(bufnr)
|
|||
end
|
||||
end
|
||||
|
||||
-- store mapping section names which have plugin key = true
|
||||
local lazyload_mappings_list = {}
|
||||
|
||||
local add_mappingSection_toList = function()
|
||||
local default_mappings = require("core.default_config").mappings
|
||||
|
||||
-- store mapping section name into a table
|
||||
for section_name, section_mappings in pairs(default_mappings) do
|
||||
if section_mappings.plugin then
|
||||
lazyload_mappings_list[section_name] = true
|
||||
section_mappings.plugin = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
M.load_config = function()
|
||||
local config = require "core.default_config"
|
||||
local chadrc_exists, chadrc = pcall(require, "custom.chadrc")
|
||||
|
||||
add_mappingSection_toList()
|
||||
|
||||
if chadrc_exists then
|
||||
-- merge user config if it exists and is a table; otherwise display an error
|
||||
if type(chadrc) == "table" then
|
||||
M.remove_default_keys(chadrc.mappings or {})
|
||||
config = merge_tb("force", config, chadrc)
|
||||
config.mappings = M.remove_disabled_keys(chadrc.mappings, config.mappings)
|
||||
config = merge_tb("force", config, chadrc) or {}
|
||||
else
|
||||
error "chadrc must return a table!"
|
||||
end
|
||||
|
@ -50,77 +33,71 @@ M.load_config = function()
|
|||
return config
|
||||
end
|
||||
|
||||
M.remove_default_keys = function(user_mappings)
|
||||
local user_keys = {}
|
||||
local user_sections = vim.tbl_keys(user_mappings)
|
||||
|
||||
-- push user_map keys in user_keys table
|
||||
for _, section in ipairs(user_sections) do
|
||||
user_keys = vim.tbl_deep_extend("force", user_keys, user_mappings[section])
|
||||
M.remove_disabled_keys = function(chadrc_mappings, default_mappings)
|
||||
if not chadrc_mappings then
|
||||
return default_mappings
|
||||
end
|
||||
|
||||
local function disable_key(mode, keybind, mode_mapping)
|
||||
if user_keys[mode] and user_keys[mode][keybind] then
|
||||
mode_mapping[keybind] = nil
|
||||
-- store keys in a array with true value to compare
|
||||
local keys_to_disable = {}
|
||||
for _, mappings in pairs(chadrc_mappings) do
|
||||
for mode, section_keys in pairs(mappings) do
|
||||
if not keys_to_disable[mode] then
|
||||
keys_to_disable[mode] = {}
|
||||
end
|
||||
for k, _ in pairs(section_keys) do
|
||||
keys_to_disable[mode][k] = true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local default_mappings = require("core.default_config").mappings
|
||||
-- make a copy as we need to modify default_mappings
|
||||
for section_name, section_mappings in pairs(default_mappings) do
|
||||
for mode, mode_mappings in pairs(section_mappings) do
|
||||
mode_mappings = (type(mode_mappings) == "table" and mode_mappings) or {}
|
||||
for k, _ in pairs(mode_mappings) do
|
||||
-- if key if found then remove from default_mappings
|
||||
if keys_to_disable[mode] and keys_to_disable[mode][k] then
|
||||
default_mappings[section_name][mode][k] = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- remove user_maps from default mapping table
|
||||
for _, section_mappings in pairs(default_mappings) do
|
||||
for mode, mode_mapping in pairs(section_mappings) do
|
||||
for keybind, _ in pairs(mode_mapping) do
|
||||
disable_key(mode, keybind, mode_mapping)
|
||||
end
|
||||
end
|
||||
end
|
||||
return default_mappings
|
||||
end
|
||||
|
||||
M.load_mappings = function(mappings, mapping_opt)
|
||||
-- set mapping function with/without whichkey
|
||||
local set_maps
|
||||
local whichkey_exists, wk = pcall(require, "which-key")
|
||||
|
||||
if whichkey_exists then
|
||||
set_maps = function(keybind, mapping_info, opts)
|
||||
wk.register({ [keybind] = mapping_info }, opts)
|
||||
M.load_mappings = function(section, mapping_opt)
|
||||
local function set_section_map(section_values)
|
||||
if section_values.plugin then
|
||||
return
|
||||
end
|
||||
else
|
||||
set_maps = function(keybind, mapping_info, opts)
|
||||
local mode = opts.mode
|
||||
opts.mode = nil
|
||||
section_values.plugin = nil
|
||||
|
||||
for mode, mode_values in pairs(section_values) do
|
||||
local default_opts = merge_tb("force", { mode = mode }, mapping_opt or {})
|
||||
for keybind, mapping_info in pairs(mode_values) do
|
||||
-- merge default + user opts
|
||||
local opts = merge_tb("force", default_opts, mapping_info.opts or {})
|
||||
|
||||
mapping_info.opts, opts.mode = nil, nil
|
||||
opts.desc = mapping_info[2]
|
||||
|
||||
vim.keymap.set(mode, keybind, mapping_info[1], opts)
|
||||
end
|
||||
end
|
||||
|
||||
local mappings_tb = M.load_config().mappings
|
||||
mappings = vim.deepcopy(type(mappings) == "string" and { mappings_tb[mappings] } or mappings_tb)
|
||||
|
||||
local function set_mappings()
|
||||
for name, section in pairs(mappings) do
|
||||
-- skip mapping section with plugin=true
|
||||
if not lazyload_mappings_list[name] then
|
||||
for mode, mode_values in pairs(section) do
|
||||
for keybind, mapping_info in pairs(mode_values) do
|
||||
-- merge default + user opts
|
||||
local default_opts = merge_tb("force", { mode = mode }, mapping_opt or {})
|
||||
local opts = merge_tb("force", default_opts, mapping_info.opts or {})
|
||||
|
||||
if mapping_info.opts then
|
||||
mapping_info.opts = nil
|
||||
end
|
||||
|
||||
set_maps(keybind, mapping_info, opts)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
local mappings = require("core.utils").load_config().mappings
|
||||
|
||||
if type(section) == "string" then
|
||||
mappings[section]["plugin"] = nil
|
||||
mappings = { mappings[section] }
|
||||
end
|
||||
|
||||
vim.defer_fn(function()
|
||||
set_mappings()
|
||||
end, 0)
|
||||
for _, sect in pairs(mappings) do
|
||||
set_section_map(sect)
|
||||
end
|
||||
end
|
||||
|
||||
-- remove plugins defined in chadrc
|
||||
|
|
|
@ -38,14 +38,4 @@ local options = {
|
|||
|
||||
options = require("core.utils").load_override(options, "folke/which-key.nvim")
|
||||
|
||||
local utils = require "core.utils"
|
||||
|
||||
local mappings = utils.load_config().mappings
|
||||
local mapping_groups = { groups = vim.deepcopy(mappings.groups) }
|
||||
|
||||
mappings.disabled = nil
|
||||
mappings.groups = nil
|
||||
|
||||
utils.load_mappings(mapping_groups)
|
||||
|
||||
wk.setup(options)
|
||||
|
|
|
@ -204,6 +204,7 @@ local plugins = {
|
|||
["folke/which-key.nvim"] = {
|
||||
disable = true,
|
||||
module = "which-key",
|
||||
keys = "<leader>",
|
||||
config = function()
|
||||
require "plugins.configs.whichkey"
|
||||
end,
|
||||
|
|
Loading…
Reference in a new issue