From 7753e03b9e561fa359912c1d632a5e69c035d90c Mon Sep 17 00:00:00 2001 From: Akianonymus Date: Thu, 19 Aug 2021 08:51:42 +0530 Subject: [PATCH] feat: Do not depend on user config | Fix merging of configs because it is a user config, so our config shoudn't break even we if dont have it use our own table merge function move loading config to a function use a global variable to store the config, so no need to call the table function everytime --- lua/chadrc.lua | 2 - lua/mappings.lua | 8 ++-- lua/options.lua | 8 +++- lua/pluginList.lua | 2 +- lua/plugins/chadsheet.lua | 2 +- lua/theme.lua | 2 +- lua/utils.lua | 86 ++++++++++++++++++++++++++++++++++++++- 7 files changed, 98 insertions(+), 12 deletions(-) diff --git a/lua/chadrc.lua b/lua/chadrc.lua index acb5012..7c98cd3 100644 --- a/lua/chadrc.lua +++ b/lua/chadrc.lua @@ -144,6 +144,4 @@ M.mappings = { }, } -M = vim.tbl_deep_extend("force", require "default_config", M) - return M diff --git a/lua/mappings.lua b/lua/mappings.lua index 14740aa..bcf5e96 100644 --- a/lua/mappings.lua +++ b/lua/mappings.lua @@ -1,4 +1,4 @@ -local user_map = require("chadrc").mappings +local user_map = require("utils").load_config().mappings local miscMap = user_map.misc local cmd = vim.cmd @@ -65,7 +65,7 @@ M.misc = function() map("n", "", ":noh", opt) -- navigation within insert mode - local check_insertNav = require("chadrc").options.enable_insertNav + local check_insertNav = require("utils").load_config().options.enable_insertNav if check_insertNav == true then local m = user_map.insert_nav @@ -79,11 +79,11 @@ M.misc = function() end -- check the theme toggler - local theme_toggler = require("chadrc").ui.theme_toggler + local theme_toggler = require("utils").load_config().ui.theme_toggler if theme_toggler == true then local m = user_map.misc.theme_toggle - map("n", m, ":lua require('utils').toggle_theme(require('chadrc').ui.fav_themes)", opt) + map("n", m, ":lua require('utils').toggle_theme(require('utils').load_config().ui.fav_themes)", opt) end -- Packer commands till because we are not loading it at startup diff --git a/lua/options.lua b/lua/options.lua index c64589b..e0aff03 100644 --- a/lua/options.lua +++ b/lua/options.lua @@ -1,7 +1,11 @@ -local options = require("chadrc").options local opt = vim.opt local g = vim.g +-- export user config as a global varibale +g.nvchad_user_config = "chadrc" + +local options = require("utils").load_config().options + opt.completeopt = { "menuone", "noselect" } opt.undofile = options.permanent_undo opt.ruler = options.ruler @@ -76,7 +80,7 @@ vim.cmd [[ au TermOpen term://* setlocal nonumber norelativenumber ]] -- Don't show status line on certain windows vim.cmd [[ au TermOpen term://* setfiletype terminal ]] -vim.cmd [[ let hidden_statusline = luaeval('require("chadrc").ui.hidden_statusline') | autocmd BufEnter,BufWinEnter,WinEnter,CmdwinEnter,TermEnter * nested if index(hidden_statusline, &ft) >= 0 | set laststatus=0 | else | set laststatus=2 | endif ]] +vim.cmd [[ let hidden_statusline = luaeval('require("utils").load_config().ui.hidden_statusline') | autocmd BufEnter,BufWinEnter,WinEnter,CmdwinEnter,TermEnter * nested if index(hidden_statusline, &ft) >= 0 | set laststatus=0 | else | set laststatus=2 | endif ]] -- Open a file from its last left off position -- vim.cmd [[ au BufReadPost * if expand('%:p') !~# '\m/\.git/' && line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif ]] diff --git a/lua/pluginList.lua b/lua/pluginList.lua index df3cdcf..5c56855 100644 --- a/lua/pluginList.lua +++ b/lua/pluginList.lua @@ -1,4 +1,4 @@ -local plugin_status = require("chadrc").plugin_status +local plugin_status = require("utils").load_config().plugin_status local present, _ = pcall(require, "packerInit") local packer diff --git a/lua/plugins/chadsheet.lua b/lua/plugins/chadsheet.lua index 4ccd2b5..21862c1 100644 --- a/lua/plugins/chadsheet.lua +++ b/lua/plugins/chadsheet.lua @@ -4,7 +4,7 @@ if not present then return end -local mappings = require("chadrc").mappings +local mappings = require("utils").load_config().mappings -- add user mappings to the cheetsheet for section, data in pairs(mappings) do diff --git a/lua/theme.lua b/lua/theme.lua index 04f0871..3002ac2 100644 --- a/lua/theme.lua +++ b/lua/theme.lua @@ -1,4 +1,4 @@ -local chad_theme = require("chadrc").ui.theme +local chad_theme = require("utils").load_config().ui.theme vim.g.nvchad_theme = chad_theme local present, base16 = pcall(require, "base16") diff --git a/lua/utils.lua b/lua/utils.lua index afde106..fb7c13f 100644 --- a/lua/utils.lua +++ b/lua/utils.lua @@ -10,7 +10,8 @@ M.change_theme = function(current_theme, new_theme) return end - local file = vim.fn.stdpath "config" .. "/lua/chadrc.lua" + local user_config = vim.g.nvchad_user_config + local file = vim.fn.stdpath "config" .. "/lua/" .. user_config .. ".lua" -- store in data variable local data = assert(M.file("r", file)) -- escape characters which can be parsed as magic chars @@ -195,6 +196,89 @@ M.list_themes = function(return_type) return themes end +-- Base code: https://gist.github.com/revolucas/184aec7998a6be5d2f61b984fac1d7f7 +-- Changes over it: preserving table 1 contents and also update with table b, without duplicating +-- 1st arg - base table, 2nd arg - table to merge +M.merge_table = function(into, from) + -- make sure both are table + if type(into) ~= "table" or type(from) ~= "table" then + return 1 + end + local stack, seen = {}, {} + local table1, table2 = into, from + while true do + for k, v in pairs(table2) do + if type(v) == "table" and type(table1[k]) == "table" then + table.insert(stack, { table1[k], table2[k] }) + else + local present = seen[v] or false + if not present then + -- add the value to seen table until value is found + -- todo: maybe improve this + for _, value in pairs(table1) do + if value == v then + present = true + break + end + end + end + seen[v] = true + if not present then + -- if type is number, then it is a sub table value, so append + if type(k) == "number" then + table1[#table1 + 1] = v + else + table1[k] = v + end + end + end + end + if #stack > 0 then + local t = stack[#stack] + table1, table2 = t[1], t[2] + stack[#stack] = nil + else + break + end + end + return into +end + +-- load config +-- 1st arg = boolean - whether to force reload +-- Modifies _G._NVCHAD_CONFIG global variable +M.load_config = function(reload) + -- only do the stuff below one time, otherwise just return the set config + if _G._NVCHAD_CONFIG_CONTENTS ~= nil and not (reload or false) then + return _G._NVCHAD_CONFIG_CONTENTS + end + + -- don't enclose in pcall, it better break when default config is faulty + _G._NVCHAD_CONFIG_CONTENTS = require "default_config" + + -- user config is not required to run nvchad but a optional + -- Make sure the config doesn't break the whole system if user config is not present or in bad state or not a table + -- print warning texts if user config file is present + local config_name = vim.g.nvchad_user_config or "chadrc" + local config_file = vim.fn.stdpath "config" .. "/lua/" .. config_name .. ".lua" + -- check if the user config is present + if vim.fn.empty(vim.fn.glob(config_file)) < 1 then + local present, config = pcall(require, config_name) + if present then + -- make sure the returned value is table + if type(config) == "table" then + -- data = require(config_name) + _G._NVCHAD_CONFIG_CONTENTS = require("utils").merge_table(_G._NVCHAD_CONFIG_CONTENTS, config) + else + print("Warning: " .. config_name .. " sourced successfully but did not return a lua table.") + end + else + print("Warning: " .. config_file .. " is present but sourcing failed.") + end + end + return _G._NVCHAD_CONFIG_CONTENTS +end + -- reload a plugin ( will try to load even if not loaded) -- can take a string or list ( table ) -- return true or false