From 7bb3afbb969807db7d6638c5695d61853584be81 Mon Sep 17 00:00:00 2001 From: Leon Heidelbach Date: Mon, 20 Jun 2022 01:26:13 +0200 Subject: [PATCH] feat: add warning message when using PackerSync on snapshot branches --- lua/core/init.lua | 13 ++++++++++--- lua/core/utils.lua | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/lua/core/init.lua b/lua/core/init.lua index edb0a71..e0a889a 100644 --- a/lua/core/init.lua +++ b/lua/core/init.lua @@ -4,9 +4,17 @@ vim.cmd "silent! command! NvChadSnapshotCreate lua require('nvchad').snap_create vim.cmd "silent! command! NvChadSnapshotDelete lua require('nvchad').snap_delete()" vim.cmd "silent! command! NvChadSnapshotCheckout lua require('nvchad').snap_checkout()" + -- autocmds local autocmd = vim.api.nvim_create_autocmd +-- wrap the PackerSync command to warn people before using it in NvChadSnapshots +autocmd("VimEnter", { + callback = function() + vim.cmd "command! -nargs=* -complete=customlist,v:lua.require'packer'.plugin_complete PackerSync lua require('core.utils').packer_sync()" + end, +}) + -- Disable statusline in dashboard autocmd("FileType", { pattern = "alpha", @@ -30,9 +38,8 @@ autocmd("BufEnter", { autocmd("InsertLeave", { callback = function() - if - require("luasnip").session.current_nodes[vim.api.nvim_get_current_buf()] - and not require("luasnip").session.jump_active + if require("luasnip").session.current_nodes[vim.api.nvim_get_current_buf()] + and not require("luasnip").session.jump_active then require("luasnip").unlink_current() end diff --git a/lua/core/utils.lua b/lua/core/utils.lua index 7090e7e..62d838f 100644 --- a/lua/core/utils.lua +++ b/lua/core/utils.lua @@ -160,4 +160,38 @@ M.load_override = function(default_table, plugin_name) return default_table end +M.packer_sync = function(...) + local git_exists, git = pcall(require, "nvchad.utils.git") + local defaults_exists, defaults = pcall(require, "nvchad.utils.config") + local packer_exists, packer = pcall(require, "packer") + + if git_exists and defaults_exists then + local current_branch_name = git.get_current_branch_name() + + -- warn the user if we are on a snapshot branch + if current_branch_name:match(defaults.snaps.base_snap_branch_name .. "(.+)" .. "$") then + vim.api.nvim_echo({ + { "WARNING: You are trying to use ", "WarningMsg" }, + { "PackerSync" }, + { " on a NvChadSnapshot. This will cause issues if NvChad dependencies contain " + .. "any breaking changes! Plugin updates will not be included in this " + .. "snapshot, so they will be lost after switching between snapshots! Would " + .. "you still like to continue? [y/N]\n", "WarningMsg" } + }, false, {}) + + local ans = vim.trim(string.lower(vim.fn.input("-> "))) + + if ans ~= "y" then + return + end + end + end + + if packer_exists then + packer.sync(...) + else + error("Packer could not be loaded!") + end +end + return M