2022-05-29 06:37:47 -04:00
|
|
|
local M = {}
|
2022-05-29 05:38:53 -04:00
|
|
|
local merge_tb = vim.tbl_deep_extend
|
|
|
|
|
2022-05-29 06:37:47 -04:00
|
|
|
M.load_config = function()
|
2022-07-22 12:00:00 -04:00
|
|
|
local config = require "core.default_config"
|
2022-12-17 06:25:30 -05:00
|
|
|
local chadrc_path = vim.api.nvim_get_runtime_file("lua/custom/chadrc.lua", false)[1]
|
2022-07-22 12:00:00 -04:00
|
|
|
|
2022-12-17 06:25:30 -05:00
|
|
|
if chadrc_path then
|
|
|
|
local chadrc = dofile(chadrc_path)
|
2024-01-28 11:03:38 -05:00
|
|
|
|
2023-06-14 22:33:01 -04:00
|
|
|
config.mappings = M.remove_disabled_keys(chadrc.mappings, config.mappings)
|
2022-12-17 06:25:30 -05:00
|
|
|
config = merge_tb("force", config, chadrc)
|
2023-06-14 22:33:01 -04:00
|
|
|
config.mappings.disabled = nil
|
2022-07-22 12:00:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
return config
|
2021-08-25 01:28:56 -04:00
|
|
|
end
|
|
|
|
|
2022-08-11 06:10:26 -04:00
|
|
|
M.remove_disabled_keys = function(chadrc_mappings, default_mappings)
|
|
|
|
if not chadrc_mappings then
|
|
|
|
return default_mappings
|
2022-07-22 12:00:00 -04:00
|
|
|
end
|
|
|
|
|
2022-08-11 06:10:26 -04:00
|
|
|
-- 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
|
2022-08-12 08:27:32 -04:00
|
|
|
section_keys = (type(section_keys) == "table" and section_keys) or {}
|
2022-08-11 06:10:26 -04:00
|
|
|
for k, _ in pairs(section_keys) do
|
|
|
|
keys_to_disable[mode][k] = true
|
|
|
|
end
|
2022-07-22 12:00:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-08-11 06:10:26 -04:00
|
|
|
-- 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
|
2022-05-14 08:56:55 -04:00
|
|
|
end
|
2022-07-22 12:00:00 -04:00
|
|
|
end
|
|
|
|
end
|
2022-05-13 19:31:13 -04:00
|
|
|
|
2022-08-11 06:10:26 -04:00
|
|
|
return default_mappings
|
|
|
|
end
|
2022-07-22 12:00:00 -04:00
|
|
|
|
2022-08-11 06:10:26 -04:00
|
|
|
M.load_mappings = function(section, mapping_opt)
|
2023-04-09 20:34:37 -04:00
|
|
|
vim.schedule(function()
|
|
|
|
local function set_section_map(section_values)
|
|
|
|
if section_values.plugin then
|
|
|
|
return
|
|
|
|
end
|
2023-01-29 13:00:13 -05:00
|
|
|
|
2023-04-09 20:34:37 -04:00
|
|
|
section_values.plugin = nil
|
2022-07-22 12:00:00 -04:00
|
|
|
|
2023-04-09 20:34:37 -04:00
|
|
|
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 {})
|
2022-08-11 06:10:26 -04:00
|
|
|
|
2023-04-09 20:34:37 -04:00
|
|
|
mapping_info.opts, opts.mode = nil, nil
|
|
|
|
opts.desc = mapping_info[2]
|
2022-08-11 06:10:26 -04:00
|
|
|
|
2023-04-09 20:34:37 -04:00
|
|
|
vim.keymap.set(mode, keybind, mapping_info[1], opts)
|
|
|
|
end
|
2022-05-12 08:56:01 -04:00
|
|
|
end
|
2022-07-22 12:00:00 -04:00
|
|
|
end
|
2022-08-04 10:32:16 -04:00
|
|
|
|
2023-10-15 21:54:14 -04:00
|
|
|
local mappings = require("nvconfig").mappings
|
2022-08-11 06:10:26 -04:00
|
|
|
|
2023-04-09 20:34:37 -04:00
|
|
|
if type(section) == "string" then
|
|
|
|
mappings[section]["plugin"] = nil
|
|
|
|
mappings = { mappings[section] }
|
|
|
|
end
|
2022-08-11 06:10:26 -04:00
|
|
|
|
2023-04-09 20:34:37 -04:00
|
|
|
for _, sect in pairs(mappings) do
|
|
|
|
set_section_map(sect)
|
|
|
|
end
|
|
|
|
end)
|
2022-05-12 08:56:01 -04:00
|
|
|
end
|
|
|
|
|
2022-05-29 06:37:47 -04:00
|
|
|
return M
|