2022-05-29 12:37:47 +02:00
|
|
|
local M = {}
|
2022-06-05 15:41:26 +02:00
|
|
|
local api = vim.api
|
2021-08-22 09:49:15 +02:00
|
|
|
|
2022-05-29 11:38:53 +02:00
|
|
|
local merge_tb = vim.tbl_deep_extend
|
|
|
|
|
2022-06-24 15:54:59 +02:00
|
|
|
M.close_buffer = function(bufnr)
|
2022-05-01 04:31:47 +02:00
|
|
|
if vim.bo.buftype == "terminal" then
|
2022-06-28 16:39:02 +02:00
|
|
|
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)
|
2022-05-01 04:31:47 +02:00
|
|
|
end
|
2021-08-22 09:49:15 +02:00
|
|
|
end
|
|
|
|
|
2022-05-29 12:37:47 +02:00
|
|
|
M.load_config = function()
|
2022-05-29 11:38:53 +02:00
|
|
|
local config = require "core.default_config"
|
|
|
|
local chadrc_exists, chadrc = pcall(require, "custom.chadrc")
|
2022-05-23 09:24:03 +02:00
|
|
|
|
2022-04-10 13:06:05 +02:00
|
|
|
if chadrc_exists then
|
|
|
|
-- merge user config if it exists and is a table; otherwise display an error
|
2022-05-29 11:38:53 +02:00
|
|
|
if type(chadrc) == "table" then
|
2022-05-29 12:37:47 +02:00
|
|
|
M.remove_default_keys()
|
2022-05-29 11:38:53 +02:00
|
|
|
config = merge_tb("force", config, chadrc)
|
2022-04-10 13:06:05 +02:00
|
|
|
else
|
2022-05-29 11:38:53 +02:00
|
|
|
error "chadrc must return a table!"
|
2022-04-10 13:06:05 +02:00
|
|
|
end
|
2021-11-17 06:30:57 +01:00
|
|
|
end
|
2021-08-25 07:28:56 +02:00
|
|
|
|
2022-05-29 11:38:53 +02:00
|
|
|
config.mappings.disabled = nil
|
|
|
|
return config
|
2021-08-25 07:28:56 +02:00
|
|
|
end
|
|
|
|
|
2022-05-29 12:37:47 +02:00
|
|
|
M.remove_default_keys = function()
|
2022-05-29 11:38:53 +02:00
|
|
|
local chadrc = require "custom.chadrc"
|
|
|
|
local user_mappings = chadrc.mappings or {}
|
|
|
|
local user_keys = {}
|
|
|
|
local user_sections = vim.tbl_keys(user_mappings)
|
2022-05-23 09:24:03 +02:00
|
|
|
|
2022-05-29 11:38:53 +02:00
|
|
|
-- 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-05-14 01:31:13 +02:00
|
|
|
end
|
|
|
|
|
2022-05-29 11:38:53 +02:00
|
|
|
local function disable_key(mode, keybind, mode_mapping)
|
|
|
|
local keys_in_mode = vim.tbl_keys(user_keys[mode] or {})
|
2022-05-23 09:24:03 +02:00
|
|
|
|
2022-05-29 11:38:53 +02:00
|
|
|
if vim.tbl_contains(keys_in_mode, keybind) then
|
|
|
|
mode_mapping[keybind] = nil
|
2022-05-14 14:56:55 +02:00
|
|
|
end
|
|
|
|
end
|
2022-05-23 09:24:03 +02:00
|
|
|
|
2022-05-29 11:38:53 +02:00
|
|
|
local default_mappings = require("core.default_config").mappings
|
2022-05-14 14:56:55 +02:00
|
|
|
|
2022-05-29 11:38:53 +02:00
|
|
|
-- remove user_maps from default mapping table
|
|
|
|
for _, section_mappings in pairs(default_mappings) do
|
|
|
|
for mode, mode_mapping in pairs(section_mappings) do
|
|
|
|
for keybind, _ in pairs(mode_mapping) do
|
|
|
|
disable_key(mode, keybind, mode_mapping)
|
2022-05-14 01:31:13 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-29 12:37:47 +02:00
|
|
|
M.load_mappings = function(mappings, mapping_opt)
|
2022-05-31 21:19:23 +02:00
|
|
|
-- set mapping function with/without whichkey
|
2022-07-12 18:24:25 +02:00
|
|
|
local set_maps
|
2022-05-29 11:38:53 +02:00
|
|
|
local whichkey_exists, wk = pcall(require, "which-key")
|
2021-08-25 07:28:56 +02:00
|
|
|
|
2022-05-29 11:38:53 +02:00
|
|
|
if whichkey_exists then
|
2022-07-12 18:24:25 +02:00
|
|
|
set_maps = function(keybind, mapping_info, opts)
|
2022-05-29 11:38:53 +02:00
|
|
|
wk.register({ [keybind] = mapping_info }, opts)
|
|
|
|
end
|
|
|
|
else
|
2022-07-12 18:24:25 +02:00
|
|
|
set_maps = function(keybind, mapping_info, opts)
|
2022-05-29 11:38:53 +02:00
|
|
|
local mode = opts.mode
|
|
|
|
opts.mode = nil
|
|
|
|
vim.keymap.set(mode, keybind, mapping_info[1], opts)
|
2021-08-25 07:28:56 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-31 21:19:23 +02:00
|
|
|
mappings = mappings or vim.deepcopy(M.load_config().mappings)
|
|
|
|
mappings.lspconfig = nil
|
2021-08-25 07:28:56 +02:00
|
|
|
|
2022-07-12 18:24:25 +02:00
|
|
|
for _, section in pairs(mappings) do
|
|
|
|
for mode, mode_values in pairs(section) do
|
|
|
|
for keybind, mapping_info in pairs(mode_values) do
|
2022-05-31 21:19:23 +02:00
|
|
|
-- 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 {})
|
2022-07-12 18:34:22 +02:00
|
|
|
|
|
|
|
if mapping_info.opts then
|
|
|
|
mapping_info.opts = nil
|
|
|
|
end
|
2022-05-25 01:16:08 +02:00
|
|
|
|
2022-07-12 18:24:25 +02:00
|
|
|
set_maps(keybind, mapping_info, opts)
|
2022-05-12 14:56:01 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-29 07:04:44 +02:00
|
|
|
-- remove plugins defined in chadrc
|
2022-05-29 12:37:47 +02:00
|
|
|
M.remove_default_plugins = function(plugins)
|
|
|
|
local removals = M.load_config().plugins.remove or {}
|
2022-05-12 15:57:11 +02:00
|
|
|
|
2022-04-29 07:04:44 +02:00
|
|
|
if not vim.tbl_isempty(removals) then
|
2022-05-01 04:31:47 +02:00
|
|
|
for _, plugin in pairs(removals) do
|
|
|
|
plugins[plugin] = nil
|
|
|
|
end
|
2022-04-29 07:04:44 +02:00
|
|
|
end
|
2022-05-12 15:57:11 +02:00
|
|
|
|
2022-04-29 07:04:44 +02:00
|
|
|
return plugins
|
|
|
|
end
|
|
|
|
|
2022-04-27 17:42:28 +02:00
|
|
|
-- merge default/user plugin tables
|
2022-05-29 12:37:47 +02:00
|
|
|
M.merge_plugins = function(default_plugins)
|
|
|
|
local user_plugins = M.load_config().plugins.user
|
2021-11-13 18:37:20 +01:00
|
|
|
|
2022-04-27 17:42:28 +02:00
|
|
|
-- merge default + user plugin table
|
2022-05-29 11:38:53 +02:00
|
|
|
default_plugins = merge_tb("force", default_plugins, user_plugins)
|
2022-01-21 01:12:09 +01:00
|
|
|
|
2022-04-27 17:42:28 +02:00
|
|
|
local final_table = {}
|
2022-01-21 01:12:09 +01:00
|
|
|
|
2022-04-27 17:42:28 +02:00
|
|
|
for key, _ in pairs(default_plugins) do
|
|
|
|
default_plugins[key][1] = key
|
|
|
|
final_table[#final_table + 1] = default_plugins[key]
|
2022-01-31 02:28:19 +01:00
|
|
|
end
|
2022-04-27 17:42:28 +02:00
|
|
|
|
|
|
|
return final_table
|
2022-02-13 08:39:07 +01:00
|
|
|
end
|
2022-01-31 09:43:51 +01:00
|
|
|
|
2022-06-28 07:41:11 +02:00
|
|
|
M.load_override = function(default_table, plugin_name)
|
|
|
|
local user_table = M.load_config().plugins.override[plugin_name] or {}
|
|
|
|
user_table = type(user_table) == "table" and user_table or user_table()
|
2022-06-29 06:47:28 +02:00
|
|
|
return merge_tb("force", default_table, user_table)
|
2022-01-31 02:28:19 +01:00
|
|
|
end
|
2022-05-29 12:37:47 +02:00
|
|
|
|
2022-06-20 01:26:13 +02:00
|
|
|
M.packer_sync = function(...)
|
|
|
|
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" },
|
2022-06-24 15:54:59 +02:00
|
|
|
{
|
|
|
|
" on a NvChadSnapshot. This will cause issues if NvChad dependencies contain "
|
2022-07-12 18:34:22 +02:00
|
|
|
.. "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",
|
2022-06-24 15:54:59 +02:00
|
|
|
"WarningMsg",
|
|
|
|
},
|
2022-06-20 01:26:13 +02:00
|
|
|
}, false, {})
|
|
|
|
|
2022-06-24 15:54:59 +02:00
|
|
|
local ans = vim.trim(string.lower(vim.fn.input "-> "))
|
2022-06-20 01:26:13 +02:00
|
|
|
|
|
|
|
if ans ~= "y" then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if packer_exists then
|
|
|
|
packer.sync(...)
|
|
|
|
else
|
2022-06-24 15:54:59 +02:00
|
|
|
error "Packer could not be loaded!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-26 02:54:52 +02:00
|
|
|
M.bufilter = function()
|
|
|
|
local bufs = vim.t.bufs
|
|
|
|
|
|
|
|
for i = #bufs, 1, -1 do
|
2022-06-27 16:41:20 +02:00
|
|
|
if not vim.api.nvim_buf_is_valid(bufs[i]) then
|
2022-06-26 02:54:52 +02:00
|
|
|
table.remove(bufs, i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return bufs
|
|
|
|
end
|
|
|
|
|
2022-06-24 15:54:59 +02:00
|
|
|
M.tabuflineNext = function()
|
2022-06-26 02:54:52 +02:00
|
|
|
local bufs = M.bufilter() or {}
|
2022-06-24 15:54:59 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
M.tabuflinePrev = function()
|
2022-06-26 02:54:52 +02:00
|
|
|
local bufs = M.bufilter() or {}
|
2022-06-24 15:54:59 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
end
|
2022-06-27 03:46:33 +02:00
|
|
|
|
2022-06-24 15:54:59 +02:00
|
|
|
-- closes tab + all of its buffers
|
2022-06-27 03:46:33 +02:00
|
|
|
M.closeAllBufs = function(action)
|
2022-06-28 16:39:02 +02:00
|
|
|
local bufs = vim.t.bufs
|
|
|
|
|
|
|
|
if action == "closeTab" then
|
|
|
|
vim.cmd "tabclose"
|
|
|
|
end
|
2022-06-24 15:54:59 +02:00
|
|
|
|
|
|
|
for _, buf in ipairs(bufs) do
|
2022-06-27 03:46:33 +02:00
|
|
|
M.close_buffer(buf)
|
2022-06-20 01:26:13 +02:00
|
|
|
end
|
2022-06-27 03:46:33 +02:00
|
|
|
|
2022-06-28 16:39:02 +02:00
|
|
|
if action ~= "closeTab" then
|
|
|
|
vim.cmd "enew"
|
|
|
|
end
|
2022-06-20 01:26:13 +02:00
|
|
|
end
|
|
|
|
|
2022-05-29 12:37:47 +02:00
|
|
|
return M
|