From d61946d3bfa304e8924120888611d64ee9f98f51 Mon Sep 17 00:00:00 2001 From: siduck Date: Fri, 2 Sep 2022 11:05:50 +0530 Subject: [PATCH] Simplify plugin management even more (#1518) --- lua/core/default_config.lua | 6 +--- lua/core/packer.lua | 42 ----------------------- lua/core/utils.lua | 41 ++++++++++------------- lua/plugins/configs/others.lua | 26 ++++++++++----- lua/plugins/init.lua | 61 +++++++++++++++++++--------------- 5 files changed, 70 insertions(+), 106 deletions(-) delete mode 100644 lua/core/packer.lua diff --git a/lua/core/default_config.lua b/lua/core/default_config.lua index 17444b0..1c7152b 100644 --- a/lua/core/default_config.lua +++ b/lua/core/default_config.lua @@ -19,11 +19,7 @@ M.ui = { transparency = false, } -M.plugins = { - override = {}, - remove = {}, - user = {}, -} +M.plugins = {} -- check core.mappings for table structure M.mappings = require "core.mappings" diff --git a/lua/core/packer.lua b/lua/core/packer.lua deleted file mode 100644 index 33bfa38..0000000 --- a/lua/core/packer.lua +++ /dev/null @@ -1,42 +0,0 @@ -local M = {} - -M.options = { - auto_clean = true, - compile_on_sync = true, - git = { clone_timeout = 6000 }, - display = { - working_sym = "ﲊ", - error_sym = "✗ ", - done_sym = " ", - removed_sym = " ", - moved_sym = "", - open_fn = function() - return require("packer.util").float { border = "single" } - end, - }, -} - --- merge overrides if there are any -M.options = require("core.utils").load_override(M.options, "wbthomason/packer.nvim") - -M.run = function(plugins) - local present, packer = pcall(require, "packer") - - if not present then - return - end - - -- Override with chadrc values - plugins = require("core.utils").remove_default_plugins(plugins) - plugins = require("core.utils").merge_plugins(plugins) - - packer.init(M.options) - - packer.startup(function(use) - for _, v in pairs(plugins) do - use(v) - end - end) -end - -return M diff --git a/lua/core/utils.lua b/lua/core/utils.lua index b89b9d7..d95cb92 100644 --- a/lua/core/utils.lua +++ b/lua/core/utils.lua @@ -99,40 +99,33 @@ M.load_mappings = function(section, mapping_opt) end end --- remove plugins defined in chadrc -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 - plugins[plugin] = nil - end - end - - return plugins -end - -- merge default/user plugin tables 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) + default_plugins = merge_tb("force", default_plugins, M.load_config().plugins) local final_table = {} - for key, _ in pairs(default_plugins) do - default_plugins[key][1] = key - final_table[#final_table + 1] = default_plugins[key] + for key, val in pairs(default_plugins) do + if val then + default_plugins[key][1] = key + final_table[#final_table + 1] = default_plugins[key] + end end return final_table end -M.load_override = function(default_table, plugin_name) - local user_table = M.load_config().plugins.override[plugin_name] or {} - user_table = type(user_table) == "table" and user_table or user_table() - return merge_tb("force", default_table, user_table) or {} +-- override plugin options table with custom ones +M.load_override = function(options_table, name) + local user_plugins = M.load_config().plugins + local plugin_options = {} + + if user_plugins[name] then + plugin_options = user_plugins[name].override_options or {} + plugin_options = type(plugin_options) == "table" and plugin_options or plugin_options() + end + + return merge_tb("force", options_table, plugin_options) end M.packer_sync = function(...) diff --git a/lua/plugins/configs/others.lua b/lua/plugins/configs/others.lua index 9d0fef3..e0870d2 100644 --- a/lua/plugins/configs/others.lua +++ b/lua/plugins/configs/others.lua @@ -2,14 +2,6 @@ local M = {} local load_override = require("core.utils").load_override -M.nvchad_ui = function() - local present, nvchad_ui = pcall(require, "nvchad_ui") - - if present then - nvchad_ui.setup() - end -end - M.autopairs = function() local present1, autopairs = pcall(require, "nvim-autopairs") local present2, cmp = pcall(require, "cmp") @@ -170,4 +162,22 @@ M.devicons = function() end end +M.packer_init = function() + return { + auto_clean = true, + compile_on_sync = true, + git = { clone_timeout = 6000 }, + display = { + working_sym = "ﲊ", + error_sym = "✗ ", + done_sym = " ", + removed_sym = " ", + moved_sym = "", + open_fn = function() + return require("packer.util").float { border = "single" } + end, + }, + } +end + return M diff --git a/lua/plugins/init.lua b/lua/plugins/init.lua index 823654d..9a4eeda 100644 --- a/lua/plugins/init.lua +++ b/lua/plugins/init.lua @@ -1,14 +1,16 @@ -vim.cmd "packadd packer.nvim" - local plugins = { ["nvim-lua/plenary.nvim"] = { module = "plenary" }, + + ["lewis6991/impatient.nvim"] = {}, + ["wbthomason/packer.nvim"] = { cmd = require("core.lazy_load").packer_cmds, config = function() require "plugins" end, }, + ["NvChad/extensions"] = { module = { "telescope", "nvchad" } }, ["NvChad/base46"] = { @@ -24,7 +26,11 @@ local plugins = { ["NvChad/ui"] = { after = "base46", config = function() - require("plugins.configs.others").nvchad_ui() + local present, nvchad_ui = pcall(require, "nvchad_ui") + + if present then + nvchad_ui.setup() + end end, }, @@ -91,7 +97,6 @@ local plugins = { }, -- lsp stuff - ["williamboman/mason.nvim"] = { cmd = require("core.lazy_load").mason_cmds, config = function() @@ -131,25 +136,11 @@ local plugins = { end, }, - ["saadparwaiz1/cmp_luasnip"] = { - after = "LuaSnip", - }, - - ["hrsh7th/cmp-nvim-lua"] = { - after = "cmp_luasnip", - }, - - ["hrsh7th/cmp-nvim-lsp"] = { - after = "cmp-nvim-lua", - }, - - ["hrsh7th/cmp-buffer"] = { - after = "cmp-nvim-lsp", - }, - - ["hrsh7th/cmp-path"] = { - after = "cmp-buffer", - }, + ["saadparwaiz1/cmp_luasnip"] = { after = "LuaSnip" }, + ["hrsh7th/cmp-nvim-lua"] = { after = "cmp_luasnip" }, + ["hrsh7th/cmp-nvim-lsp"] = { after = "cmp-nvim-lua" }, + ["hrsh7th/cmp-buffer"] = { after = "cmp-nvim-lsp" }, + ["hrsh7th/cmp-path"] = { after = "cmp-buffer" }, -- misc plugins ["windwp/nvim-autopairs"] = { @@ -212,9 +203,25 @@ local plugins = { require("core.utils").load_mappings "whichkey" end, }, - - -- Speed up deffered plugins - ["lewis6991/impatient.nvim"] = {}, } -require("core.packer").run(plugins) +-- Load all plugins +local present, packer = pcall(require, "packer") + +if present then + vim.cmd "packadd packer.nvim" + + -- Override with default plugins with user ones + plugins = require("core.utils").merge_plugins(plugins) + + -- load packer init options + local init_options = require("plugins.configs.others").packer_init() + init_options = require("core.utils").load_override(init_options, "wbthomason/packer.nvim") + packer.init(init_options) + + packer.startup(function(use) + for _, v in pairs(plugins) do + use(v) + end + end) +end