From f8b5571466173f6518ab22351bacda186fd23b2e Mon Sep 17 00:00:00 2001 From: Leon Heidelbach Date: Thu, 26 May 2022 17:49:13 +0200 Subject: [PATCH] fix: fixed lspconfig issues and cleaned up --- lua/core/mappings.lua | 3 ++- lua/core/utils.lua | 36 ++++++++++++++----------------- lua/plugins/configs/lspconfig.lua | 2 +- lua/plugins/configs/whichkey.lua | 12 +++++------ 4 files changed, 25 insertions(+), 28 deletions(-) diff --git a/lua/core/mappings.lua b/lua/core/mappings.lua index fd10425..f7c4f67 100644 --- a/lua/core/mappings.lua +++ b/lua/core/mappings.lua @@ -102,7 +102,8 @@ M.comment = { M.lspconfig = { -- See ` :help vim.lsp.*` for documentation on any of the below functions - ignore = { "/lua/plugins/configs/whichkey.lua" }, + -- 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"] = { diff --git a/lua/core/utils.lua b/lua/core/utils.lua index 4b97769..a712791 100644 --- a/lua/core/utils.lua +++ b/lua/core/utils.lua @@ -129,7 +129,7 @@ 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() + local caller_path = nvchad.get_caller_file_path(4) if not present then return false @@ -140,8 +140,7 @@ nvchad.whichKey_map = function(maps, opts) 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 + 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 @@ -152,16 +151,20 @@ nvchad.whichKey_map = function(maps, opts) end -- for those who disabled whichkey and want to add specific mapping tables -nvchad.no_WhichKey_table_map = function(mappings) +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 for _, value in pairs(mappings) do - 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]) + 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 end end @@ -169,13 +172,6 @@ nvchad.no_WhichKey_table_map = function(mappings) 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 @@ -229,11 +225,11 @@ 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") +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"), "") + return result.source:match("@(.*)"):gsub(vim.fn.stdpath "config", "") else return "" end diff --git a/lua/plugins/configs/lspconfig.lua b/lua/plugins/configs/lspconfig.lua index 8355385..46d3558 100644 --- a/lua/plugins/configs/lspconfig.lua +++ b/lua/plugins/configs/lspconfig.lua @@ -28,7 +28,7 @@ M.on_attach = function(client, bufnr) lsp_mappings[1]["mode_opts"] = { buffer = bufnr } if not nvchad.whichKey_map(lsp_mappings, options) then - nvchad.no_WhichKey_table_map(lsp_mappings) + nvchad.no_WhichKey_map(lsp_mappings) end end diff --git a/lua/plugins/configs/whichkey.lua b/lua/plugins/configs/whichkey.lua index e113b3b..1e23133 100644 --- a/lua/plugins/configs/whichkey.lua +++ b/lua/plugins/configs/whichkey.lua @@ -1,9 +1,3 @@ -local present, wk = pcall(require, "which-key") - -if not present then - return -end - local M = {} M.options = { @@ -60,6 +54,12 @@ M.options = { M.options = nvchad.load_override(M.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 mapping_groups = { groups = vim.deepcopy(mappings.groups) }