Breaking change for non-whichkey users | Refactor mapping functions
This commit is contained in:
parent
c5bcf9d9a1
commit
0f013d4e7b
|
@ -102,9 +102,6 @@ M.comment = {
|
||||||
M.lspconfig = {
|
M.lspconfig = {
|
||||||
-- See `<cmd> :help vim.lsp.*` for documentation on any of the below functions
|
-- See `<cmd> :help vim.lsp.*` for documentation on any of the below functions
|
||||||
|
|
||||||
-- define all paths from which these maps should not be applied through the mapping function
|
|
||||||
ignore = { "/lua/custom/init.lua", "/lua/plugins/configs/whichkey.lua" },
|
|
||||||
|
|
||||||
n = {
|
n = {
|
||||||
["gD"] = {
|
["gD"] = {
|
||||||
function()
|
function()
|
||||||
|
|
|
@ -13,8 +13,6 @@ M.bootstrap = function()
|
||||||
|
|
||||||
fn.system { "git", "clone", "--depth", "1", "https://github.com/wbthomason/packer.nvim", install_path }
|
fn.system { "git", "clone", "--depth", "1", "https://github.com/wbthomason/packer.nvim", install_path }
|
||||||
|
|
||||||
print "Packer cloned successfully!"
|
|
||||||
|
|
||||||
-- install plugins + compile their configs
|
-- install plugins + compile their configs
|
||||||
vim.cmd "packadd packer.nvim"
|
vim.cmd "packadd packer.nvim"
|
||||||
require "plugins"
|
require "plugins"
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
_G.nvchad = {}
|
_G.nvchad = {}
|
||||||
|
|
||||||
|
local merge_tb = vim.tbl_deep_extend
|
||||||
|
|
||||||
nvchad.close_buffer = function(force)
|
nvchad.close_buffer = function(force)
|
||||||
if vim.bo.buftype == "terminal" then
|
if vim.bo.buftype == "terminal" then
|
||||||
vim.api.nvim_win_hide(0)
|
vim.api.nvim_win_hide(0)
|
||||||
|
@ -23,150 +25,89 @@ nvchad.close_buffer = function(force)
|
||||||
end
|
end
|
||||||
|
|
||||||
nvchad.load_config = function()
|
nvchad.load_config = function()
|
||||||
local conf = require "core.default_config"
|
local config = require "core.default_config"
|
||||||
local ignore_modes = { "mode_opts" }
|
local chadrc_exists, chadrc = pcall(require, "custom.chadrc")
|
||||||
|
|
||||||
-- attempt to load and merge a user config
|
|
||||||
local chadrc_exists = vim.fn.filereadable(vim.fn.stdpath "config" .. "/lua/custom/chadrc.lua") == 1
|
|
||||||
|
|
||||||
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
|
||||||
local user_config = require "custom.chadrc"
|
if type(chadrc) == "table" then
|
||||||
|
nvchad.remove_default_keys()
|
||||||
if type(user_config) == "table" then
|
config = merge_tb("force", config, chadrc)
|
||||||
conf.mappings = conf.mappings and nvchad.prune_key_map(conf.mappings, user_config.mappings, ignore_modes) or {}
|
|
||||||
user_config.mappings = user_config.mappings
|
|
||||||
and nvchad.prune_key_map(user_config.mappings, "rm_disabled", ignore_modes)
|
|
||||||
or {}
|
|
||||||
conf = vim.tbl_deep_extend("force", conf, user_config)
|
|
||||||
else
|
else
|
||||||
error "User config (chadrc.lua) *must* return a table!"
|
error "chadrc must return a table!"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return conf
|
config.mappings.disabled = nil
|
||||||
|
return config
|
||||||
end
|
end
|
||||||
|
|
||||||
-- reduces a given keymap to a table of modes each containing a list of key maps
|
nvchad.remove_default_keys = function()
|
||||||
nvchad.reduce_key_map = function(key_map, ignore_modes)
|
local chadrc = require "custom.chadrc"
|
||||||
local prune_keys = {}
|
local user_mappings = chadrc.mappings or {}
|
||||||
|
local user_keys = {}
|
||||||
|
local user_sections = vim.tbl_keys(user_mappings)
|
||||||
|
|
||||||
for _, modes in pairs(key_map) do
|
-- push user_map keys in user_keys table
|
||||||
for mode, mappings in pairs(modes) do
|
for _, section in ipairs(user_sections) do
|
||||||
if not vim.tbl_contains(ignore_modes, mode) then
|
user_keys = vim.tbl_deep_extend("force", user_keys, user_mappings[section])
|
||||||
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
|
end
|
||||||
|
|
||||||
-- remove disabled mappings from a given key map
|
local function disable_key(mode, keybind, mode_mapping)
|
||||||
nvchad.remove_disabled_mappings = function(key_map)
|
local keys_in_mode = vim.tbl_keys(user_keys[mode] or {})
|
||||||
local clean_map = {}
|
|
||||||
|
|
||||||
if key_map == nil or key_map == "" then
|
if vim.tbl_contains(keys_in_mode, keybind) then
|
||||||
return key_map
|
mode_mapping[keybind] = nil
|
||||||
end
|
|
||||||
|
|
||||||
if type(key_map) == "table" then
|
|
||||||
for k, v in pairs(key_map) do
|
|
||||||
if v ~= nil and v ~= "" then
|
|
||||||
clean_map[k] = v
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return clean_map
|
local default_mappings = require("core.default_config").mappings
|
||||||
end
|
|
||||||
|
|
||||||
-- prune keys from a key map table by matching against another key map table
|
-- remove user_maps from default mapping table
|
||||||
nvchad.prune_key_map = function(key_map, prune_map, ignore_modes)
|
for _, section_mappings in pairs(default_mappings) do
|
||||||
if not prune_map then
|
for mode, mode_mapping in pairs(section_mappings) do
|
||||||
return key_map
|
for keybind, _ in pairs(mode_mapping) do
|
||||||
end
|
disable_key(mode, keybind, mode_mapping)
|
||||||
if not key_map then
|
|
||||||
return prune_map
|
|
||||||
end
|
|
||||||
local prune_keys = type(prune_map) == "table" and nvchad.reduce_key_map(prune_map, ignore_modes)
|
|
||||||
or { n = {}, v = {}, i = {}, t = {} }
|
|
||||||
|
|
||||||
for ext, modes in pairs(key_map) do
|
|
||||||
for mode, mappings in pairs(modes) do
|
|
||||||
if not vim.tbl_contains(ignore_modes, mode) then
|
|
||||||
-- filter mappings table so that only keys that are not in user_mappings are left
|
|
||||||
for b, _ in pairs(mappings) do
|
|
||||||
if prune_keys[mode] and vim.tbl_contains(prune_keys[mode], b) then
|
|
||||||
key_map[ext][mode][b] = nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
key_map[ext][mode] = nvchad.remove_disabled_mappings(mappings)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return key_map
|
|
||||||
end
|
|
||||||
|
|
||||||
nvchad.map = function(mode, keys, command, opt)
|
|
||||||
local options = { silent = true }
|
|
||||||
|
|
||||||
if opt then
|
|
||||||
options = vim.tbl_extend("force", options, opt)
|
|
||||||
end
|
|
||||||
|
|
||||||
if type(keys) == "table" then
|
|
||||||
for _, keymap in ipairs(keys) do
|
|
||||||
nvchad.map(mode, keymap, command, opt)
|
|
||||||
end
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
vim.keymap.set(mode, keys, command, opt)
|
|
||||||
end
|
|
||||||
|
|
||||||
-- register mappings through which-key
|
|
||||||
nvchad.whichKey_map = function(maps, opts)
|
|
||||||
local present, wk = pcall(require, "which-key")
|
|
||||||
local caller_path = nvchad.get_caller_file_path(4)
|
|
||||||
|
|
||||||
if not present then
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
for mode, opt in pairs(opts.mode_opts) do
|
|
||||||
for _, value in pairs(maps) do
|
|
||||||
if value[mode] then
|
|
||||||
-- check if caller_path is in the ignore list
|
|
||||||
if not value["ignore"] or not vim.tbl_contains(value["ignore"], caller_path) then
|
|
||||||
local mode_opts = value["mode_opts"] and vim.tbl_deep_extend("force", opt, value["mode_opts"]) or opt
|
|
||||||
wk.register(value[mode], mode_opts)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return true
|
nvchad.load_mappings = function(mappings, mapping_opt)
|
||||||
end
|
|
||||||
|
|
||||||
-- for those who disabled whichkey and want to add specific mapping tables
|
|
||||||
nvchad.no_WhichKey_map = function(mappings)
|
|
||||||
local caller_path = nvchad.get_caller_file_path(4)
|
|
||||||
local ignore_modes = { "mode_opts" }
|
|
||||||
mappings = mappings or nvchad.load_config().mappings
|
mappings = mappings or nvchad.load_config().mappings
|
||||||
|
|
||||||
for _, value in pairs(mappings) do
|
-- set mapping function with/without whichkye
|
||||||
if not value["ignore"] or not vim.tbl_contains(value["ignore"], caller_path) then
|
local map_func
|
||||||
for mode, keymap in pairs(value) do
|
local whichkey_exists, wk = pcall(require, "which-key")
|
||||||
if not vim.tbl_contains(ignore_modes, mode) then
|
|
||||||
for keybind, cmd in pairs(keymap) do
|
if whichkey_exists then
|
||||||
-- disabled keys will not have cmd set
|
map_func = function(keybind, mapping_info, opts)
|
||||||
if cmd ~= "" and cmd[1] then
|
wk.register({ [keybind] = mapping_info }, opts)
|
||||||
nvchad.map(mode, keybind, cmd[1], value["mode_opts"] or nil)
|
end
|
||||||
|
else
|
||||||
|
map_func = function(keybind, mapping_info, opts)
|
||||||
|
local mode = opts.mode
|
||||||
|
opts.mode = nil
|
||||||
|
vim.keymap.set(mode, keybind, mapping_info[1], opts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
mappings.lspconfig = nil
|
||||||
|
|
||||||
|
for _, section_mappings in pairs(mappings) do
|
||||||
|
-- skip mapping this as its mapppings are loaded in lspconfiguti
|
||||||
|
for mode, mode_mappings in pairs(section_mappings) do
|
||||||
|
for keybind, mapping_info in pairs(mode_mappings) 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
|
||||||
|
|
||||||
|
map_func(keybind, mapping_info, opts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -200,7 +141,7 @@ nvchad.merge_plugins = function(default_plugins)
|
||||||
local user_plugins = nvchad.load_config().plugins.user
|
local user_plugins = nvchad.load_config().plugins.user
|
||||||
|
|
||||||
-- merge default + user plugin table
|
-- merge default + user plugin table
|
||||||
default_plugins = vim.tbl_deep_extend("force", default_plugins, user_plugins)
|
default_plugins = merge_tb("force", default_plugins, user_plugins)
|
||||||
|
|
||||||
local final_table = {}
|
local final_table = {}
|
||||||
|
|
||||||
|
@ -217,20 +158,10 @@ nvchad.load_override = function(default_table, plugin_name)
|
||||||
local user_table = nvchad.load_config().plugins.override[plugin_name]
|
local user_table = nvchad.load_config().plugins.override[plugin_name]
|
||||||
|
|
||||||
if type(user_table) == "table" then
|
if type(user_table) == "table" then
|
||||||
default_table = vim.tbl_deep_extend("force", default_table, user_table)
|
default_table = merge_tb("force", default_table, user_table)
|
||||||
else
|
else
|
||||||
default_table = default_table
|
default_table = default_table
|
||||||
end
|
end
|
||||||
|
|
||||||
return default_table
|
return default_table
|
||||||
end
|
end
|
||||||
|
|
||||||
nvchad.get_caller_file_path = function(call_stack_depth)
|
|
||||||
local success, result = pcall(debug.getinfo, call_stack_depth, "S")
|
|
||||||
|
|
||||||
if success then
|
|
||||||
return result.source:match("@(.*)"):gsub(vim.fn.stdpath "config", "")
|
|
||||||
else
|
|
||||||
return ""
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
|
@ -22,14 +22,8 @@ M.on_attach = function(client, bufnr)
|
||||||
client.resolved_capabilities.document_formatting = false
|
client.resolved_capabilities.document_formatting = false
|
||||||
client.resolved_capabilities.document_range_formatting = false
|
client.resolved_capabilities.document_range_formatting = false
|
||||||
|
|
||||||
local options = require("plugins.configs.whichkey").options
|
local lsp_mappings = nvchad.load_config().mappings.lspconfig
|
||||||
local lsp_mappings = { nvchad.load_config().mappings.lspconfig }
|
nvchad.load_mappings({ lsp_mappings }, { buffer = bufnr })
|
||||||
|
|
||||||
lsp_mappings[1]["mode_opts"] = { buffer = bufnr }
|
|
||||||
|
|
||||||
if not nvchad.whichKey_map(lsp_mappings, options) then
|
|
||||||
nvchad.no_WhichKey_map(lsp_mappings)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local capabilities = vim.lsp.protocol.make_client_capabilities()
|
local capabilities = vim.lsp.protocol.make_client_capabilities()
|
||||||
|
|
|
@ -1,27 +1,10 @@
|
||||||
local M = {}
|
local present, wk = pcall(require, "which-key")
|
||||||
|
|
||||||
M.options = {
|
if not present then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
-- NOTE : this mode_opts table isnt in the default whichkey config
|
local options = {
|
||||||
-- Its added here so you could configure it in chadrc
|
|
||||||
|
|
||||||
mode_opts = {
|
|
||||||
n = {
|
|
||||||
mode = "n",
|
|
||||||
},
|
|
||||||
|
|
||||||
v = {
|
|
||||||
mode = "v",
|
|
||||||
},
|
|
||||||
|
|
||||||
i = {
|
|
||||||
mode = "i",
|
|
||||||
},
|
|
||||||
|
|
||||||
t = {
|
|
||||||
mode = "t",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
icons = {
|
icons = {
|
||||||
breadcrumb = "»", -- symbol used in the command line area that shows your active key combo
|
breadcrumb = "»", -- symbol used in the command line area that shows your active key combo
|
||||||
|
@ -51,14 +34,7 @@ M.options = {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
M.options = nvchad.load_override(M.options, "folke/which-key.nvim")
|
options = nvchad.load_override(options, "folke/which-key.nvim")
|
||||||
|
|
||||||
M.setup = function()
|
|
||||||
local present, wk = pcall(require, "which-key")
|
|
||||||
|
|
||||||
if not present then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local mappings = nvchad.load_config().mappings
|
local mappings = nvchad.load_config().mappings
|
||||||
local mapping_groups = { groups = vim.deepcopy(mappings.groups) }
|
local mapping_groups = { groups = vim.deepcopy(mappings.groups) }
|
||||||
|
@ -66,10 +42,7 @@ M.setup = function()
|
||||||
mappings.disabled = nil
|
mappings.disabled = nil
|
||||||
mappings.groups = nil
|
mappings.groups = nil
|
||||||
|
|
||||||
nvchad.whichKey_map(mappings, M.options)
|
nvchad.load_mappings()
|
||||||
nvchad.whichKey_map(mapping_groups, M.options)
|
nvchad.load_mappings(mapping_groups)
|
||||||
|
|
||||||
wk.setup(M.options)
|
wk.setup(options)
|
||||||
end
|
|
||||||
|
|
||||||
return M
|
|
||||||
|
|
|
@ -206,7 +206,7 @@ local plugins = {
|
||||||
nvchad.packer_lazy_load "which-key.nvim"
|
nvchad.packer_lazy_load "which-key.nvim"
|
||||||
end,
|
end,
|
||||||
config = function()
|
config = function()
|
||||||
require("plugins.configs.whichkey").setup()
|
require "plugins.configs.whichkey"
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue