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
This commit is contained in:
Akianonymus
2021-08-19 08:51:42 +05:30
committed by siduck76
parent 02f0122ab4
commit 7753e03b9e
7 changed files with 98 additions and 12 deletions

View File

@@ -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