You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
NvChad/lua/core/utils.lua

174 lines
5.0 KiB
Lua

_G.nvchad = {}
Restructure config | Move some to a packer plugin | Lot of cleanup * move teleacope files, updater and related utils to https://github.com/NvChad/core * restructure config file and directory structure * expose mappings for better escape * allow multiple mappings for some * improve merge table function for the same * move autocommands to a seperate file * rearrange everything alphabetically where sanely possible * rearrange packer plugin list on the basis of trigerred state config structure now . ├── init.lua ├── LICENSE ├── lua │ ├── chadrc.lua │ ├── colors │ │ ├── highlights.lua │ │ ├── init.lua │ │ └── themes │ │ ├── chadracula.lua │ │ ├── everforest.lua │ │ ├── gruvchad.lua │ │ ├── javacafe.lua │ │ ├── mountain.lua │ │ ├── norchad.lua │ │ ├── one-light.lua │ │ ├── onedark.lua │ │ ├── tokyonight.lua │ │ └── tomorrow-night.lua │ ├── core │ │ ├── autocmds.lua │ │ ├── init.lua │ │ ├── mappings.lua │ │ ├── options.lua │ │ └── utils.lua │ ├── default_config.lua │ └── plugins │ ├── configs │ │ ├── autopairs.lua │ │ ├── autosave.lua │ │ ├── bufferline.lua │ │ ├── chadsheet.lua │ │ ├── compe.lua │ │ ├── dashboard.lua │ │ ├── gitsigns.lua │ │ ├── icons.lua │ │ ├── lspconfig.lua │ │ ├── luasnip.lua │ │ ├── nvimtree.lua │ │ ├── others.lua │ │ ├── statusline.lua │ │ ├── telescope.lua │ │ ├── treesitter.lua │ │ └── zenmode.lua │ ├── init.lua │ └── packerInit.lua └── README.md
3 years ago
nvchad.close_buffer = function(force)
if vim.bo.buftype == "terminal" then
vim.api.nvim_win_hide(0)
return
end
local fileExists = vim.fn.filereadable(vim.fn.expand "%p")
local modified = vim.api.nvim_buf_get_option(vim.fn.bufnr(), "modified")
-- if file doesnt exist & its modified
if fileExists == 0 and modified then
print "no file name? add it now!"
return
end
force = force or not vim.bo.buflisted or vim.bo.buftype == "nofile"
-- if not force, change to prev buf and then close current
local close_cmd = force and ":bd!" or ":bp | bd" .. vim.fn.bufnr()
vim.cmd(close_cmd)
Restructure config | Move some to a packer plugin | Lot of cleanup * move teleacope files, updater and related utils to https://github.com/NvChad/core * restructure config file and directory structure * expose mappings for better escape * allow multiple mappings for some * improve merge table function for the same * move autocommands to a seperate file * rearrange everything alphabetically where sanely possible * rearrange packer plugin list on the basis of trigerred state config structure now . ├── init.lua ├── LICENSE ├── lua │ ├── chadrc.lua │ ├── colors │ │ ├── highlights.lua │ │ ├── init.lua │ │ └── themes │ │ ├── chadracula.lua │ │ ├── everforest.lua │ │ ├── gruvchad.lua │ │ ├── javacafe.lua │ │ ├── mountain.lua │ │ ├── norchad.lua │ │ ├── one-light.lua │ │ ├── onedark.lua │ │ ├── tokyonight.lua │ │ └── tomorrow-night.lua │ ├── core │ │ ├── autocmds.lua │ │ ├── init.lua │ │ ├── mappings.lua │ │ ├── options.lua │ │ └── utils.lua │ ├── default_config.lua │ └── plugins │ ├── configs │ │ ├── autopairs.lua │ │ ├── autosave.lua │ │ ├── bufferline.lua │ │ ├── chadsheet.lua │ │ ├── compe.lua │ │ ├── dashboard.lua │ │ ├── gitsigns.lua │ │ ├── icons.lua │ │ ├── lspconfig.lua │ │ ├── luasnip.lua │ │ ├── nvimtree.lua │ │ ├── others.lua │ │ ├── statusline.lua │ │ ├── telescope.lua │ │ ├── treesitter.lua │ │ └── zenmode.lua │ ├── init.lua │ └── packerInit.lua └── README.md
3 years ago
end
nvchad.load_config = function()
local conf = require "core.default_config"
-- attempt to load and merge a user config
local chadrc_exists = vim.fn.filereadable(vim.fn.stdpath "config" .. "/lua/custom/chadrc.lua") == 1
if chadrc_exists then
-- merge user config if it exists and is a table; otherwise display an error
local user_config = require "custom.chadrc"
if type(user_config) == "table" then
conf.mappings = nvchad.prune_key_map(conf.mappings, user_config.mappings, { "mode_opts" })
conf = vim.tbl_deep_extend("force", conf, user_config)
else
error "User config (chadrc.lua) *must* return a table!"
end
3 years ago
end
return conf
end
-- reduces a given keymap to a table of modes each containing a list of key maps for that mode
nvchad.reduce_key_map = function(key_map, ignore_modes)
local prune_keys = {}
for _, modes in pairs(key_map) do
for mode, mappings in pairs(modes) do
if not vim.tbl_contains(ignore_modes, mode) then
prune_keys[mode] = prune_keys[mode] and prune_keys[mode] or {}
prune_keys[mode] = vim.list_extend(prune_keys[mode], vim.tbl_keys(mappings))
end
end
end
return prune_keys
end
-- prune keys from a key map table by matching against another key map table
nvchad.prune_key_map = function(key_map, prune_map, ignore_modes)
if not prune_map then return key_map end
if not key_map then return prune_map end
local prune_keys = nvchad.reduce_key_map(prune_map, ignore_modes)
for ext, modes in pairs(key_map) do
for mode, mappings in pairs(modes) do
if not vim.tbl_contains(ignore_modes, mode) then
if prune_keys[mode] then
-- filter mappings table so that only keys that are not in user_mappings are left
local filtered_mappings = {}
for k, v in pairs(mappings) do
if not vim.tbl_contains(prune_keys[mode], k) then
filtered_mappings[k] = v
end
end
key_map[ext][mode] = filtered_mappings
end
end
end
end
return key_map
end
nvchad.map = function(mode, keys, command, opt)
local options = { silent = true }
if opt then
options = vim.tbl_extend("force", options, opt)
end
if type(keys) == "table" then
for _, keymap in ipairs(keys) do
nvchad.map(mode, keymap, command, opt)
end
return
end
vim.keymap.set(mode, keys, command, opt)
end
-- For those who disabled whichkey
nvchad.no_WhichKey_map = function()
local mappings = nvchad.load_config().mappings
local ignore_modes = { "mode_opts" }
for _, value in pairs(mappings) do
for mode, keymap in pairs(value) do
if not vim.tbl_contains(ignore_modes, mode) then
for keybind, cmd in pairs(keymap) do
-- disabled keys will not have cmd set
if cmd ~= "" then
nvchad.map(mode, keybind, cmd[1])
end
end
end
end
end
require("plugins.configs.others").misc_mappings()
end
-- load plugin after entering vim ui
nvchad.packer_lazy_load = function(plugin, timer)
if plugin then
timer = timer or 0
vim.defer_fn(function()
require("packer").loader(plugin)
end, timer)
end
end
-- remove plugins defined in chadrc
nvchad.remove_default_plugins = function(plugins)
local removals = nvchad.load_config().plugins.remove or {}
2 years ago
if not vim.tbl_isempty(removals) then
for _, plugin in pairs(removals) do
plugins[plugin] = nil
end
end
2 years ago
return plugins
end
-- merge default/user plugin tables
nvchad.plugin_list = function(default_plugins)
local user_plugins = nvchad.load_config().plugins.user
-- merge default + user plugin table
default_plugins = vim.tbl_deep_extend("force", default_plugins, user_plugins)
local final_table = {}
for key, _ in pairs(default_plugins) do
default_plugins[key][1] = key
final_table[#final_table + 1] = default_plugins[key]
end
return final_table
end
nvchad.load_override = function(default_table, plugin_name)
local user_table = nvchad.load_config().plugins.override[plugin_name]
2 years ago
if type(user_table) == "table" then
default_table = vim.tbl_deep_extend("force", default_table, user_table)
else
default_table = default_table
end
2 years ago
return default_table
end