fix: default mappings can now be properly disabled, independent of load order

This commit is contained in:
Leon Heidelbach 2022-05-14 01:31:13 +02:00 committed by siduck
parent c7a4d4e337
commit 96cd08ef45
1 changed files with 50 additions and 7 deletions

View File

@ -31,6 +31,7 @@ nvchad.load_config = function()
-- 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
local user_config = require "custom.chadrc" local user_config = require "custom.chadrc"
if type(user_config) == "table" then if type(user_config) == "table" then
conf.mappings = nvchad.prune_key_map(conf.mappings, user_config.mappings, { "mode_opts" })
conf = vim.tbl_deep_extend("force", conf, user_config) conf = vim.tbl_deep_extend("force", conf, user_config)
else else
error "User config (chadrc.lua) *must* return a table!" error "User config (chadrc.lua) *must* return a table!"
@ -40,6 +41,46 @@ nvchad.load_config = function()
return conf return conf
end end
-- reduces a given keymap to a table of modes each containing a list of key maps for that mode
nvchad.reduce_key_map = function(key_map, ignore_modes)
local prune_keys = {}
for _, modes in pairs(key_map) do
for mode, mappings in pairs(modes) do
if not vim.tbl_contains(ignore_modes, mode) then
prune_keys[mode] = prune_keys[mode] and prune_keys[mode] or {}
prune_keys[mode] = vim.list_extend(prune_keys[mode], vim.tbl_keys(mappings))
end
end
end
return prune_keys
end
-- prune keys from a key map table by matching against another key map table
nvchad.prune_key_map = function(key_map, prune_map, ignore_modes)
if not prune_map then return key_map end
if not key_map then return prune_map end
local prune_keys = nvchad.reduce_key_map(prune_map, ignore_modes)
for ext, modes in pairs(key_map) do
for mode, mappings in pairs(modes) do
if not vim.tbl_contains(ignore_modes, mode) then
if prune_keys[mode] then
-- filter mappings table so that only keys that are not in user_mappings are left
local filtered_mappings = {}
for k, v in pairs(mappings) do
if not vim.tbl_contains(prune_keys[mode], k) then
filtered_mappings[k] = v
end
end
key_map[ext][mode] = filtered_mappings
end
end
end
end
return key_map
end
nvchad.map = function(mode, keys, command, opt) nvchad.map = function(mode, keys, command, opt)
local options = { silent = true } local options = { silent = true }
@ -58,13 +99,14 @@ nvchad.map = function(mode, keys, command, opt)
end end
-- For those who disabled whichkey -- For those who disabled whichkey
nvchad.no_WhichKey_map = function() nvchad.no_WhichKey_map = function()
local mappings = nvchad.load_config().mappings local mappings = nvchad.load_config().mappings
local ignore_modes = { "mode_opts" }
for key, _ in pairs(mappings) do for _, value in pairs(mappings) do
for mode, _ in pairs(mappings[key]) do for mode, keymap in pairs(value) do
for keybind, cmd in pairs(mappings[key][mode]) do if not vim.tbl_contains(ignore_modes, mode) then
for keybind, cmd in pairs(keymap) do
-- disabled keys will not have cmd set -- disabled keys will not have cmd set
if cmd ~= "" then if cmd ~= "" then
nvchad.map(mode, keybind, cmd[1]) nvchad.map(mode, keybind, cmd[1])
@ -72,6 +114,7 @@ nvchad.no_WhichKey_map = function()
end end
end end
end end
end
require("plugins.configs.others").misc_mappings() require("plugins.configs.others").misc_mappings()
end end