From a2ff5c285e3fe96026db2852f169a6703090e26e Mon Sep 17 00:00:00 2001 From: siduck Date: Mon, 23 May 2022 12:54:03 +0530 Subject: [PATCH] avoid un-needed lazy loading of packer | improve packer bootstrapping --- .github/README.md | 6 ++-- examples/chadrc.lua | 3 ++ init.lua | 31 ++++------------ lua/core/autocmds.lua | 73 -------------------------------------- lua/core/commands.lua | 37 ------------------- lua/core/init.lua | 18 ++++++++++ lua/core/packer.lua | 62 ++++++++++++++++++++++++++++++++ lua/core/utils.lua | 26 ++++++++++---- lua/plugins/init.lua | 26 +++----------- lua/plugins/packerInit.lua | 48 ------------------------- 10 files changed, 116 insertions(+), 214 deletions(-) delete mode 100644 lua/core/autocmds.lua delete mode 100644 lua/core/commands.lua create mode 100644 lua/core/init.lua create mode 100644 lua/core/packer.lua delete mode 100644 lua/plugins/packerInit.lua diff --git a/.github/README.md b/.github/README.md index 9f7a274..484c102 100644 --- a/.github/README.md +++ b/.github/README.md @@ -130,13 +130,13 @@ Without/with Treesitter: ## History -- I (@siduck i.e creator of NvChad) in my initial days of learning to program wanted a lightweight IDE for writing code, I had a very low end system, which was like 1.4ghz pentium + 4gb ram & HDD. I was into web dev so many suggested vscode but that thing was very heavy on my system, It took more ram than my browser! ( minimal ungoogled chromium ) so I never tried it again, sublime text was nice but the fear of using proprietary software XD for a linux user bugged me a lot. Then I tried doom-emacs which looked pretty but it was slow and emacs was kinda hard, I tried lunarvim but too lazy to read the docs which got me into vim and got into neovim lua stuff real quick. I wanted to make the config ery pretty and fast. +- I (@siduck i.e creator of NvChad) in my initial days of learning to program wanted a lightweight IDE for writing code, I had a very low end system which was like 1.4ghz pentium + 4gb ram & HDD. I was into web dev stuff so many suggested me to use vscode but that thing was very heavy on my system, It took more ram than my browser! ( minimal ungoogled chromium ) so I never tried it again, sublime text was nice but the fear of using proprietary software XD for a linux user bugged me a lot. Then I tried doom-emacs which looked pretty but it was slow and I was lost within its docs, I tried lunarvim but too lazy to read the docs. Doom-emacs and lunarvim inspired me to make a config which is the prettiest + very fast and simple. -- I'm decent at ricing i.e customizing system and making it look pretty so I posted my neovim rice on [neovim subreddit](https://www.reddit.com/r/neovim/comments/m3xl4f/neovim_rice/), my neovim-dotfiles github repo blew up and then I had to come up with a name, I was amazed by the chad meme lol so I put NvChad as the name, the chad word in the name doesnt literally mean the chad guy but in the sense such as chad linux vs windows i.e meaning superior,best etc. NvChad was made for my personal use but it gained some popularity which inspired me to make a public config i.e config usable by many and less hassle to update, without the custom config stuff users would have to keep a track of every commit and copy paste git diffs to manually update nvchad. +- I'm decent at ricing i.e customizing system and making it look pretty so I posted my neovim rice on [neovim subreddit](https://www.reddit.com/r/neovim/comments/m3xl4f/neovim_rice/), my neovim-dotfiles github repo blew up and then I had to come up with a name, I was amazed by the chad meme lol so I put NvChad as the name, the chad word in here doesnt literally mean the chad guy but in the sense such as chad linux vs windows i.e meaning superior, best etc. NvChad was made for my personal use but it gained some popularity which inspired me to make a public config i.e config usable by many and less hassle to update as everyone's going to use the same base config (NvChad) with their custom modifications (which are gitignored so that wont mess up), without the custom config stuff users would have to keep a track of every commit and copy paste git diffs to manually update nvchad. ## :gift_heart: Support -I'm (@siduck) really very sorry to remove the previous charity links but I had to do it for various reasons. A lot has been going here regarding financial issues and I do need some support if it's possible from your end. I haven't graduated yet and it would be great to earn some stuff as that'd help me with daily minor expenses. If you like NvChad and would like to support it via donation then I'll gladly accept it. Dont worry! NvChad will still stay alive & active without your donations! I will remove the sponsor button and re-add the charity links once I get a job, probably a year or two. +I'm (@siduck) really very sorry to remove the previous charity links but I had to do it for various reasons. A lot has been going here regarding financial issues and I do need some support if it's possible from your end. I haven't graduated yet and it would be great to earn some stuff as that'd help me with daily minor expenses. If you like NvChad and would like to support & appreciate it via donation then I'll gladly accept it. Dont worry! NvChad will still stay alive & active without your donations! I will remove the sponsor button and re-add the charity links once I get a job, probably a year or two. [![kofi](https://img.shields.io/badge/Ko--fi-F16061?style=for-the-badge&logo=ko-fi&logoColor=white)](https://ko-fi.com/siduck) [![paypal](https://img.shields.io/badge/PayPal-00457C?style=for-the-badge&logo=paypal&logoColor=white)](https://paypal.me/siduck76) diff --git a/examples/chadrc.lua b/examples/chadrc.lua index 6aa2521..9b03aab 100644 --- a/examples/chadrc.lua +++ b/examples/chadrc.lua @@ -1,5 +1,8 @@ -- Just an example, supposed to be placed in /lua/custom/ +-- remove this if you dont use custom.init.lua at all +require "custom" + local M = {} -- make sure you maintain the structure of `core/default_config.lua` here, diff --git a/init.lua b/init.lua index be2faf0..1fad1f1 100644 --- a/init.lua +++ b/init.lua @@ -4,29 +4,10 @@ if present then impatient.enable_profile() end -local modules = { - "utils", - "options", - "autocmds", - "commands", -} +require "core" +require "core.utils" +require "core.options" -for _, module in ipairs(modules) do - local ok, err = pcall(require, "core." .. module) - - if not ok then - error("Error loading " .. module .. "\n\n" .. err) - end -end - --- check if custom init.lua file exists -if vim.fn.filereadable(vim.fn.stdpath "config" .. "/lua/custom/init.lua") == 1 then - -- try to call custom init, if not successful, show error - local ok, err = pcall(require, "custom") - - if not ok then - vim.notify("Error loading custom/init.lua\n\n" .. err) - end - - return -end +-- setup packer + plugins +require("core.packer").bootstrap() +require "plugins" diff --git a/lua/core/autocmds.lua b/lua/core/autocmds.lua deleted file mode 100644 index 409abbc..0000000 --- a/lua/core/autocmds.lua +++ /dev/null @@ -1,73 +0,0 @@ -local autocmd = vim.api.nvim_create_autocmd - --- Disable statusline in dashboard -autocmd("FileType", { - pattern = "alpha", - callback = function() - vim.opt.laststatus = 0 - end, -}) - -autocmd("BufUnload", { - buffer = 0, - callback = function() - vim.opt.laststatus = 3 - end, -}) - --- open nvim with a dir while still lazy loading nvimtree --- autocmd("BufEnter", { --- callback = function() --- if vim.api.nvim_buf_get_option(0, "buftype") ~= "terminal" then --- vim.cmd "lcd %:p:h" --- end --- end, --- }) - --- Use relative & absolute line numbers in 'n' & 'i' modes respectively --- autocmd("InsertEnter", { --- callback = function() --- vim.opt.relativenumber = false --- end, --- }) --- autocmd("InsertLeave", { --- callback = function() --- vim.opt.relativenumber = true --- end, --- }) - --- Open a file from its last left off position --- autocmd("BufReadPost", { --- callback = function() --- if not vim.fn.expand("%:p"):match ".git" and vim.fn.line "'\"" > 1 and vim.fn.line "'\"" <= vim.fn.line "$" then --- vim.cmd "normal! g'\"" --- vim.cmd "normal zz" --- end --- end, --- }) - --- File extension specific tabbing --- autocmd("Filetype", { --- pattern = "python", --- callback = function() --- vim.opt_local.expandtab = true --- vim.opt_local.tabstop = 4 --- vim.opt_local.shiftwidth = 4 --- vim.opt_local.softtabstop = 4 --- end, --- }) - --- Highlight yanked text --- autocmd("TextYankPost", { --- callback = function() --- vim.highlight.on_yank { higroup = "Visual", timeout = 200 } --- end, --- }) - --- Enable spellchecking in markdown, text and gitcommit files --- autocmd("FileType", { --- pattern = { "gitcommit", "markdown", "text" }, --- callback = function() --- vim.opt_local.spell = true --- end, --- }) diff --git a/lua/core/commands.lua b/lua/core/commands.lua deleted file mode 100644 index 9c12009..0000000 --- a/lua/core/commands.lua +++ /dev/null @@ -1,37 +0,0 @@ -local user_cmd = vim.api.nvim_create_user_command -local cmd = vim.cmd - --- snapshot stuff -user_cmd("PackerSnapshot", function(info) - require "plugins" - require("packer").snapshot(info.args) -end, { nargs = "+" }) - -user_cmd("PackerSnapshotDelete", function(info) - require "plugins" - require("packer.snapshot").delete(info.args) -end, { nargs = "+" }) - -user_cmd("PackerSnapshotRollback", function(info) - require "plugins" - require("packer").rollback(info.args) -end, { nargs = "+" }) - --- Add Packer commands because we are not loading it at startup - -local packer_cmd = function(callback) - return function() - require "plugins" - require("packer")[callback]() - end -end - -user_cmd("PackerClean", packer_cmd "clean", {}) -user_cmd("PackerCompile", packer_cmd "compile", {}) -user_cmd("PackerInstall", packer_cmd "install", {}) -user_cmd("PackerStatus", packer_cmd "status", {}) -user_cmd("PackerSync", packer_cmd "sync", {}) -user_cmd("PackerUpdate", packer_cmd "update", {}) - --- add NvChadUpdate command and mapping -cmd "silent! command! NvChadUpdate lua require('nvchad').update_nvchad()" diff --git a/lua/core/init.lua b/lua/core/init.lua new file mode 100644 index 0000000..8e089d5 --- /dev/null +++ b/lua/core/init.lua @@ -0,0 +1,18 @@ +vim.cmd "silent! command! NvChadUpdate lua require('nvchad').update_nvchad()" + +local autocmd = vim.api.nvim_create_autocmd + +-- Disable statusline in dashboard +autocmd("FileType", { + pattern = "alpha", + callback = function() + vim.opt.laststatus = 0 + end, +}) + +autocmd("BufUnload", { + buffer = 0, + callback = function() + vim.opt.laststatus = 3 + end, +}) diff --git a/lua/core/packer.lua b/lua/core/packer.lua new file mode 100644 index 0000000..2bd4a6e --- /dev/null +++ b/lua/core/packer.lua @@ -0,0 +1,62 @@ +local M = {} + +M.bootstrap = function() + local fn = vim.fn + local install_path = fn.stdpath "data" .. "/site/pack/packer/start/packer.nvim" + + vim.api.nvim_set_hl(0, "NormalFloat", { bg = "#1e222a" }) + + if fn.empty(fn.glob(install_path)) > 0 then + print "Cloning packer .." + + fn.system { "git", "clone", "--depth", "1", "https://github.com/wbthomason/packer.nvim", install_path } + + print "Packer cloned successfully!" + + -- install plugins + compile their configs + vim.cmd "packadd packer.nvim" + require "plugins" + vim.cmd "PackerSync" + end +end + +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 = nvchad.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 = nvchad.remove_default_plugins(plugins) + plugins = nvchad.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 6676e74..d9662e8 100644 --- a/lua/core/utils.lua +++ b/lua/core/utils.lua @@ -28,12 +28,16 @@ nvchad.load_config = function() -- attempt to load and merge a user config local chadrc_exists = vim.fn.filereadable(vim.fn.stdpath "config" .. "/lua/custom/chadrc.lua") == 1 + if chadrc_exists then -- merge user config if it exists and is a table; otherwise display an error local user_config = require "custom.chadrc" + 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 {} + user_config.mappings = user_config.mappings + 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!" @@ -46,6 +50,7 @@ end -- reduces a given keymap to a table of modes each containing a list of key maps nvchad.reduce_key_map = function(key_map, ignore_modes) local prune_keys = {} + for _, modes in pairs(key_map) do for mode, mappings in pairs(modes) do if not vim.tbl_contains(ignore_modes, mode) then @@ -60,23 +65,32 @@ end -- remove disabled mappings from a given key map nvchad.remove_disabled_mappings = function(key_map) local clean_map = {} + if key_map == nil or key_map == "" then return key_map end + if type(key_map) == "table" then for k, v in pairs(key_map) do - if v ~= nil and v ~= "" then clean_map[k] = v end + if v ~= nil and v ~= "" then + clean_map[k] = v + end end end + return clean_map end -- prune keys from a key map table by matching against another key map table nvchad.prune_key_map = function(key_map, prune_map, ignore_modes) - if not prune_map then return key_map end - if not key_map then return prune_map end + if not prune_map then + return key_map + end + if not key_map then + 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 @@ -155,7 +169,7 @@ nvchad.remove_default_plugins = function(plugins) end -- merge default/user plugin tables -nvchad.plugin_list = function(default_plugins) +nvchad.merge_plugins = function(default_plugins) local user_plugins = nvchad.load_config().plugins.user -- merge default + user plugin table diff --git a/lua/plugins/init.lua b/lua/plugins/init.lua index 24739b3..c6b2746 100644 --- a/lua/plugins/init.lua +++ b/lua/plugins/init.lua @@ -1,21 +1,12 @@ -local present, packer = pcall(require, "plugins.packerInit") - -if not present then - return false -end - local plugins = { + ["nvim-lua/plenary.nvim"] = {}, ["lewis6991/impatient.nvim"] = {}, - - ["wbthomason/packer.nvim"] = { - event = "VimEnter", - }, - + ["wbthomason/packer.nvim"] = {}, ["NvChad/extensions"] = {}, ["NvChad/base46"] = { - after = "packer.nvim", + after = "plenary.nvim", config = function() local ok, base46 = pcall(require, "base46") @@ -219,13 +210,4 @@ local plugins = { }, } -plugins = nvchad.remove_default_plugins(plugins) - --- merge user plugin table & default plugin table -plugins = nvchad.plugin_list(plugins) - -return packer.startup(function(use) - for _, v in pairs(plugins) do - use(v) - end -end) +require("core.packer").run(plugins) diff --git a/lua/plugins/packerInit.lua b/lua/plugins/packerInit.lua deleted file mode 100644 index fbf26c7..0000000 --- a/lua/plugins/packerInit.lua +++ /dev/null @@ -1,48 +0,0 @@ -vim.cmd "packadd packer.nvim" - -local present, packer = pcall(require, "packer") - -if not present then - local packer_path = vim.fn.stdpath "data" .. "/site/pack/packer/opt/packer.nvim" - - print "Cloning packer.." - -- remove the dir before cloning - vim.fn.delete(packer_path, "rf") - vim.fn.system { - "git", - "clone", - "https://github.com/wbthomason/packer.nvim", - "--depth", - "20", - packer_path, - } - - vim.cmd "packadd packer.nvim" - present, packer = pcall(require, "packer") - - if present then - print "Packer cloned successfully." - else - error("Couldn't clone packer !\nPacker path: " .. packer_path .. "\n" .. packer) - end -end - -local options = { - display = { - open_fn = function() - return require("packer.util").float { border = "single" } - end, - }, - git = { - clone_timeout = 6000, -- seconds - }, - auto_clean = true, - compile_on_sync = true, - snapshot = nil, -} - -options = nvchad.load_override(options, "wbthomason/packer.nvim") - -packer.init(options) - -return packer