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 {}
|
end
|
||||||
prune_keys[mode] = vim.list_extend(prune_keys[mode], vim.tbl_keys(mappings))
|
|
||||||
|
local function disable_key(mode, keybind, mode_mapping)
|
||||||
|
local keys_in_mode = vim.tbl_keys(user_keys[mode] or {})
|
||||||
|
|
||||||
|
if vim.tbl_contains(keys_in_mode, keybind) then
|
||||||
|
mode_mapping[keybind] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local default_mappings = require("core.default_config").mappings
|
||||||
|
|
||||||
|
-- remove user_maps from default mapping table
|
||||||
|
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
|
||||||
return prune_keys
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- remove disabled mappings from a given key map
|
nvchad.load_mappings = function(mappings, mapping_opt)
|
||||||
nvchad.remove_disabled_mappings = function(key_map)
|
|
||||||
local clean_map = {}
|
|
||||||
|
|
||||||
if key_map == nil or key_map == "" then
|
|
||||||
return key_map
|
|
||||||
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
|
|
||||||
|
|
||||||
return clean_map
|
|
||||||
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 = 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
|
|
||||||
|
|
||||||
return true
|
|
||||||
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
|
||||||
end
|
else
|
||||||
end
|
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
|
||||||
|
|
||||||
|
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,25 +34,15 @@ 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 mappings = nvchad.load_config().mappings
|
||||||
local present, wk = pcall(require, "which-key")
|
local mapping_groups = { groups = vim.deepcopy(mappings.groups) }
|
||||||
|
|
||||||
if not present then
|
mappings.disabled = nil
|
||||||
return
|
mappings.groups = nil
|
||||||
end
|
|
||||||
|
|
||||||
local mappings = nvchad.load_config().mappings
|
nvchad.load_mappings()
|
||||||
local mapping_groups = { groups = vim.deepcopy(mappings.groups) }
|
nvchad.load_mappings(mapping_groups)
|
||||||
|
|
||||||
mappings.disabled = nil
|
wk.setup(options)
|
||||||
mappings.groups = nil
|
|
||||||
|
|
||||||
nvchad.whichKey_map(mappings, M.options)
|
|
||||||
nvchad.whichKey_map(mapping_groups, M.options)
|
|
||||||
|
|
||||||
wk.setup(M.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