2022-05-29 11:37:47 +01:00
|
|
|
local M = {}
|
2022-06-05 14:41:26 +01:00
|
|
|
local api = vim.api
|
2021-08-22 08:49:15 +01:00
|
|
|
|
2022-05-29 10:38:53 +01:00
|
|
|
local merge_tb = vim.tbl_deep_extend
|
|
|
|
|
2022-06-24 14:54:59 +01:00
|
|
|
M.close_buffer = function(bufnr)
|
2022-07-22 17:00:00 +01:00
|
|
|
if vim.bo.buftype == "terminal" then
|
|
|
|
vim.cmd(vim.bo.buflisted and "set nobl | enew" or "hide")
|
|
|
|
elseif vim.bo.modified then
|
|
|
|
print "save the file bruh"
|
|
|
|
else
|
|
|
|
bufnr = bufnr or api.nvim_get_current_buf()
|
|
|
|
require("core.utils").tabuflinePrev()
|
|
|
|
vim.cmd("bd" .. bufnr)
|
|
|
|
end
|
2021-08-22 08:49:15 +01:00
|
|
|
end
|
|
|
|
|
2022-08-07 04:20:36 +01:00
|
|
|
-- store mapping section names which have plugin key = true
|
|
|
|
local lazyload_mappings_list = {}
|
|
|
|
|
|
|
|
local add_mappingSection_toList = function()
|
|
|
|
local default_mappings = require("core.default_config").mappings
|
|
|
|
|
|
|
|
-- store mapping section name into a table
|
|
|
|
for section_name, section_mappings in pairs(default_mappings) do
|
|
|
|
if section_mappings.plugin then
|
|
|
|
lazyload_mappings_list[section_name] = true
|
|
|
|
section_mappings.plugin = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-29 11:37:47 +01:00
|
|
|
M.load_config = function()
|
2022-07-22 17:00:00 +01:00
|
|
|
local config = require "core.default_config"
|
|
|
|
local chadrc_exists, chadrc = pcall(require, "custom.chadrc")
|
|
|
|
|
2022-08-07 04:20:36 +01:00
|
|
|
add_mappingSection_toList()
|
|
|
|
|
2022-07-22 17:00:00 +01:00
|
|
|
if chadrc_exists then
|
|
|
|
-- merge user config if it exists and is a table; otherwise display an error
|
|
|
|
if type(chadrc) == "table" then
|
2022-08-07 03:55:07 +01:00
|
|
|
M.remove_default_keys(chadrc.mappings or {})
|
|
|
|
config = merge_tb("force", config, chadrc)
|
2022-07-22 17:00:00 +01:00
|
|
|
else
|
|
|
|
error "chadrc must return a table!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
config.mappings.disabled = nil
|
|
|
|
return config
|
2021-08-25 06:28:56 +01:00
|
|
|
end
|
|
|
|
|
2022-08-07 03:55:07 +01:00
|
|
|
M.remove_default_keys = function(user_mappings)
|
|
|
|
local user_keys = {}
|
|
|
|
local user_sections = vim.tbl_keys(user_mappings)
|
|
|
|
|
|
|
|
-- push user_map keys in user_keys table
|
|
|
|
for _, section in ipairs(user_sections) do
|
|
|
|
user_keys = vim.tbl_deep_extend("force", user_keys, user_mappings[section])
|
2022-07-22 17:00:00 +01:00
|
|
|
end
|
|
|
|
|
2022-08-07 03:55:07 +01:00
|
|
|
local function disable_key(mode, keybind, mode_mapping)
|
|
|
|
if user_keys[mode] and user_keys[mode][keybind] then
|
|
|
|
mode_mapping[keybind] = nil
|
2022-07-22 17:00:00 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-08-07 03:55:07 +01:00
|
|
|
local default_mappings = require("core.default_config").mappings
|
|
|
|
|
|
|
|
-- remove user_maps from default mapping table
|
2022-08-07 04:20:36 +01:00
|
|
|
for _, section_mappings in pairs(default_mappings) do
|
2022-07-22 17:00:00 +01:00
|
|
|
for mode, mode_mapping in pairs(section_mappings) do
|
2022-08-07 03:55:07 +01:00
|
|
|
for keybind, _ in pairs(mode_mapping) do
|
|
|
|
disable_key(mode, keybind, mode_mapping)
|
2022-05-14 13:56:55 +01:00
|
|
|
end
|
2022-07-22 17:00:00 +01:00
|
|
|
end
|
|
|
|
end
|
2022-05-14 00:31:13 +01:00
|
|
|
end
|
|
|
|
|
2022-08-07 03:55:07 +01:00
|
|
|
M.load_mappings = function(mappings, mapping_opt)
|
2022-07-22 17:00:00 +01:00
|
|
|
-- set mapping function with/without whichkey
|
|
|
|
local set_maps
|
|
|
|
local whichkey_exists, wk = pcall(require, "which-key")
|
|
|
|
|
|
|
|
if whichkey_exists then
|
|
|
|
set_maps = function(keybind, mapping_info, opts)
|
|
|
|
wk.register({ [keybind] = mapping_info }, opts)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
set_maps = function(keybind, mapping_info, opts)
|
|
|
|
local mode = opts.mode
|
|
|
|
opts.mode = nil
|
|
|
|
vim.keymap.set(mode, keybind, mapping_info[1], opts)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-08-07 03:55:07 +01:00
|
|
|
local mappings_tb = M.load_config().mappings
|
|
|
|
mappings = vim.deepcopy(type(mappings) == "string" and { mappings_tb[mappings] } or mappings_tb)
|
|
|
|
|
|
|
|
local function set_mappings()
|
|
|
|
for name, section in pairs(mappings) do
|
|
|
|
-- skip mapping section with plugin=true
|
|
|
|
if not lazyload_mappings_list[name] then
|
|
|
|
for mode, mode_values in pairs(section) do
|
|
|
|
for keybind, mapping_info in pairs(mode_values) do
|
|
|
|
-- merge default + user opts
|
|
|
|
local default_opts = merge_tb("force", { mode = mode }, mapping_opt or {})
|
|
|
|
local opts = merge_tb("force", default_opts, mapping_info.opts or {})
|
|
|
|
|
|
|
|
if mapping_info.opts then
|
|
|
|
mapping_info.opts = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
set_maps(keybind, mapping_info, opts)
|
|
|
|
end
|
2022-07-22 17:00:00 +01:00
|
|
|
end
|
2022-05-12 13:56:01 +01:00
|
|
|
end
|
2022-07-22 17:00:00 +01:00
|
|
|
end
|
|
|
|
end
|
2022-08-04 15:32:16 +01:00
|
|
|
|
2022-08-07 03:55:07 +01:00
|
|
|
vim.defer_fn(function()
|
|
|
|
set_mappings()
|
|
|
|
end, 0)
|
2022-05-12 13:56:01 +01:00
|
|
|
end
|
|
|
|
|
2022-04-29 06:04:44 +01:00
|
|
|
-- remove plugins defined in chadrc
|
2022-05-29 11:37:47 +01:00
|
|
|
M.remove_default_plugins = function(plugins)
|
2022-07-22 17:00:00 +01:00
|
|
|
local removals = M.load_config().plugins.remove or {}
|
2022-05-12 14:57:11 +01:00
|
|
|
|
2022-07-22 17:00:00 +01:00
|
|
|
if not vim.tbl_isempty(removals) then
|
|
|
|
for _, plugin in pairs(removals) do
|
|
|
|
plugins[plugin] = nil
|
|
|
|
end
|
|
|
|
end
|
2022-05-12 14:57:11 +01:00
|
|
|
|
2022-07-22 17:00:00 +01:00
|
|
|
return plugins
|
2022-04-29 06:04:44 +01:00
|
|
|
end
|
|
|
|
|
2022-04-27 16:42:28 +01:00
|
|
|
-- merge default/user plugin tables
|
2022-05-29 11:37:47 +01:00
|
|
|
M.merge_plugins = function(default_plugins)
|
2022-07-22 17:00:00 +01:00
|
|
|
local user_plugins = M.load_config().plugins.user
|
2021-11-13 17:37:20 +00:00
|
|
|
|
2022-07-22 17:00:00 +01:00
|
|
|
-- merge default + user plugin table
|
2022-08-07 03:55:07 +01:00
|
|
|
default_plugins = merge_tb("force", default_plugins, user_plugins)
|
2022-01-21 00:12:09 +00:00
|
|
|
|
2022-07-22 17:00:00 +01:00
|
|
|
local final_table = {}
|
2022-01-21 00:12:09 +00:00
|
|
|
|
2022-07-22 17:00:00 +01:00
|
|
|
for key, _ in pairs(default_plugins) do
|
|
|
|
default_plugins[key][1] = key
|
|
|
|
final_table[#final_table + 1] = default_plugins[key]
|
|
|
|
end
|
2022-04-27 16:42:28 +01:00
|
|
|
|
2022-07-22 17:00:00 +01:00
|
|
|
return final_table
|
2022-02-13 07:39:07 +00:00
|
|
|
end
|
2022-01-31 08:43:51 +00:00
|
|
|
|
2022-06-28 06:41:11 +01:00
|
|
|
M.load_override = function(default_table, plugin_name)
|
2022-07-22 17:00:00 +01:00
|
|
|
local user_table = M.load_config().plugins.override[plugin_name] or {}
|
2022-08-07 03:55:07 +01:00
|
|
|
user_table = type(user_table) == "table" and user_table or user_table()
|
|
|
|
return merge_tb("force", default_table, user_table)
|
2022-01-31 01:28:19 +00:00
|
|
|
end
|
2022-05-29 11:37:47 +01:00
|
|
|
|
2022-06-20 00:26:13 +01:00
|
|
|
M.packer_sync = function(...)
|
2022-07-22 17:00:00 +01:00
|
|
|
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
|
2022-06-20 00:26:13 +01:00
|
|
|
end
|
2022-07-22 17:00:00 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if packer_exists then
|
|
|
|
packer.sync(...)
|
|
|
|
else
|
|
|
|
error "Packer could not be loaded!"
|
|
|
|
end
|
2022-06-24 14:54:59 +01:00
|
|
|
end
|
|
|
|
|
2022-06-26 01:54:52 +01:00
|
|
|
M.bufilter = function()
|
2022-07-22 17:00:00 +01:00
|
|
|
local bufs = vim.t.bufs
|
2022-06-26 01:54:52 +01:00
|
|
|
|
2022-07-22 17:00:00 +01:00
|
|
|
for i = #bufs, 1, -1 do
|
|
|
|
if not vim.api.nvim_buf_is_valid(bufs[i]) then
|
|
|
|
table.remove(bufs, i)
|
|
|
|
end
|
|
|
|
end
|
2022-06-26 01:54:52 +01:00
|
|
|
|
2022-07-22 17:00:00 +01:00
|
|
|
return bufs
|
2022-06-26 01:54:52 +01:00
|
|
|
end
|
|
|
|
|
2022-06-24 14:54:59 +01:00
|
|
|
M.tabuflineNext = function()
|
2022-07-22 17:00:00 +01:00
|
|
|
local bufs = M.bufilter() or {}
|
|
|
|
|
|
|
|
for i, v in ipairs(bufs) do
|
|
|
|
if api.nvim_get_current_buf() == v then
|
|
|
|
vim.cmd(i == #bufs and "b" .. bufs[1] or "b" .. bufs[i + 1])
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2022-06-24 14:54:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
M.tabuflinePrev = function()
|
2022-07-22 17:00:00 +01:00
|
|
|
local bufs = M.bufilter() or {}
|
|
|
|
|
|
|
|
for i, v in ipairs(bufs) do
|
|
|
|
if api.nvim_get_current_buf() == v then
|
|
|
|
vim.cmd(i == 1 and "b" .. bufs[#bufs] or "b" .. bufs[i - 1])
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2022-06-24 14:54:59 +01:00
|
|
|
end
|
2022-06-27 02:46:33 +01:00
|
|
|
|
2022-06-24 14:54:59 +01:00
|
|
|
-- closes tab + all of its buffers
|
2022-06-27 02:46:33 +01:00
|
|
|
M.closeAllBufs = function(action)
|
2022-07-22 17:00:00 +01:00
|
|
|
local bufs = vim.t.bufs
|
2022-06-28 15:39:02 +01:00
|
|
|
|
2022-07-22 17:00:00 +01:00
|
|
|
if action == "closeTab" then
|
|
|
|
vim.cmd "tabclose"
|
|
|
|
end
|
2022-06-24 14:54:59 +01:00
|
|
|
|
2022-07-22 17:00:00 +01:00
|
|
|
for _, buf in ipairs(bufs) do
|
|
|
|
M.close_buffer(buf)
|
|
|
|
end
|
2022-06-27 02:46:33 +01:00
|
|
|
|
2022-07-22 17:00:00 +01:00
|
|
|
if action ~= "closeTab" then
|
|
|
|
vim.cmd "enew"
|
|
|
|
end
|
2022-06-20 00:26:13 +01:00
|
|
|
end
|
|
|
|
|
2022-05-29 11:37:47 +01:00
|
|
|
return M
|