From 0f013d4e7b52da7978b386c281cd026413543417 Mon Sep 17 00:00:00 2001 From: siduck Date: Sun, 29 May 2022 15:08:53 +0530 Subject: [PATCH] Breaking change for non-whichkey users | Refactor mapping functions --- lua/core/mappings.lua | 3 - lua/core/packer.lua | 2 - lua/core/utils.lua | 207 ++++++++++-------------------- lua/plugins/configs/lspconfig.lua | 10 +- lua/plugins/configs/whichkey.lua | 53 ++------ lua/plugins/init.lua | 2 +- 6 files changed, 85 insertions(+), 192 deletions(-) diff --git a/lua/core/mappings.lua b/lua/core/mappings.lua index f7c4f67..8ba5d92 100644 --- a/lua/core/mappings.lua +++ b/lua/core/mappings.lua @@ -102,9 +102,6 @@ M.comment = { M.lspconfig = { -- See ` :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 = { ["gD"] = { function() diff --git a/lua/core/packer.lua b/lua/core/packer.lua index e48a9cd..126f9ab 100644 --- a/lua/core/packer.lua +++ b/lua/core/packer.lua @@ -13,8 +13,6 @@ M.bootstrap = function() fn.system { "git", "clone", "--depth", "1", "https://github.com/wbthomason/packer.nvim", install_path } - print "Packer cloned successfully!" - -- install plugins + compile their configs vim.cmd "packadd packer.nvim" require "plugins" diff --git a/lua/core/utils.lua b/lua/core/utils.lua index a712791..95beb4b 100644 --- a/lua/core/utils.lua +++ b/lua/core/utils.lua @@ -1,5 +1,7 @@ _G.nvchad = {} +local merge_tb = vim.tbl_deep_extend + nvchad.close_buffer = function(force) if vim.bo.buftype == "terminal" then vim.api.nvim_win_hide(0) @@ -23,150 +25,89 @@ nvchad.close_buffer = function(force) end nvchad.load_config = function() - local conf = require "core.default_config" - local ignore_modes = { "mode_opts" } - - -- attempt to load and merge a user config - local chadrc_exists = vim.fn.filereadable(vim.fn.stdpath "config" .. "/lua/custom/chadrc.lua") == 1 + local config = require "core.default_config" + local chadrc_exists, chadrc = pcall(require, "custom.chadrc") if chadrc_exists then -- merge user config if it exists and is a table; otherwise display an error - local user_config = require "custom.chadrc" - - if type(user_config) == "table" then - 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) + if type(chadrc) == "table" then + nvchad.remove_default_keys() + config = merge_tb("force", config, chadrc) else - error "User config (chadrc.lua) *must* return a table!" + error "chadrc must return a table!" end end - return conf + config.mappings.disabled = nil + return config end --- reduces a given keymap to a table of modes each containing a list of key maps -nvchad.reduce_key_map = function(key_map, ignore_modes) - local prune_keys = {} +nvchad.remove_default_keys = function() + local chadrc = require "custom.chadrc" + local user_mappings = chadrc.mappings or {} + local user_keys = {} + local user_sections = vim.tbl_keys(user_mappings) - 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)) + -- 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 + + 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 - return prune_keys end --- remove disabled mappings from a given key map -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" } +nvchad.load_mappings = function(mappings, mapping_opt) mappings = mappings or nvchad.load_config().mappings - for _, value in pairs(mappings) do - if not value["ignore"] or not vim.tbl_contains(value["ignore"], caller_path) then - for mode, keymap in pairs(value) do - if not vim.tbl_contains(ignore_modes, mode) then - for keybind, cmd in pairs(keymap) do - -- disabled keys will not have cmd set - if cmd ~= "" and cmd[1] then - nvchad.map(mode, keybind, cmd[1], value["mode_opts"] or nil) - end - end + -- set mapping function with/without whichkye + local map_func + local whichkey_exists, wk = pcall(require, "which-key") + + if whichkey_exists then + map_func = function(keybind, mapping_info, opts) + wk.register({ [keybind] = mapping_info }, opts) + 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 + + 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 + + map_func(keybind, mapping_info, opts) end end end @@ -200,7 +141,7 @@ nvchad.merge_plugins = function(default_plugins) local user_plugins = nvchad.load_config().plugins.user -- 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 = {} @@ -217,20 +158,10 @@ nvchad.load_override = function(default_table, plugin_name) local user_table = nvchad.load_config().plugins.override[plugin_name] 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 default_table = default_table end return default_table 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 diff --git a/lua/plugins/configs/lspconfig.lua b/lua/plugins/configs/lspconfig.lua index 46d3558..f123ac5 100644 --- a/lua/plugins/configs/lspconfig.lua +++ b/lua/plugins/configs/lspconfig.lua @@ -22,14 +22,8 @@ M.on_attach = function(client, bufnr) client.resolved_capabilities.document_formatting = false client.resolved_capabilities.document_range_formatting = false - local options = require("plugins.configs.whichkey").options - local lsp_mappings = { nvchad.load_config().mappings.lspconfig } - - lsp_mappings[1]["mode_opts"] = { buffer = bufnr } - - if not nvchad.whichKey_map(lsp_mappings, options) then - nvchad.no_WhichKey_map(lsp_mappings) - end + local lsp_mappings = nvchad.load_config().mappings.lspconfig + nvchad.load_mappings({ lsp_mappings }, { buffer = bufnr }) end local capabilities = vim.lsp.protocol.make_client_capabilities() diff --git a/lua/plugins/configs/whichkey.lua b/lua/plugins/configs/whichkey.lua index 1e23133..c2487bb 100644 --- a/lua/plugins/configs/whichkey.lua +++ b/lua/plugins/configs/whichkey.lua @@ -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 - -- Its added here so you could configure it in chadrc - - mode_opts = { - n = { - mode = "n", - }, - - v = { - mode = "v", - }, - - i = { - mode = "i", - }, - - t = { - mode = "t", - }, - }, +local options = { icons = { 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 present, wk = pcall(require, "which-key") +local mappings = nvchad.load_config().mappings +local mapping_groups = { groups = vim.deepcopy(mappings.groups) } - if not present then - return - end +mappings.disabled = nil +mappings.groups = nil - local mappings = nvchad.load_config().mappings - local mapping_groups = { groups = vim.deepcopy(mappings.groups) } +nvchad.load_mappings() +nvchad.load_mappings(mapping_groups) - mappings.disabled = nil - mappings.groups = nil - - nvchad.whichKey_map(mappings, M.options) - nvchad.whichKey_map(mapping_groups, M.options) - - wk.setup(M.options) -end - -return M +wk.setup(options) diff --git a/lua/plugins/init.lua b/lua/plugins/init.lua index f0c1867..0c3d16d 100644 --- a/lua/plugins/init.lua +++ b/lua/plugins/init.lua @@ -206,7 +206,7 @@ local plugins = { nvchad.packer_lazy_load "which-key.nvim" end, config = function() - require("plugins.configs.whichkey").setup() + require "plugins.configs.whichkey" end, }, }