use tbl_deep_extend to merge configs | rm other functions

This commit is contained in:
siduck 2021-11-15 21:39:35 +05:30
parent e8b919723e
commit 1567a9c73a
3 changed files with 14 additions and 162 deletions

View file

@ -2,7 +2,6 @@
-- use custom/chadrc.lua instead
local M = {}
M.options, M.ui, M.mappings, M.plugins = {}, {}, {}, {}
M.options = {
-- custom = {}

View file

@ -137,57 +137,21 @@ M.hide_statusline = function()
end
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
local conf = require "core.default_config"
-- these are the table value which will be always prioritiezed to take user config value
local to_replace = {
"['mappings']['plugins']['esc_insertmode']",
"['mappings']['terminal']['esc_termmode']",
"['mappings']['terminal']['esc_hide_termmode']",
}
local chadrcExists, _ = pcall(require, "custom.chadrc")
local default_config = "core.default_config"
local config_file = vim.fn.stdpath "config" .. "/lua/custom/" .. "chadrc.lua"
-- if chadrc exists , then merge its table into the default config's
-- unload the modules if force reload
if reload then
package.loaded[default_config or false] = nil
package.loaded["chadrc" or false] = nil
end
if chadrcExists then
local change = require "custom.chadrc"
conf = vim.tbl_deep_extend("force", conf, change)
return conf
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
-- check if the user config is present
if vim.fn.filereadable(vim.fn.glob(config_file)) == 1 then
local present, config = pcall(require, "custom/chadrc")
if present then
-- make sure the returned value is table
if type(config) == "table" then
-- data = require(config_name)
_G._NVCHAD_CONFIG_CONTENTS = require("core.utils").merge_table(
_G._NVCHAD_CONFIG_CONTENTS,
config,
to_replace
)
else
print("Warning: chadrc " .. " 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
-- or load default config
return conf
end
M.map = function(mode, keys, cmd, opt)
@ -238,88 +202,6 @@ M.map = function(mode, keys, cmd, opt)
map_wrapper(mode, keys, cmd, options)
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
-- 3rg arg - list of nodes as a table, if the node is found replace the from table2 to result, rather than adding the value
-- e.g: merge_table(t1, t2, { ['mappings']['plugins']['bufferline'] })
M.merge_table = function(into, from, nodes_to_replace)
-- make sure both are table
if type(into) ~= "table" or type(from) ~= "table" then
return into
end
local stack, seen = {}, {}
local table1, table2 = into, from
if type(nodes_to_replace) == "table" then
-- function that will be executed with loadstring
local replace_fn = function(node)
local base_fn = [[
return function(table1, table2)
local t1, t2 = table1_node or false , table2_node or false
if t1 and t2 then
table1_node = table2_node
end
return table1
end]]
-- replace the _node in base_fn to actual given node value
local fn = base_fn:gsub("_node", node)
-- return the function created from the string base_fn
return loadstring(fn)()(table1, table2)
end
for _, node in ipairs(nodes_to_replace) do
-- pcall() is a poor workaround for if "['mappings']['plugins']['esc_insertmode']" 'plugins' sub-table does not exist
local ok, result = pcall(replace_fn, node)
if ok then
-- if the node is found then replace
table1 = result
end
end
end
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
if type(k) == "number" then
-- add the value to seen table until value is found
-- only do when key is number we just want to append to subtables
-- todo: maybe improve this
for _, value in pairs(table1) do
if value == v then
present = true
break
end
end
seen[v] = true
if not present then
table1[#table1 + 1] = v
end
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 plugin after entering vim ui
M.packer_lazy_load = function(plugin, timer)
if plugin then

View file

@ -1,39 +1,10 @@
-- IMPORTANT NOTE : This is the user config, can be edited. Will be preserved if updated with internal updater
-- This file is for NvChad options & tools, custom settings are split between here and 'lua/custom/init.lua'
local M = {}
M.options, M.ui, M.mappings, M.plugins = {}, {}, {}, {}
-- NOTE: To use this, make a copy with `cp example_chadrc.lua chadrc.lua`
-- make sure you maintain the structure of `core/default_config.lua` here,
-- example of changing theme:
--------------------------------------------------------------------
-- To use this file, copy the structure of `core/default_config.lua`,
-- examples of setting relative number & changing theme:
-- M.options = {
-- relativenumber = true,
-- }
-- M.ui = {
-- theme = "nord"
-- }
-- NvChad included plugin options & overrides
M.plugins = {
options = {
-- lspconfig = {
-- path of file containing setups of different lsps (ex : "custom.plugins.lspconfig"), read the docs for more info
-- setup_lspconf = "",
-- },
},
-- To change the Packer `config` of a plugin that comes with NvChad,
-- add a table entry below matching the plugin github name
-- '-' -> '_', remove any '.lua', '.nvim' extensions
-- this string will be called in a `require`
-- use "(custom.configs).my_func()" to call a function
-- use "custom.blankline" to call a file
default_plugin_config_replace = {},
M.ui = {
theme = "gruvchad",
}
return M