From cb97cef2a7d6ba48cdeb0d6c959cdd458ad240b8 Mon Sep 17 00:00:00 2001 From: siduck Date: Sun, 29 May 2022 16:07:47 +0530 Subject: [PATCH] remove usage of globals for util functions --- lua/core/mappings.lua | 2 +- lua/core/options.lua | 4 ++-- lua/core/packer.lua | 6 +++--- lua/core/utils.lua | 30 ++++++++++++++------------- lua/plugins/configs/alpha.lua | 2 +- lua/plugins/configs/bufferline.lua | 2 +- lua/plugins/configs/cmp.lua | 8 +++---- lua/plugins/configs/icons.lua | 2 +- lua/plugins/configs/lsp_installer.lua | 2 +- lua/plugins/configs/lspconfig.lua | 6 +++--- lua/plugins/configs/nvimtree.lua | 2 +- lua/plugins/configs/nvterm.lua | 2 +- lua/plugins/configs/others.lua | 10 +++++---- lua/plugins/configs/statusline.lua | 4 ++-- lua/plugins/configs/telescope.lua | 2 +- lua/plugins/configs/treesitter.lua | 2 +- lua/plugins/configs/whichkey.lua | 10 +++++---- lua/plugins/init.lua | 14 ++++--------- 18 files changed, 55 insertions(+), 55 deletions(-) diff --git a/lua/core/mappings.lua b/lua/core/mappings.lua index 8ba5d92..b7c10ed 100644 --- a/lua/core/mappings.lua +++ b/lua/core/mappings.lua @@ -71,7 +71,7 @@ M.bufferline = { -- close buffer + hide terminal buffer ["x"] = { function() - nvchad.close_buffer() + require("core.utils").close_buffer() end, " close buffer", }, diff --git a/lua/core/options.lua b/lua/core/options.lua index 26727c7..05e3c76 100644 --- a/lua/core/options.lua +++ b/lua/core/options.lua @@ -1,7 +1,7 @@ local opt = vim.opt local g = vim.g -g.nvchad_theme = nvchad.load_config().ui.theme +g.nvchad_theme = require("core.utils").load_config().ui.theme -- use filetype.lua instead of filetype.vim g.did_load_filetypes = 0 @@ -86,4 +86,4 @@ vim.schedule(function() end) -- load user options if the file exists -nvchad.load_config().options.user() +require("core.utils").load_config().options.user() diff --git a/lua/core/packer.lua b/lua/core/packer.lua index 126f9ab..ceaecf5 100644 --- a/lua/core/packer.lua +++ b/lua/core/packer.lua @@ -37,7 +37,7 @@ M.options = { } -- merge overrides if there are any -M.options = nvchad.load_override(M.options, "wbthomason/packer.nvim") +M.options = require("core.utils").load_override(M.options, "wbthomason/packer.nvim") M.run = function(plugins) local present, packer = pcall(require, "packer") @@ -47,8 +47,8 @@ M.run = function(plugins) end -- Override with chadrc values - plugins = nvchad.remove_default_plugins(plugins) - plugins = nvchad.merge_plugins(plugins) + plugins = require("core.utils").remove_default_plugins(plugins) + plugins = require("core.utils").merge_plugins(plugins) packer.init(M.options) diff --git a/lua/core/utils.lua b/lua/core/utils.lua index 95beb4b..ba94f86 100644 --- a/lua/core/utils.lua +++ b/lua/core/utils.lua @@ -1,8 +1,8 @@ -_G.nvchad = {} +local M = {} local merge_tb = vim.tbl_deep_extend -nvchad.close_buffer = function(force) +M.close_buffer = function(force) if vim.bo.buftype == "terminal" then vim.api.nvim_win_hide(0) return @@ -24,14 +24,14 @@ nvchad.close_buffer = function(force) vim.cmd(close_cmd) end -nvchad.load_config = function() +M.load_config = function() 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 if type(chadrc) == "table" then - nvchad.remove_default_keys() + M.remove_default_keys() config = merge_tb("force", config, chadrc) else error "chadrc must return a table!" @@ -42,7 +42,7 @@ nvchad.load_config = function() return config end -nvchad.remove_default_keys = function() +M.remove_default_keys = function() local chadrc = require "custom.chadrc" local user_mappings = chadrc.mappings or {} local user_keys = {} @@ -73,8 +73,8 @@ nvchad.remove_default_keys = function() end end -nvchad.load_mappings = function(mappings, mapping_opt) - mappings = mappings or nvchad.load_config().mappings +M.load_mappings = function(mappings, mapping_opt) + mappings = mappings or M.load_config().mappings -- set mapping function with/without whichkye local map_func @@ -114,7 +114,7 @@ nvchad.load_mappings = function(mappings, mapping_opt) end -- load plugin after entering vim ui -nvchad.packer_lazy_load = function(plugin, timer) +M.packer_lazy_load = function(plugin, timer) if plugin then timer = timer or 0 vim.defer_fn(function() @@ -124,8 +124,8 @@ nvchad.packer_lazy_load = function(plugin, timer) end -- remove plugins defined in chadrc -nvchad.remove_default_plugins = function(plugins) - local removals = nvchad.load_config().plugins.remove or {} +M.remove_default_plugins = function(plugins) + local removals = M.load_config().plugins.remove or {} if not vim.tbl_isempty(removals) then for _, plugin in pairs(removals) do @@ -137,8 +137,8 @@ nvchad.remove_default_plugins = function(plugins) end -- merge default/user plugin tables -nvchad.merge_plugins = function(default_plugins) - local user_plugins = nvchad.load_config().plugins.user +M.merge_plugins = function(default_plugins) + local user_plugins = M.load_config().plugins.user -- merge default + user plugin table default_plugins = merge_tb("force", default_plugins, user_plugins) @@ -154,8 +154,8 @@ nvchad.merge_plugins = function(default_plugins) return final_table end -nvchad.load_override = function(default_table, plugin_name) - local user_table = nvchad.load_config().plugins.override[plugin_name] +M.load_override = function(default_table, plugin_name) + local user_table = M.load_config().plugins.override[plugin_name] if type(user_table) == "table" then default_table = merge_tb("force", default_table, user_table) @@ -165,3 +165,5 @@ nvchad.load_override = function(default_table, plugin_name) return default_table end + +return M diff --git a/lua/plugins/configs/alpha.lua b/lua/plugins/configs/alpha.lua index 840cafd..6a57bd6 100644 --- a/lua/plugins/configs/alpha.lua +++ b/lua/plugins/configs/alpha.lua @@ -72,7 +72,7 @@ options.buttons = { }, } -options = nvchad.load_override(options, "goolord/alpha-nvim") +options = require("core.utils").load_override(options, "goolord/alpha-nvim") -- dynamic header padding local fn = vim.fn diff --git a/lua/plugins/configs/bufferline.lua b/lua/plugins/configs/bufferline.lua index b6f62da..f554a54 100644 --- a/lua/plugins/configs/bufferline.lua +++ b/lua/plugins/configs/bufferline.lua @@ -65,6 +65,6 @@ local options = { } -- check for any override -options = nvchad.load_override(options, "akinsho/bufferline.nvim") +options = require("core.utils").load_override(options, "akinsho/bufferline.nvim") bufferline.setup(options) diff --git a/lua/plugins/configs/cmp.lua b/lua/plugins/configs/cmp.lua index c587eb1..36baf27 100644 --- a/lua/plugins/configs/cmp.lua +++ b/lua/plugins/configs/cmp.lua @@ -23,9 +23,9 @@ local cmp_window = require "cmp.utils.window" cmp_window.info_ = cmp_window.info cmp_window.info = function(self) - local info = self:info_() - info.scrollable = false - return info + local info = self:info_() + info.scrollable = false + return info end local options = { @@ -96,6 +96,6 @@ local options = { } -- check for any override -options = nvchad.load_override(options, "hrsh7th/nvim-cmp") +options = require("core.utils").load_override(options, "hrsh7th/nvim-cmp") cmp.setup(options) diff --git a/lua/plugins/configs/icons.lua b/lua/plugins/configs/icons.lua index 7a92dd3..c3f3ba0 100644 --- a/lua/plugins/configs/icons.lua +++ b/lua/plugins/configs/icons.lua @@ -118,6 +118,6 @@ local options = { } -- check for any override -options = nvchad.load_override(options, "kyazdani42/nvim-web-devicons") +options = require("core.utils").load_override(options, "kyazdani42/nvim-web-devicons") devicons.setup(options) diff --git a/lua/plugins/configs/lsp_installer.lua b/lua/plugins/configs/lsp_installer.lua index 000f536..2e0cef9 100644 --- a/lua/plugins/configs/lsp_installer.lua +++ b/lua/plugins/configs/lsp_installer.lua @@ -31,6 +31,6 @@ local options = { max_concurrent_installers = 20, } -options = nvchad.load_override(options, "williamboman/nvim-lsp-installer") +options = require("core.utils").load_override(options, "williamboman/nvim-lsp-installer") lsp_installer.setup(options) diff --git a/lua/plugins/configs/lspconfig.lua b/lua/plugins/configs/lspconfig.lua index f123ac5..c8f8ab5 100644 --- a/lua/plugins/configs/lspconfig.lua +++ b/lua/plugins/configs/lspconfig.lua @@ -22,8 +22,8 @@ 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 - nvchad.load_mappings({ lsp_mappings }, { buffer = bufnr }) + local lsp_mappings = require("core.utils").load_config().mappings.lspconfig + require("core.utils").load_mappings({ lsp_mappings }, { buffer = bufnr }) end local capabilities = vim.lsp.protocol.make_client_capabilities() @@ -68,7 +68,7 @@ lspconfig.sumneko_lua.setup { } -- requires a file containing user's lspconfigs -local addlsp_confs = nvchad.load_config().plugins.options.lspconfig.setup_lspconf +local addlsp_confs = require("core.utils").load_config().plugins.options.lspconfig.setup_lspconf if #addlsp_confs ~= 0 then require(addlsp_confs).setup_lsp(M.on_attach, capabilities) diff --git a/lua/plugins/configs/nvimtree.lua b/lua/plugins/configs/nvimtree.lua index c1bda95..e2866b1 100644 --- a/lua/plugins/configs/nvimtree.lua +++ b/lua/plugins/configs/nvimtree.lua @@ -78,6 +78,6 @@ local options = { } -- check for any override -options = nvchad.load_override(options, "kyazdani42/nvim-tree.lua") +options = require("core.utils").load_override(options, "kyazdani42/nvim-tree.lua") nvimtree.setup(options) diff --git a/lua/plugins/configs/nvterm.lua b/lua/plugins/configs/nvterm.lua index 5e13ec2..581e4c4 100644 --- a/lua/plugins/configs/nvterm.lua +++ b/lua/plugins/configs/nvterm.lua @@ -27,6 +27,6 @@ local options = { enable_new_mappings = true, } -options = nvchad.load_override(options, "NvChad/nvterm") +options = require("core.utils").load_override(options, "NvChad/nvterm") nvterm.setup(options) diff --git a/lua/plugins/configs/others.lua b/lua/plugins/configs/others.lua index 433560b..e44ee32 100644 --- a/lua/plugins/configs/others.lua +++ b/lua/plugins/configs/others.lua @@ -1,5 +1,7 @@ local M = {} +local load_override = require("core.utils").load_override + M.autopairs = function() local present1, autopairs = pcall(require, "nvim-autopairs") local present2, cmp = pcall(require, "cmp") @@ -32,7 +34,7 @@ M.better_escape = function() keys = "", } - options = nvchad.load_override(options, "max397574/better-escape.nvim") + options = load_override(options, "max397574/better-escape.nvim") escape.setup(options) end @@ -63,7 +65,7 @@ M.blankline = function() show_first_indent_level = false, } - options = nvchad.load_override(options, "lukas-reineke/indent-blankline.nvim") + options = load_override(options, "lukas-reineke/indent-blankline.nvim") blankline.setup(options) end @@ -93,7 +95,7 @@ M.colorizer = function() }, } - options = nvchad.load_override(options, "NvChad/nvim-colorizer.lua") + options = load_override(options, "NvChad/nvim-colorizer.lua") colorizer.setup(options["filetypes"], options["user_default_options"]) vim.cmd "ColorizerReloadAllBuffers" @@ -149,7 +151,7 @@ M.signature = function() padding = "", -- character to pad on left and right of signature can be ' ', or '|' etc } - options = nvchad.load_override(options, "ray-x/lsp_signature.nvim") + options = load_override(options, "ray-x/lsp_signature.nvim") lsp_signature.setup(options) end diff --git a/lua/plugins/configs/statusline.lua b/lua/plugins/configs/statusline.lua index 4c850a9..6595901 100644 --- a/lua/plugins/configs/statusline.lua +++ b/lua/plugins/configs/statusline.lua @@ -50,7 +50,7 @@ options.icon_styles = { }, } -options.separator_style = options.icon_styles[nvchad.load_config().plugins.options.statusline.separator_style] +options.separator_style = options.icon_styles[require("core.utils").load_config().plugins.options.statusline.separator_style] options.main_icon = { provider = options.separator_style.main_icon, @@ -312,7 +312,7 @@ options.current_line = { hl = "Feline_CurrentLine", } -options = nvchad.load_override(options, "feline-nvim/feline.nvim") +options = require("core.utils").load_override(options, "feline-nvim/feline.nvim") local function add_table(tbl, inject) if inject then diff --git a/lua/plugins/configs/telescope.lua b/lua/plugins/configs/telescope.lua index 4637492..15bd800 100644 --- a/lua/plugins/configs/telescope.lua +++ b/lua/plugins/configs/telescope.lua @@ -59,7 +59,7 @@ local options = { } -- check for any override -options = nvchad.load_override(options, "nvim-telescope/telescope.nvim") +options = require("core.utils").load_override(options, "nvim-telescope/telescope.nvim") telescope.setup(options) -- load extensions diff --git a/lua/plugins/configs/treesitter.lua b/lua/plugins/configs/treesitter.lua index b576b3f..a6e6483 100644 --- a/lua/plugins/configs/treesitter.lua +++ b/lua/plugins/configs/treesitter.lua @@ -16,6 +16,6 @@ local options = { } -- check for any override -options = nvchad.load_override(options, "nvim-treesitter/nvim-treesitter") +options = require("core.utils").load_override(options, "nvim-treesitter/nvim-treesitter") treesitter.setup(options) diff --git a/lua/plugins/configs/whichkey.lua b/lua/plugins/configs/whichkey.lua index c2487bb..023327f 100644 --- a/lua/plugins/configs/whichkey.lua +++ b/lua/plugins/configs/whichkey.lua @@ -34,15 +34,17 @@ local options = { }, } -options = nvchad.load_override(options, "folke/which-key.nvim") +options = require("core.utils").load_override(options, "folke/which-key.nvim") -local mappings = nvchad.load_config().mappings +local utils = require "core.utils" + +local mappings = utils.load_config().mappings local mapping_groups = { groups = vim.deepcopy(mappings.groups) } mappings.disabled = nil mappings.groups = nil -nvchad.load_mappings() -nvchad.load_mappings(mapping_groups) +utils.load_mappings() +utils.load_mappings(mapping_groups) wk.setup(options) diff --git a/lua/plugins/init.lua b/lua/plugins/init.lua index 0c3d16d..78bdaaa 100644 --- a/lua/plugins/init.lua +++ b/lua/plugins/init.lua @@ -71,9 +71,7 @@ local plugins = { config = function() require("plugins.configs.others").gitsigns() end, - setup = function() - nvchad.packer_lazy_load "gitsigns.nvim" - end, + setup = require("core.utils").packer_lazy_load "gitsigns.nvim", }, -- lsp stuff @@ -81,7 +79,7 @@ local plugins = { ["williamboman/nvim-lsp-installer"] = { opt = true, setup = function() - nvchad.packer_lazy_load "nvim-lsp-installer" + require("core.utils").packer_lazy_load "nvim-lsp-installer" -- reload the current file so lsp actually starts for it vim.defer_fn(function() vim.cmd 'if &ft == "packer" | echo "" | else | silent! e %' @@ -107,9 +105,7 @@ local plugins = { ["andymass/vim-matchup"] = { opt = true, - setup = function() - nvchad.packer_lazy_load "vim-matchup" - end, + setup = require("core.utils").packer_lazy_load "vim-matchup", }, ["max397574/better-escape.nvim"] = { @@ -202,9 +198,7 @@ local plugins = { ["folke/which-key.nvim"] = { opt = true, - setup = function() - nvchad.packer_lazy_load "which-key.nvim" - end, + setup = require("core.utils").packer_lazy_load "which-key.nvim", config = function() require "plugins.configs.whichkey" end,