diff --git a/lua/core/mappings.lua b/lua/core/mappings.lua index 8ba5d92..fd10425 100644 --- a/lua/core/mappings.lua +++ b/lua/core/mappings.lua @@ -102,6 +102,8 @@ M.comment = { M.lspconfig = { -- See ` :help vim.lsp.*` for documentation on any of the below functions + ignore = { "/lua/plugins/configs/whichkey.lua" }, + n = { ["gD"] = { function() diff --git a/lua/core/utils.lua b/lua/core/utils.lua index d9662e8..4b97769 100644 --- a/lua/core/utils.lua +++ b/lua/core/utils.lua @@ -36,8 +36,8 @@ nvchad.load_config = function() 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 {} + and nvchad.prune_key_map(user_config.mappings, "rm_disabled", ignore_modes) + or {} conf = vim.tbl_deep_extend("force", conf, user_config) else error "User config (chadrc.lua) *must* return a table!" @@ -90,7 +90,7 @@ nvchad.prune_key_map = function(key_map, prune_map, ignore_modes) 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 = {} } + or { n = {}, v = {}, i = {}, t = {} } for ext, modes in pairs(key_map) do for mode, mappings in pairs(modes) do @@ -126,9 +126,33 @@ nvchad.map = function(mode, keys, command, opt) vim.keymap.set(mode, keys, command, opt) end --- For those who disabled whichkey -nvchad.no_WhichKey_map = function() - local mappings = nvchad.load_config().mappings +-- 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() + + 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_table_map = function(mappings) local ignore_modes = { "mode_opts" } for _, value in pairs(mappings) do @@ -145,6 +169,13 @@ nvchad.no_WhichKey_map = function() end end +-- for those who disabled whichkey +nvchad.no_WhichKey_map = function() + local mappings = nvchad.load_config().mappings + + nvchad.no_WhichKey_table_map(mappings) +end + -- load plugin after entering vim ui nvchad.packer_lazy_load = function(plugin, timer) if plugin then @@ -197,3 +228,13 @@ nvchad.load_override = function(default_table, plugin_name) return default_table end + +nvchad.get_caller_file_path = function() + local success, result = pcall(debug.getinfo, 4, "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 f660a81..e4593ee 100644 --- a/lua/plugins/configs/lspconfig.lua +++ b/lua/plugins/configs/lspconfig.lua @@ -22,14 +22,64 @@ M.on_attach = function(client, bufnr) client.resolved_capabilities.document_formatting = false client.resolved_capabilities.document_range_formatting = false - local lsp_mappings = nvchad.load_config().mappings.lspconfig - local wk_exists, wk = pcall(require, "which-key") + local options = { - if wk_exists then - wk.register(lsp_mappings.n, { buffer = bufnr }) - else - -- todo, make use of bufnr here - -- add no whichkey func logic here + -- 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", + }, + }, + + icons = { + breadcrumb = "»", -- symbol used in the command line area that shows your active key combo + separator = "  ", -- symbol used between a key and it's label + group = "+", -- symbol prepended to a group + }, + + popup_mappings = { + scroll_down = "", -- binding to scroll down inside the popup + scroll_up = "", -- binding to scroll up inside the popup + }, + + window = { + border = "none", -- none/single/double/shadow + }, + + layout = { + spacing = 6, -- spacing between columns + }, + + hidden = { "", "", "", "", "call", "lua", "^:", "^ " }, + + triggers_blacklist = { + -- list of mode / prefixes that should never be hooked by WhichKey + i = { "j", "k" }, + v = { "j", "k" }, + }, + } + + options = nvchad.load_override(options, "folke/which-key.nvim") + + 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_table_map(lsp_mappings) end end diff --git a/lua/plugins/configs/whichkey.lua b/lua/plugins/configs/whichkey.lua index 70b1a11..afcf40c 100644 --- a/lua/plugins/configs/whichkey.lua +++ b/lua/plugins/configs/whichkey.lua @@ -63,21 +63,7 @@ local mapping_groups = { groups = vim.deepcopy(mappings.groups) } mappings.disabled = nil mappings.groups = nil --- register mappings -local function register_mappings(maps, opts) - for mode, opt in pairs(opts.mode_opts) do - for key, value in pairs(maps) do - if key ~= "lspconfig" then - if value[mode] 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 - -register_mappings(mappings, options) -register_mappings(mapping_groups, options) +nvchad.whichKey_map(mappings, options) +nvchad.whichKey_map(mapping_groups, options) wk.setup(options)