remove usage of globals for util functions

This commit is contained in:
siduck 2022-05-29 16:07:47 +05:30
parent 0f013d4e7b
commit cb97cef2a7
18 changed files with 55 additions and 55 deletions

View File

@ -71,7 +71,7 @@ M.bufferline = {
-- close buffer + hide terminal buffer -- close buffer + hide terminal buffer
["<leader>x"] = { ["<leader>x"] = {
function() function()
nvchad.close_buffer() require("core.utils").close_buffer()
end, end,
" close buffer", " close buffer",
}, },

View File

@ -1,7 +1,7 @@
local opt = vim.opt local opt = vim.opt
local g = vim.g 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 -- use filetype.lua instead of filetype.vim
g.did_load_filetypes = 0 g.did_load_filetypes = 0
@ -86,4 +86,4 @@ vim.schedule(function()
end) end)
-- load user options if the file exists -- load user options if the file exists
nvchad.load_config().options.user() require("core.utils").load_config().options.user()

View File

@ -37,7 +37,7 @@ M.options = {
} }
-- merge overrides if there are any -- 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) M.run = function(plugins)
local present, packer = pcall(require, "packer") local present, packer = pcall(require, "packer")
@ -47,8 +47,8 @@ M.run = function(plugins)
end end
-- Override with chadrc values -- Override with chadrc values
plugins = nvchad.remove_default_plugins(plugins) plugins = require("core.utils").remove_default_plugins(plugins)
plugins = nvchad.merge_plugins(plugins) plugins = require("core.utils").merge_plugins(plugins)
packer.init(M.options) packer.init(M.options)

View File

@ -1,8 +1,8 @@
_G.nvchad = {} local M = {}
local merge_tb = vim.tbl_deep_extend local merge_tb = vim.tbl_deep_extend
nvchad.close_buffer = function(force) M.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)
return return
@ -24,14 +24,14 @@ nvchad.close_buffer = function(force)
vim.cmd(close_cmd) vim.cmd(close_cmd)
end end
nvchad.load_config = function() M.load_config = function()
local config = require "core.default_config" local config = require "core.default_config"
local chadrc_exists, chadrc = pcall(require, "custom.chadrc") local chadrc_exists, chadrc = pcall(require, "custom.chadrc")
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
if type(chadrc) == "table" then if type(chadrc) == "table" then
nvchad.remove_default_keys() M.remove_default_keys()
config = merge_tb("force", config, chadrc) config = merge_tb("force", config, chadrc)
else else
error "chadrc must return a table!" error "chadrc must return a table!"
@ -42,7 +42,7 @@ nvchad.load_config = function()
return config return config
end end
nvchad.remove_default_keys = function() M.remove_default_keys = function()
local chadrc = require "custom.chadrc" local chadrc = require "custom.chadrc"
local user_mappings = chadrc.mappings or {} local user_mappings = chadrc.mappings or {}
local user_keys = {} local user_keys = {}
@ -73,8 +73,8 @@ nvchad.remove_default_keys = function()
end end
end end
nvchad.load_mappings = function(mappings, mapping_opt) M.load_mappings = function(mappings, mapping_opt)
mappings = mappings or nvchad.load_config().mappings mappings = mappings or M.load_config().mappings
-- set mapping function with/without whichkye -- set mapping function with/without whichkye
local map_func local map_func
@ -114,7 +114,7 @@ nvchad.load_mappings = function(mappings, mapping_opt)
end end
-- load plugin after entering vim ui -- load plugin after entering vim ui
nvchad.packer_lazy_load = function(plugin, timer) M.packer_lazy_load = function(plugin, timer)
if plugin then if plugin then
timer = timer or 0 timer = timer or 0
vim.defer_fn(function() vim.defer_fn(function()
@ -124,8 +124,8 @@ nvchad.packer_lazy_load = function(plugin, timer)
end end
-- remove plugins defined in chadrc -- remove plugins defined in chadrc
nvchad.remove_default_plugins = function(plugins) M.remove_default_plugins = function(plugins)
local removals = nvchad.load_config().plugins.remove or {} local removals = M.load_config().plugins.remove or {}
if not vim.tbl_isempty(removals) then if not vim.tbl_isempty(removals) then
for _, plugin in pairs(removals) do for _, plugin in pairs(removals) do
@ -137,8 +137,8 @@ nvchad.remove_default_plugins = function(plugins)
end end
-- merge default/user plugin tables -- merge default/user plugin tables
nvchad.merge_plugins = function(default_plugins) M.merge_plugins = function(default_plugins)
local user_plugins = nvchad.load_config().plugins.user local user_plugins = M.load_config().plugins.user
-- merge default + user plugin table -- merge default + user plugin table
default_plugins = merge_tb("force", default_plugins, user_plugins) default_plugins = merge_tb("force", default_plugins, user_plugins)
@ -154,8 +154,8 @@ nvchad.merge_plugins = function(default_plugins)
return final_table return final_table
end end
nvchad.load_override = function(default_table, plugin_name) M.load_override = function(default_table, plugin_name)
local user_table = nvchad.load_config().plugins.override[plugin_name] local user_table = M.load_config().plugins.override[plugin_name]
if type(user_table) == "table" then if type(user_table) == "table" then
default_table = merge_tb("force", default_table, user_table) default_table = merge_tb("force", default_table, user_table)
@ -165,3 +165,5 @@ nvchad.load_override = function(default_table, plugin_name)
return default_table return default_table
end end
return M

View File

@ -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 -- dynamic header padding
local fn = vim.fn local fn = vim.fn

View File

@ -65,6 +65,6 @@ local options = {
} }
-- check for any override -- 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) bufferline.setup(options)

