mirror of
https://github.com/CompeyDev/my-nvim-setup.git
synced 2024-12-12 21:00:38 +00:00
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
|
||||||
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
|
||||||
end
|
for mode, section_keys in pairs(mappings) do
|
||||||
end
|
if not keys_to_disable[mode] then
|
||||||
|
keys_to_disable[mode] = {}
|
||||||
local default_mappings = require("core.default_config").mappings
|
end
|
||||||
|
for k, _ in pairs(section_keys) do
|
||||||
-- remove user_maps from default mapping table
|
keys_to_disable[mode][k] = true
|
||||||
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
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
M.load_mappings = function(mappings, mapping_opt)
|
-- make a copy as we need to modify default_mappings
|
||||||
-- set mapping function with/without whichkey
|
for section_name, section_mappings in pairs(default_mappings) do
|
||||||
local set_maps
|
for mode, mode_mappings in pairs(section_mappings) do
|
||||||
local whichkey_exists, wk = pcall(require, "which-key")
|
mode_mappings = (type(mode_mappings) == "table" and mode_mappings) or {}
|
||||||
|
for k, _ in pairs(mode_mappings) do
|
||||||
if whichkey_exists then
|
-- if key if found then remove from default_mappings
|
||||||
set_maps = function(keybind, mapping_info, opts)
|
if keys_to_disable[mode] and keys_to_disable[mode][k] then
|
||||||
wk.register({ [keybind] = mapping_info }, opts)
|
default_mappings[section_name][mode][k] = nil
|
||||||
end
|
|
||||||
else
|
|
||||||
set_maps = function(keybind, mapping_info, opts)
|
|
||||||
local mode = opts.mode
|
|
||||||
opts.mode = nil
|
|
||||||
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
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
vim.defer_fn(function()
|
return default_mappings
|
||||||
set_mappings()
|
end
|
||||||
end, 0)
|
|
||||||
|
M.load_mappings = function(section, mapping_opt)
|
||||||
|
local function set_section_map(section_values)
|
||||||
|
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)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local mappings = require("core.utils").load_config().mappings
|
||||||
|
|
||||||
|
if type(section) == "string" then
|
||||||
|
mappings[section]["plugin"] = nil
|
||||||
|
mappings = { mappings[section] }
|
||||||
|
end
|
||||||
|
|
||||||
|
for _, sect in pairs(mappings) do
|
||||||
|
set_section_map(sect)
|
||||||
|
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 a new issue