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
|
@ -15,32 +15,15 @@ M.close_buffer = function(bufnr)
|
||||||
end
|
end
|
||||||
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()
|
M.load_config = function()
|
||||||
local config = require "core.default_config"
|
local config = require "core.default_config"
|
||||||
local chadrc_exists, chadrc = pcall(require, "custom.chadrc")
|
local chadrc_exists, chadrc = pcall(require, "custom.chadrc")
|
||||||
|
|
||||||
add_mappingSection_toList()
|
|
||||||
|
|
||||||
if chadrc_exists then
|
if chadrc_exists then
|
||||||
-- merge user config if it exists and is a table; otherwise display an error
|
-- merge user config if it exists and is a table; otherwise display an error
|
||||||
if type(chadrc) == "table" then
|
if type(chadrc) == "table" then
|
||||||
M.remove_default_keys(chadrc.mappings or {})
|
config.mappings = M.remove_disabled_keys(chadrc.mappings, config.mappings)
|
||||||
config = merge_tb("force", config, chadrc)
|
config = merge_tb("force", config, chadrc) or {}
|
||||||
else
|
else
|
||||||
error "chadrc must return a table!"
|
error "chadrc must return a table!"
|
||||||
end
|
end
|
||||||
|
@ -50,77 +33,71 @@ M.load_config = function()
|
||||||
return config
|
return config
|
||||||
end
|
end
|
||||||
|
|
||||||
M.remove_default_keys = function(user_mappings)
|
M.remove_disabled_keys = function(chadrc_mappings, default_mappings)
|
||||||
local user_keys = {}
|
if not chadrc_mappings then
|
||||||
local user_sections = vim.tbl_keys(user_mappings)
|
return default_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])
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function disable_key(mode, keybind, mode_mapping)
|
-- store keys in a array with true value to compare
|
||||||
if user_keys[mode] and user_keys[mode][keybind] then
|
local keys_to_disable = {}
|
||||||
mode_mapping[keybind] = nil
|
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
|
||||||
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
|
||||||
-- remove user_maps from default mapping table
|
for mode, mode_mappings in pairs(section_mappings) do
|
||||||
for _, section_mappings in pairs(default_mappings) do
|
mode_mappings = (type(mode_mappings) == "table" and mode_mappings) or {}
|
||||||
for mode, mode_mapping in pairs(section_mappings) do
|
for k, _ in pairs(mode_mappings) do
|
||||||
for keybind, _ in pairs(mode_mapping) do
|
-- if key if found then remove from default_mappings
|
||||||
disable_key(mode, keybind, mode_mapping)
|
if keys_to_disable[mode] and keys_to_disable[mode][k] then
|
||||||
|
default_mappings[section_name][mode][k] = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
M.load_mappings = function(mappings, mapping_opt)
|
return default_mappings
|
||||||
-- 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)
|
|
||||||
end
|
end
|
||||||
else
|
|
||||||
set_maps = function(keybind, mapping_info, opts)
|
M.load_mappings = function(section, mapping_opt)
|
||||||
local mode = opts.mode
|
local function set_section_map(section_values)
|
||||||
opts.mode = nil
|
if section_values.plugin then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
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)
|
vim.keymap.set(mode, keybind, mapping_info[1], opts)
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
set_maps(keybind, mapping_info, opts)
|
local mappings = require("core.utils").load_config().mappings
|
||||||
end
|
|
||||||
end
|
if type(section) == "string" then
|
||||||
end
|
mappings[section]["plugin"] = nil
|
||||||
end
|
mappings = { mappings[section] }
|
||||||
end
|
end
|
||||||
|
|
||||||
vim.defer_fn(function()
|
for _, sect in pairs(mappings) do
|
||||||
set_mappings()
|
set_section_map(sect)
|
||||||
end, 0)
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- remove plugins defined in chadrc
|
-- remove plugins defined in chadrc
|
||||||
|
|
|
@ -38,14 +38,4 @@ local options = {
|
||||||
|
|
||||||
options = require("core.utils").load_override(options, "folke/which-key.nvim")
|
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)
|
wk.setup(options)
|
||||||
|
|
|
@ -204,6 +204,7 @@ local plugins = {
|
||||||
["folke/which-key.nvim"] = {
|
["folke/which-key.nvim"] = {
|
||||||
disable = true,
|
disable = true,
|
||||||
module = "which-key",
|
module = "which-key",
|
||||||
|
keys = "<leader>",
|
||||||
config = function()
|
config = function()
|
||||||
require "plugins.configs.whichkey"
|
require "plugins.configs.whichkey"
|
||||||
end,
|
end,
|
||||||
|
|
Loading…
Reference in New Issue