View File

@ -96,6 +96,6 @@ local options = {
} }
-- check for any override -- 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) cmp.setup(options)

View File

@ -118,6 +118,6 @@ local options = {
} }
-- check for any override -- 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) devicons.setup(options)

View File

@ -31,6 +31,6 @@ local options = {
max_concurrent_installers = 20, 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) lsp_installer.setup(options)

View File

@ -22,8 +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 lsp_mappings = nvchad.load_config().mappings.lspconfig local lsp_mappings = require("core.utils").load_config().mappings.lspconfig
nvchad.load_mappings({ lsp_mappings }, { buffer = bufnr }) require("core.utils").load_mappings({ lsp_mappings }, { buffer = bufnr })
end end
local capabilities = vim.lsp.protocol.make_client_capabilities() local capabilities = vim.lsp.protocol.make_client_capabilities()
@ -68,7 +68,7 @@ lspconfig.sumneko_lua.setup {
} }
-- requires a file containing user's lspconfigs -- 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 if #addlsp_confs ~= 0 then
require(addlsp_confs).setup_lsp(M.on_attach, capabilities) require(addlsp_confs).setup_lsp(M.on_attach, capabilities)

View File

@ -78,6 +78,6 @@ local options = {
} }
-- check for any override -- 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) nvimtree.setup(options)

View File

@ -27,6 +27,6 @@ local options = {
enable_new_mappings = true, enable_new_mappings = true,
} }
options = nvchad.load_override(options, "NvChad/nvterm") options = require("core.utils").load_override(options, "NvChad/nvterm")
nvterm.setup(options) nvterm.setup(options)

View File

@ -1,5 +1,7 @@
local M = {} local M = {}
local load_override = require("core.utils").load_override
M.autopairs = function() M.autopairs = function()
local present1, autopairs = pcall(require, "nvim-autopairs") local present1, autopairs = pcall(require, "nvim-autopairs")
local present2, cmp = pcall(require, "cmp") local present2, cmp = pcall(require, "cmp")
@ -32,7 +34,7 @@ M.better_escape = function()
keys = "<Esc>", keys = "<Esc>",
} }
options = nvchad.load_override(options, "max397574/better-escape.nvim") options = load_override(options, "max397574/better-escape.nvim")
escape.setup(options) escape.setup(options)
end end
@ -63,7 +65,7 @@ M.blankline = function()
show_first_indent_level = false, 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) blankline.setup(options)
end 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"]) colorizer.setup(options["filetypes"], options["user_default_options"])
vim.cmd "ColorizerReloadAllBuffers" vim.cmd "ColorizerReloadAllBuffers"
@ -149,7 +151,7 @@ M.signature = function()
padding = "", -- character to pad on left and right of signature can be ' ', or '|' etc 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) lsp_signature.setup(options)
end end

View File

@ -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 = { options.main_icon = {
provider = options.separator_style.main_icon, provider = options.separator_style.main_icon,
@ -312,7 +312,7 @@ options.current_line = {
hl = "Feline_CurrentLine", 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) local function add_table(tbl, inject)
if inject then if inject then

View File

@ -59,7 +59,7 @@ local options = {
} }
-- check for any override -- 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) telescope.setup(options)
-- load extensions -- load extensions

View File

@ -16,6 +16,6 @@ local options = {
} }
-- check for any override -- 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) treesitter.setup(options)

View File

@ -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) } local mapping_groups = { groups = vim.deepcopy(mappings.groups) }
mappings.disabled = nil mappings.disabled = nil
mappings.groups = nil mappings.groups = nil
nvchad.load_mappings() utils.load_mappings()
nvchad.load_mappings(mapping_groups) utils.load_mappings(mapping_groups)
wk.setup(options) wk.setup(options)

View File

@ -71,9 +71,7 @@ local plugins = {
config = function() config = function()
require("plugins.configs.others").gitsigns() require("plugins.configs.others").gitsigns()
end, end,
setup = function() setup = require("core.utils").packer_lazy_load "gitsigns.nvim",
nvchad.packer_lazy_load "gitsigns.nvim"
end,
}, },
-- lsp stuff -- lsp stuff
@ -81,7 +79,7 @@ local plugins = {
["williamboman/nvim-lsp-installer"] = { ["williamboman/nvim-lsp-installer"] = {
opt = true, opt = true,
setup = function() 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 -- reload the current file so lsp actually starts for it
vim.defer_fn(function() vim.defer_fn(function()
vim.cmd 'if &ft == "packer" | echo "" | else | silent! e %' vim.cmd 'if &ft == "packer" | echo "" | else | silent! e %'
@ -107,9 +105,7 @@ local plugins = {
["andymass/vim-matchup"] = { ["andymass/vim-matchup"] = {
opt = true, opt = true,
setup = function() setup = require("core.utils").packer_lazy_load "vim-matchup",
nvchad.packer_lazy_load "vim-matchup"
end,
}, },
["max397574/better-escape.nvim"] = { ["max397574/better-escape.nvim"] = {
@ -202,9 +198,7 @@ local plugins = {
["folke/which-key.nvim"] = { ["folke/which-key.nvim"] = {
opt = true, opt = true,
setup = function() setup = require("core.utils").packer_lazy_load "which-key.nvim",
nvchad.packer_lazy_load "which-key.nvim"
end,
config = function() config = function()
require "plugins.configs.whichkey" require "plugins.configs.whichkey"
end, end,