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

@ -144,6 +144,4 @@ M.mappings = {
},
}
M = vim.tbl_deep_extend("force", require "default_config", M)
return M

View File

@ -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", "<Esc>", ":noh<CR>", 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)<CR>", opt)
map("n", m, ":lua require('utils').toggle_theme(require('utils').load_config().ui.fav_themes)<CR>", opt)
end
-- Packer commands till because we are not loading it at startup

View File

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

View File

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

View File

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

View File

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

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