2022-05-29 10:37:47 +00:00
|
|
|
local M = {}
|
2022-06-05 13:41:26 +00:00
|
|
|
local api = vim.api
|
2021-08-22 07:49:15 +00:00
|
|
|
|
2022-05-29 09:38:53 +00:00
|
|
|
local merge_tb = vim.tbl_deep_extend
|
|
|
|
|
2022-06-24 13:54:59 +00:00
|
|
|
M.close_buffer = function(bufnr)
|
2022-07-22 16:00:00 +00:00
|
|
|
if vim.bo.buftype == "terminal" then
|
|
|
|
vim.cmd(vim.bo.buflisted and "set nobl | enew" or "hide")
|
|
|
|
else
|
|
|
|
bufnr = bufnr or api.nvim_get_current_buf()
|
|
|
|
require("core.utils").tabuflinePrev()
|
2022-08-28 03:44:09 +00:00
|
|
|
vim.cmd("confirm bd" .. bufnr)
|
2022-07-22 16:00:00 +00:00
|
|
|
end
|
2021-08-22 07:49:15 +00:00
|
|
|
end
|
|
|
|
|
2022-05-29 10:37:47 +00:00
|
|
|
M.load_config = function()
|
2022-07-22 16:00:00 +00:00
|
|
|
local config = require "core.default_config"
|
|
|
|
local chadrc_exists, chadrc = pcall(require, "custom.chadrc")
|
|
|
|
|
|
|
|
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-11 10:10:26 +00:00
|
|
|
config.mappings = M.remove_disabled_keys(chadrc.mappings, config.mappings)
|
|
|
|
config = merge_tb("force", config, chadrc) or {}
|
2022-07-22 16:00:00 +00:00
|
|
|
else
|
|
|
|
error "chadrc must return a table!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
config.mappings.disabled = nil
|
|
|
|
return config
|
2021-08-25 05:28:56 +00:00
|
|
|
end
|
|
|
|
|
2022-08-11 10:10:26 +00:00
|
|
|
M.remove_disabled_keys = function(chadrc_mappings, default_mappings)
|
|
|
|
if not chadrc_mappings then
|
|
|
|
return default_mappings
|
2022-07-22 16:00:00 +00:00
|
|
|
end
|
|
|
|
|
2022-08-11 10:10:26 +00:00
|
|
|
-- store keys in a array with true value to compare
|
|
|
|
local keys_to_disable = {}
|
|
|
|
for _, mappings in pairs(chadrc_mappings) do
|
|
|
|
for mode, section_keys in pairs(mappings) do
|
|
|
|
if not keys_to_disable[mode] then
|
|
|
|
keys_to_disable[mode] = {}
|
|
|
|
end
|
2022-08-12 12:27:32 +00:00
|
|
|
section_keys = (type(section_keys) == "table" and section_keys) or {}
|
2022-08-11 10:10:26 +00:00
|
|
|
for k, _ in pairs(section_keys) do
|
|
|
|
keys_to_disable[mode][k] = true
|
|
|
|
end
|
2022-07-22 16:00:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-08-11 10:10:26 +00:00
|
|
|
-- make a copy as we need to modify default_mappings
|
|
|
|
for section_name, section_mappings in pairs(default_mappings) do
|
|
|
|
for mode, mode_mappings in pairs(section_mappings) do
|
|
|
|
mode_mappings = (type(mode_mappings) == "table" and mode_mappings) or {}
|
|
|
|
for k, _ in pairs(mode_mappings) do
|
|
|
|
-- if key if found then remove from default_mappings
|
|
|
|
if keys_to_disable[mode] and keys_to_disable[mode][k] then
|
|
|
|
default_mappings[section_name][mode][k] = nil
|
|
|
|
end
|
2022-05-14 12:56:55 +00:00
|
|
|
end
|
2022-07-22 16:00:00 +00:00
|
|
|
end
|
|
|
|
end
|
2022-05-13 23:31:13 +00:00
|
|
|
|
2022-08-11 10:10:26 +00:00
|
|
|
return default_mappings
|
|
|
|
end
|
2022-07-22 16:00:00 +00:00
|
|
|
|
2022-08-11 10:10:26 +00:00
|
|
|
M.load_mappings = function(section, mapping_opt)
|
|
|
|
local function set_section_map(section_values)
|
|
|
|
if section_values.plugin then
|
|
|
|
return
|
2022-07-22 16:00:00 +00:00
|
|
|
end
|
2022-08-11 10:10:26 +00:00
|
|
|
section_values.plugin = nil
|
2022-07-22 16:00:00 +00:00
|
|
|
|
2022-08-11 10:10:26 +00:00
|
|
|
for mode, mode_values in pairs(section_values) do
|
|
|
|
local default_opts = merge_tb("force", { mode = mode }, mapping_opt or {})
|
|
|
|
for keybind, mapping_info in pairs(mode_values) do
|
|
|
|
-- merge default + user opts
|
|
|
|
local opts = merge_tb("force", default_opts, mapping_info.opts or {})
|
|
|
|
|
|
|
|
mapping_info.opts, opts.mode = nil, nil
|
|
|
|
opts.desc = mapping_info[2]
|
|
|
|
|
|
|
|
vim.keymap.set(mode, keybind, mapping_info[1], opts)
|
2022-05-12 12:56:01 +00:00
|
|
|
end
|
2022-07-22 16:00:00 +00:00
|
|
|
end
|
|
|
|
end
|
2022-08-04 14:32:16 +00:00
|
|
|
|
2022-08-11 10:10:26 +00:00
|
|
|
local mappings = require("core.utils").load_config().mappings
|
|
|
|
|
|
|
|
if type(section) == "string" then
|
|
|
|
mappings[section]["plugin"] = nil
|
|
|
|
mappings = { mappings[section] }
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, sect in pairs(mappings) do
|
|
|
|
set_section_map(sect)
|
|
|
|
end
|
2022-05-12 12:56:01 +00:00
|
|
|
end
|
|
|
|
|
2022-04-27 15:42:28 +00:00
|
|
|
-- merge default/user plugin tables
|
2022-09-12 02:23:07 +00:00
|
|
|
M.merge_plugins = function(plugins)
|
2022-09-08 09:15:23 +00:00
|
|
|
local plugin_configs = M.load_config().plugins
|
|
|
|
local user_plugins = plugin_configs
|
|
|
|
|
|
|
|
-- old plugin syntax for adding plugins
|
|
|
|
if plugin_configs.user and type(plugin_configs.user) == "table" then
|
|
|
|
user_plugins = plugin_configs.user
|
|
|
|
end
|
|
|
|
|
|
|
|
-- support old plugin removal syntax
|
|
|
|
local remove_plugins = plugin_configs.remove
|
|
|
|
if type(remove_plugins) == "table" then
|
|
|
|
for _, v in ipairs(remove_plugins) do
|
2022-09-12 02:23:07 +00:00
|
|
|
plugins[v] = nil
|
2022-09-08 09:15:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-09-12 02:23:07 +00:00
|
|
|
plugins = merge_tb("force", plugins, user_plugins)
|
2022-01-21 00:12:09 +00:00
|
|
|
|
2022-07-22 16:00:00 +00:00
|
|
|
local final_table = {}
|
2022-01-21 00:12:09 +00:00
|
|
|
|
2022-09-12 02:23:07 +00:00
|
|
|
for key, val in pairs(plugins) do
|
2022-09-08 09:15:23 +00:00
|
|
|
if val and type(val) == "table" then
|
2022-09-12 02:23:07 +00:00
|
|
|
plugins[key] = val.rm_default_opts and user_plugins[key] or plugins[key]
|
|
|
|
plugins[key][1] = key
|
|
|
|
final_table[#final_table + 1] = plugins[key]
|
2022-09-02 05:35:50 +00:00
|
|
|
end
|
2022-07-22 16:00:00 +00:00
|
|
|
end
|
2022-04-27 15:42:28 +00:00
|
|
|
|
2022-07-22 16:00:00 +00:00
|
|
|
return final_table
|
2022-02-13 07:39:07 +00:00
|
|
|
end
|
2022-01-31 08:43:51 +00:00
|
|
|
|
2022-09-02 05:35:50 +00:00
|
|
|
-- override plugin options table with custom ones
|
|
|
|
M.load_override = function(options_table, name)
|
2022-09-08 09:15:23 +00:00
|
|
|
local plugin_configs, plugin_options = M.load_config().plugins, nil
|
2022-09-02 05:35:50 +00:00
|
|
|
|
2022-09-08 09:15:23 +00:00
|
|
|
-- support old plugin syntax for override
|
2022-09-10 19:37:44 +00:00
|
|
|
local user_override = plugin_configs.override and plugin_configs.override[name]
|
2022-09-08 09:15:23 +00:00
|
|
|
if user_override and type(user_override) == "table" then
|
|
|
|
plugin_options = user_override
|
2022-09-02 05:35:50 +00:00
|
|
|
end
|
|
|
|
|
2022-09-08 09:15:23 +00:00
|
|
|
-- if no old style plugin override is found, then use the new syntax
|
|
|
|
if not plugin_options and plugin_configs[name] then
|
|
|
|
local override_options = plugin_configs[name].override_options or {}
|
|
|
|
if type(override_options) == "table" then
|
|
|
|
plugin_options = override_options
|
|
|
|
elseif type(override_options) == "function" then
|
|
|
|
plugin_options = override_options()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- make sure the plugin options are a table
|
|
|
|
plugin_options = type(plugin_options) == "table" and plugin_options or {}
|
|
|
|
|
2022-09-02 05:35:50 +00:00
|
|
|
return merge_tb("force", options_table, plugin_options)
|
2022-01-31 01:28:19 +00:00
|
|
|
end
|
2022-05-29 10:37:47 +00:00
|
|
|
|
2022-06-19 23:26:13 +00:00
|
|
|
M.packer_sync = function(...)
|
2022-07-22 16:00:00 +00: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-19 23:26:13 +00:00
|
|
|
end
|
2022-07-22 16:00:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if packer_exists then
|
|
|
|
packer.sync(...)
|
2022-09-08 09:15:23 +00:00
|
|
|
|
|
|
|
local plugins = M.load_config().plugins
|
|
|
|
local old_style_options = plugins.user or plugins.override or plugins.remove
|
|
|
|
if old_style_options then
|
2022-09-09 09:07:48 +00:00
|
|
|
vim.notify_once("NvChad: This plugin syntax is deprecated, use new style config.", "Error")
|
2022-09-08 09:15:23 +00:00
|
|
|
end
|
2022-07-22 16:00:00 +00:00
|
|
|
else
|
|
|
|
error "Packer could not be loaded!"
|
|
|
|
end
|
2022-06-24 13:54:59 +00:00
|
|
|
end
|
|
|
|
|
2022-06-26 00:54:52 +00:00
|
|
|
M.bufilter = function()
|
2022-08-17 15:43:12 +00:00
|
|
|
local bufs = vim.t.bufs or nil
|
|
|
|
|
|
|
|
if not bufs then
|
|
|
|
return {}
|
|
|
|
end
|
2022-06-26 00:54:52 +00:00
|
|
|
|
2022-07-22 16:00:00 +00: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 00:54:52 +00:00
|
|
|
|
2022-07-22 16:00:00 +00:00
|
|
|
return bufs
|
2022-06-26 00:54:52 +00:00
|
|
|
end
|
|
|
|
|
2022-06-24 13:54:59 +00:00
|
|
|
M.tabuflineNext = function()
|
2022-07-22 16:00:00 +00: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 13:54:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
M.tabuflinePrev = function()
|
2022-07-22 16:00:00 +00: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 13:54:59 +00:00
|
|
|
end
|
2022-06-27 01:46:33 +00:00
|
|
|
|
2022-06-24 13:54:59 +00:00
|
|
|
-- closes tab + all of its buffers
|
2022-06-27 01:46:33 +00:00
|
|
|
M.closeAllBufs = function(action)
|
2022-07-22 16:00:00 +00:00
|
|
|
local bufs = vim.t.bufs
|
2022-06-28 14:39:02 +00:00
|
|
|
|
2022-07-22 16:00:00 +00:00
|
|
|
if action == "closeTab" then
|
|
|
|
vim.cmd "tabclose"
|
|
|
|
end
|
2022-06-24 13:54:59 +00:00
|
|
|
|
2022-07-22 16:00:00 +00:00
|
|
|
for _, buf in ipairs(bufs) do
|
|
|
|
M.close_buffer(buf)
|
|
|
|
end
|
2022-06-27 01:46:33 +00:00
|
|
|
|
2022-07-22 16:00:00 +00:00
|
|
|
if action ~= "closeTab" then
|
|
|
|
vim.cmd "enew"
|
|
|
|
end
|
2022-06-19 23:26:13 +00:00
|
|
|
end
|
|
|
|
|
2022-05-29 10:37:47 +00:00
|
|
|
return M
|