diff --git a/examples/init.lua b/examples/init.lua index 4106717..809432d 100644 --- a/examples/init.lua +++ b/examples/init.lua @@ -1,9 +1,3 @@ -- example file i.e lua/custom/init.lua --- MAPPINGS -local map = nvchad.map - -map("n", "cc", ":Telescope ") -map("n", "q", ":q ") - --- require("my autocmds file") or just declare them here +-- load your globals, autocmds here or anything .__. diff --git a/init.lua b/init.lua index 0bdd69a..be2faf0 100644 --- a/init.lua +++ b/init.lua @@ -4,15 +4,16 @@ if present then impatient.enable_profile() end -local core_modules = { - "core.utils", - "core.options", - "core.autocmds", - "core.mappings", +local modules = { + "utils", + "options", + "autocmds", + "commands", } -for _, module in ipairs(core_modules) do - local ok, err = pcall(require, module) +for _, module in ipairs(modules) do + local ok, err = pcall(require, "core." .. module) + if not ok then error("Error loading " .. module .. "\n\n" .. err) end diff --git a/lua/core/autocmds.lua b/lua/core/autocmds.lua index 67be8b7..409abbc 100644 --- a/lua/core/autocmds.lua +++ b/lua/core/autocmds.lua @@ -15,7 +15,7 @@ autocmd("BufUnload", { end, }) --- Uncomment this if you want to open nvim with a dir +-- open nvim with a dir while still lazy loading nvimtree -- autocmd("BufEnter", { -- callback = function() -- if vim.api.nvim_buf_get_option(0, "buftype") ~= "terminal" then diff --git a/lua/core/commands.lua b/lua/core/commands.lua new file mode 100644 index 0000000..9c12009 --- /dev/null +++ b/lua/core/commands.lua @@ -0,0 +1,37 @@ +local user_cmd = vim.api.nvim_create_user_command +local cmd = vim.cmd + +-- snapshot stuff +user_cmd("PackerSnapshot", function(info) + require "plugins" + require("packer").snapshot(info.args) +end, { nargs = "+" }) + +user_cmd("PackerSnapshotDelete", function(info) + require "plugins" + require("packer.snapshot").delete(info.args) +end, { nargs = "+" }) + +user_cmd("PackerSnapshotRollback", function(info) + require "plugins" + require("packer").rollback(info.args) +end, { nargs = "+" }) + +-- Add Packer commands because we are not loading it at startup + +local packer_cmd = function(callback) + return function() + require "plugins" + require("packer")[callback]() + end +end + +user_cmd("PackerClean", packer_cmd "clean", {}) +user_cmd("PackerCompile", packer_cmd "compile", {}) +user_cmd("PackerInstall", packer_cmd "install", {}) +user_cmd("PackerStatus", packer_cmd "status", {}) +user_cmd("PackerSync", packer_cmd "sync", {}) +user_cmd("PackerUpdate", packer_cmd "update", {}) + +-- add NvChadUpdate command and mapping +cmd "silent! command! NvChadUpdate lua require('nvchad').update_nvchad()" diff --git a/lua/core/default_config.lua b/lua/core/default_config.lua index 21ebfb8..e9a4804 100644 --- a/lua/core/default_config.lua +++ b/lua/core/default_config.lua @@ -31,10 +31,6 @@ M.plugins = { remove = {}, options = { - packer = { - init_file = "plugins.packerInit", - snapshot = nil, - }, lspconfig = { setup_lspconf = "", -- path of lspconfig file }, @@ -50,9 +46,7 @@ M.plugins = { user = {}, } --- non plugin only -M.mappings = { - misc = function() end, -} +-- check core.mappings for table structure +M.mappings = require "core.mappings" return M diff --git a/lua/core/mappings.lua b/lua/core/mappings.lua index 18273d6..b849c1b 100644 --- a/lua/core/mappings.lua +++ b/lua/core/mappings.lua @@ -1,201 +1,288 @@ -local map = nvchad.map -local cmd = vim.cmd -local user_cmd = vim.api.nvim_create_user_command - --- This is a wrapper function made to disable a plugin mapping from chadrc --- If keys are nil, false or empty string, then the mapping will be not applied --- Useful when one wants to use that keymap for any other purpose - --- Don't copy the replaced text after pasting in visual mode --- https://vim.fandom.com/wiki/Replace_a_word_with_yanked_text#Alternative_mapping_for_paste -map("v", "p", 'p:let @+=@0:let @"=@0', { silent = true }) - --- Allow moving the cursor through wrapped lines with j, k, and --- http ://www.reddit.com/r/vim/comments/2k4cbr/problem_with_gj_and_gk/ --- empty mode is same as using :map --- also don't use g[j|k] when in operator pending mode, so it doesn't alter d, y or c behaviour - -map({ "n", "x", "o" }, "j", 'v:count || mode(1)[0:1] == "no" ? "j" : "gj"', { expr = true }) -map({ "n", "x", "o" }, "k", 'v:count || mode(1)[0:1] == "no" ? "k" : "gk"', { expr = true }) -map("", "", 'v:count || mode(1)[0:1] == "no" ? "j" : "gj"', { expr = true }) -map("", "", 'v:count || mode(1)[0:1] == "no" ? "k" : "gk"', { expr = true }) - --- use ESC to turn off search highlighting -map("n", "", " :noh ") - --- move cursor within insert mode -map("i", "", "") -map("i", "", "") -map("i", "", "") -map("i", "", "") -map("i", "", "") -map("i", "", "^i") - --- navigation between windows -map("n", "", "h") -map("n", "", "l") -map("n", "", "k") -map("n", "", "j") - -map("n", "x", function() - nvchad.close_buffer() -end) - -map("n", "", " :%y+ ") -- copy whole file content -map("n", "", " :enew ") -- new buffer -map("n", "b", " :tabnew ") -- new tabs -map("n", "n", " :set nu! ") -map("n", "rn", " :set rnu! ") -- relative line numbers -map("n", "", " :w ") -- ctrl + s to save file - --- terminal mappings - --- get out of terminal mode -map("t", { "jk" }, "") - --- Add Packer commands because we are not loading it at startup - -local packer_cmd = function(callback) - return function() - require "plugins" - require("packer")[callback]() - end -end - --- snapshot stuff -user_cmd("PackerSnapshot", function(info) - require "plugins" - require("packer").snapshot(info.args) -end, { nargs = "+" }) - -user_cmd("PackerSnapshotDelete", function(info) - require "plugins" - require("packer.snapshot").delete(info.args) -end, { nargs = "+" }) - -user_cmd("PackerSnapshotRollback", function(info) - require "plugins" - require("packer").rollback(info.args) -end, { nargs = "+" }) - -user_cmd("PackerClean", packer_cmd "clean", {}) -user_cmd("PackerCompile", packer_cmd "compile", {}) -user_cmd("PackerInstall", packer_cmd "install", {}) -user_cmd("PackerStatus", packer_cmd "status", {}) -user_cmd("PackerSync", packer_cmd "sync", {}) -user_cmd("PackerUpdate", packer_cmd "update", {}) - --- add NvChadUpdate command and mapping -cmd "silent! command! NvChadUpdate lua require('nvchad').update_nvchad()" -map("n", "uu", " :NvChadUpdate ") - --- load overriden misc mappings -nvchad.load_config().mappings.misc() +-- n, v, i are mode names local M = {} --- below are all plugin related mappings +M.general = { -M.bufferline = function() - map("n", "", " :BufferLineCycleNext ") - map("n", "", " :BufferLineCyclePrev ") -end + i = { -M.comment = function() - map("n", "/", " :lua require('Comment.api').toggle_current_linewise()") - map("v", "/", " :lua require('Comment.api').toggle_linewise_op(vim.fn.visualmode())") -end + -- go to beginning and end + [""] = { "^i", "論 beginning of line" }, + [""] = { "", "壟 end of line" }, -M.lspconfig = function() + -- navigate within insert mode + [""] = { "", " move left" }, + [""] = { "", " move right" }, + [""] = { "", " move down" }, + [""] = { "", " move up" }, + }, + + n = { + + -- switch between windows + [""] = { "h", " window left" }, + [""] = { "l", " window right" }, + [""] = { "j", " window down" }, + [""] = { "k", " window up" }, + + -- save + [""] = { " w ", "﬚ save file" }, + + -- Copy all + [""] = { " %y+ ", " copy whole file" }, + + -- line numbers + ["n"] = { " set nu! ", " toggle line number" }, + ["rn"] = { " set rnu! ", " toggle relative number" }, + + -- update nvchad + ["uu"] = { " :NvChadUpdate ", " update nvchad" }, + + ["tt"] = { + function() + require("base46").toggle_theme() + end, + + " toggle theme", + }, + }, +} + +M.bufferline = { + + n = { + -- new buffer + ["n"] = { " enew ", "烙 new buffer" }, + + -- cycle through buffers + [""] = { " BufferLineCycleNext ", " cycle next buffer" }, + [""] = { " BufferLineCyclePrev ", " cycle prev buffer" }, + + -- close buffer + hide terminal buffer + ["x"] = { + function() + nvchad.close_buffer() + end, + " close buffer", + }, + }, +} + +M.comment = { + + -- toggle comment in both modes + n = { + ["/"] = { " lua require('Comment.api').toggle_current_linewise()", "蘒 toggle comment" }, + }, + + v = { + ["/"] = { + " lua require('Comment.api').toggle_current_linewise_op(vim.fn.visualmode())", + "蘒 toggle comment", + }, + }, +} + +M.lspconfig = { -- See ` :help vim.lsp.*` for documentation on any of the below functions - map("n", "gD", function() - vim.lsp.buf.declaration() - end) - map("n", "gd", function() - vim.lsp.buf.definition() - end) + n = { + ["gD"] = { + function() + vim.lsp.buf.declaration() + end, + " lsp declaration", + }, - map("n", "K", function() - vim.lsp.buf.hover() - end) + ["gd"] = { + function() + vim.lsp.buf.definition() + end, + " lsp definition", + }, - map("n", "gi", function() - vim.lsp.buf.implementation() - end) + ["K"] = { + function() + vim.lsp.buf.hover() + end, + " lsp hover", + }, - map("n", "", function() - vim.lsp.buf.signature_help() - end) + ["gi"] = { + function() + vim.lsp.buf.implementation() + end, + " lsp implementation", + }, - map("n", "D", function() - vim.lsp.buf.type_definition() - end) + [""] = { + function() + vim.lsp.buf.signature_help() + end, + " lsp signature_help", + }, - map("n", "ra", function() - vim.lsp.buf.rename() - end) + ["D"] = { + function() + vim.lsp.buf.type_definition() + end, + " lsp definition type", + }, - map("n", "ca", function() - vim.lsp.buf.code_action() - end) + ["ra"] = { + function() + vim.lsp.buf.rename() + end, + " lsp rename", + }, - map("n", "gr", function() - vim.lsp.buf.references() - end) + ["ca"] = { + function() + vim.lsp.buf.code_action() + end, + " lsp code_action", + }, - map("n", "f", function() - vim.diagnostic.open_float() - end) + ["gr"] = { + function() + vim.lsp.buf.references() + end, + " lsp references", + }, - map("n", "[d", function() - vim.diagnostic.goto_prev() - end) + ["f"] = { + function() + vim.diagnostic.open_float() + end, + " floating diagnostic", + }, - map("n", "d]", function() - vim.diagnostic.goto_next() - end) + ["[d"] = { + function() + vim.diagnostic.goto_prev() + end, + " goto prev", + }, - map("n", "q", function() - vim.diagnostic.setloclist() - end) + ["d]"] = { + function() + vim.diagnostic.goto_next() + end, + " goto_next", + }, - map("n", "fm", function() - vim.lsp.buf.formatting() - end) + ["q"] = { + function() + vim.diagnostic.setloclist() + end, + " diagnostic setloclist", + }, - map("n", "wa", function() - vim.lsp.buf.add_workspace_folder() - end) + ["fm"] = { + function() + vim.lsp.buf.formatting() + end, + " lsp formatting", + }, - map("n", "wr", function() - vim.lsp.buf.remove_workspace_folder() - end) + ["wa"] = { + function() + vim.lsp.buf.add_workspace_folder() + end, + " add workspace folder", + }, - map("n", "wl", function() - print(vim.inspect(vim.lsp.buf.list_workspace_folders())) - end) -end + ["wr"] = { + function() + vim.lsp.buf.remove_workspace_folder() + end, + " remove workspace folder", + }, -M.nvimtree = function() - map("n", "", " :NvimTreeToggle ") - map("n", "e", " :NvimTreeFocus ") -end + ["wl"] = { + function() + print(vim.inspect(vim.lsp.buf.list_workspace_folders())) + end, + " list workspace folders", + }, + }, +} -M.telescope = function() - map("n", "fb", " :Telescope buffers ") - map("n", "ff", " :Telescope find_files ") - map("n", "fa", " :Telescope find_files follow=true no_ignore=true hidden=true ") - map("n", "cm", " :Telescope git_commits ") - map("n", "gt", " :Telescope git_status ") - map("n", "fh", " :Telescope help_tags ") - map("n", "fw", " :Telescope live_grep ") - map("n", "fo", " :Telescope oldfiles ") - map("n", "th", " :Telescope themes ") - map("n", "tk", " :Telescope keymaps ") +M.nvimtree = { - -- pick a hidden term - map("n", "W", " :Telescope terms ") -end + n = { + -- toggle + [""] = { " NvimTreeToggle ", " toggle nvimtree" }, + + -- focus + ["e"] = { " NvimTreeFocus ", " focus nvimtree" }, + }, +} + +M.telescope = { + n = { + -- find + ["ff"] = { " Telescope find_files ", " find files" }, + ["fa"] = { " Telescope find_files follow=true no_ignore=true hidden=true ", " find all" }, + ["fw"] = { " Telescope live_grep ", " live grep" }, + ["fb"] = { " Telescope buffers ", " find buffers" }, + ["fh"] = { " Telescope help_tags ", " help page" }, + ["fo"] = { " Telescope oldfiles ", " find oldfiles" }, + ["tk"] = { " Telescope keys ", " show keys" }, + + -- git + ["cm"] = { " Telescope git_commits ", " git commits" }, + ["gt"] = { " Telescope git_status ", " git status" }, + + -- pick a hidden term + ["pt"] = { " Telescope terms ", " pick hidden term" }, + + -- theme switcher + ["th"] = { " Telescope themes ", " nvchad themes" }, + }, +} + +M.nvterm = { + + n = { + -- toggle + [""] = { + function() + require("nvterm.terminal").toggle "float" + end, + " toggle floating term", + }, + + [""] = { + function() + require("nvterm.terminal").toggle "horizontal" + end, + " toggle horizontal term", + }, + + [""] = { + function() + require("nvterm.terminal").toggle "vertical" + end, + " toggle vertical term", + }, + + -- new + + ["h"] = { + function() + require("nvterm.terminal").new "horizontal" + end, + " new horizontal term", + }, + + ["v"] = { + function() + require("nvterm.terminal").new "vertical" + end, + " new vertical term", + }, + }, +} return M diff --git a/lua/core/utils.lua b/lua/core/utils.lua index abd51ea..077970c 100644 --- a/lua/core/utils.lua +++ b/lua/core/utils.lua @@ -57,6 +57,25 @@ nvchad.map = function(mode, keys, command, opt) vim.keymap.set(mode, keys, command, opt) end +-- For those who disabled whichkey + +nvchad.no_WhichKey_map = function() + local mappings = nvchad.load_config().mappings + + for key, _ in pairs(mappings) do + for mode, _ in pairs(mappings[key]) do + for keybind, cmd in pairs(mappings[key][mode]) do + -- disabled keys will not have cmd set + if cmd ~= "" then + nvchad.map(mode, keybind, cmd[1]) + 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 diff --git a/lua/plugins/configs/bufferline.lua b/lua/plugins/configs/bufferline.lua index 0d3a6f1..4292d3b 100644 --- a/lua/plugins/configs/bufferline.lua +++ b/lua/plugins/configs/bufferline.lua @@ -39,7 +39,7 @@ local options = { right = function() return { { text = "%@Toggle_theme@" .. vim.g.toggle_theme_icon .. "%X" }, - { text = "%@Quit_vim@  %X" }, + { text = "%@Quit_vim@ %X" }, } end, }, diff --git a/lua/plugins/configs/lspconfig.lua b/lua/plugins/configs/lspconfig.lua index 634f3e9..f43dc1e 100644 --- a/lua/plugins/configs/lspconfig.lua +++ b/lua/plugins/configs/lspconfig.lua @@ -11,8 +11,6 @@ require("plugins.configs.others").lsp_handlers() function M.on_attach(client, _) client.resolved_capabilities.document_formatting = false client.resolved_capabilities.document_range_formatting = false - - require("core.mappings").lspconfig() end local capabilities = vim.lsp.protocol.make_client_capabilities() diff --git a/lua/plugins/configs/nvterm.lua b/lua/plugins/configs/nvterm.lua index 0ca9f4b..5e13ec2 100644 --- a/lua/plugins/configs/nvterm.lua +++ b/lua/plugins/configs/nvterm.lua @@ -24,17 +24,6 @@ local options = { close_on_exit = true, auto_insert = true, }, - mappings = { - toggle = { - float = "", - horizontal = "", - vertical = "", - }, - new = { - horizontal = "h", - vertical = "v", - }, - }, enable_new_mappings = true, } diff --git a/lua/plugins/configs/others.lua b/lua/plugins/configs/others.lua index 433560b..b5420fd 100644 --- a/lua/plugins/configs/others.lua +++ b/lua/plugins/configs/others.lua @@ -211,4 +211,22 @@ M.gitsigns = function() } end +M.misc_mappings = function() + local map = nvchad.map + + -- Don't copy the replaced text after pasting in visual mode + map("v", "p", '"_dP') + + -- Allow moving the cursor through wrapped lines with j, k, and + -- http://www.reddit.com/r/vim/comments/2k4cbr/problem_with_gj_and_gk/ + -- empty mode is same as using :map + -- also don't use g[j|k] when in operator pending mode, so it doesn't alter d, y or c behaviour + map("", "j", 'v:count || mode(1)[0:1] == "no" ? "j" : "gj"', { expr = true }) + map("", "k", 'v:count || mode(1)[0:1] == "no" ? "k" : "gk"', { expr = true }) + map("", "", 'v:count || mode(1)[0:1] == "no" ? "j" : "gj"', { expr = true }) + map("", "", 'v:count || mode(1)[0:1] == "no" ? "k" : "gk"', { expr = true }) + + -- esscape from terminal mode + map("t", "jk", "") +end return M diff --git a/lua/plugins/configs/telescope.lua b/lua/plugins/configs/telescope.lua index 1970cc8..4637492 100644 --- a/lua/plugins/configs/telescope.lua +++ b/lua/plugins/configs/telescope.lua @@ -54,6 +54,8 @@ local options = { n = { ["q"] = require("telescope.actions").close }, }, }, + + extensions_list = { "themes", "terms" }, } -- check for any override @@ -61,10 +63,8 @@ options = nvchad.load_override(options, "nvim-telescope/telescope.nvim") telescope.setup(options) -- load extensions -local extensions = nvchad.load_config().plugins.options.telescope.extensions - pcall(function() - for _, ext in ipairs(extensions) do + for _, ext in ipairs(options.extensions_list) do telescope.load_extension(ext) end end) diff --git a/lua/plugins/configs/whichkey.lua b/lua/plugins/configs/whichkey.lua new file mode 100644 index 0000000..3dfd7a9 --- /dev/null +++ b/lua/plugins/configs/whichkey.lua @@ -0,0 +1,73 @@ +local present, wk = pcall(require, "which-key") + +if not present then + return +end + +local options = { + + -- NOTE : this mode_opts table isnt in the default whichkey config + -- Its added here so you could configure it in chadrc + + mode_opts = { + n = { + mode = "n", + }, + + v = { + mode = "v", + }, + + i = { + mode = "i", + }, + + t = { + mode = "t", + }, + }, + + icons = { + breadcrumb = "»", -- symbol used in the command line area that shows your active key combo + separator = "  ", -- symbol used between a key and it's label + group = "+", -- symbol prepended to a group + }, + + popup_mappings = { + scroll_down = "", -- binding to scroll down inside the popup + scroll_up = "", -- binding to scroll up inside the popup + }, + + window = { + border = "none", -- none/single/double/shadow + }, + + layout = { + spacing = 6, -- spacing between columns + }, + + hidden = { "", "", "", "", "call", "lua", "^:", "^ " }, + + triggers_blacklist = { + -- list of mode / prefixes that should never be hooked by WhichKey + i = { "j", "k" }, + v = { "j", "k" }, + }, +} + +require("plugins.configs.others").misc_mappings() + +local mappings = nvchad.load_config().mappings + +-- register mappings +for mode, opt in pairs(options.mode_opts) do + for key, _ in pairs(mappings) do + if mappings[key][mode] then + wk.register(mappings[key][mode], opt) + end + end +end + +options = nvchad.load_override(options, "folke/which-key.nvim") + +wk.setup(options) diff --git a/lua/plugins/init.lua b/lua/plugins/init.lua index 93d441c..24739b3 100644 --- a/lua/plugins/init.lua +++ b/lua/plugins/init.lua @@ -1,5 +1,4 @@ -local plugin_settings = nvchad.load_config().plugins -local present, packer = pcall(require, plugin_settings.options.packer.init_file) +local present, packer = pcall(require, "plugins.packerInit") if not present then return false @@ -48,11 +47,6 @@ local plugins = { ["akinsho/bufferline.nvim"] = { after = "nvim-web-devicons", - - setup = function() - require("core.mappings").bufferline() - end, - config = function() require "plugins.configs.bufferline" end, @@ -194,11 +188,6 @@ local plugins = { ["numToStr/Comment.nvim"] = { module = "Comment", keys = { "gc", "gb" }, - - setup = function() - require("core.mappings").comment() - end, - config = function() require("plugins.configs.others").comment() end, @@ -207,10 +196,6 @@ local plugins = { -- file managing , picker etc ["kyazdani42/nvim-tree.lua"] = { cmd = { "NvimTreeToggle", "NvimTreeFocus" }, - setup = function() - require("core.mappings").nvimtree() - end, - config = function() require "plugins.configs.nvimtree" end, @@ -218,18 +203,24 @@ local plugins = { ["nvim-telescope/telescope.nvim"] = { cmd = "Telescope", - - setup = function() - require("core.mappings").telescope() - end, - config = function() require "plugins.configs.telescope" end, }, + + ["folke/which-key.nvim"] = { + opt = true, + setup = function() + nvchad.packer_lazy_load "which-key.nvim" + end, + config = function() + require "plugins.configs.whichkey" + end, + }, } plugins = nvchad.remove_default_plugins(plugins) + -- merge user plugin table & default plugin table plugins = nvchad.plugin_list(plugins) diff --git a/lua/plugins/packerInit.lua b/lua/plugins/packerInit.lua index c763fa1..50a29f5 100644 --- a/lua/plugins/packerInit.lua +++ b/lua/plugins/packerInit.lua @@ -27,21 +27,22 @@ if not present then end end -local user_snapshot = nvchad.load_config().snapshot - -packer.init { +local options = { display = { open_fn = function() - return require("packer.util").float { border = "single" } + return require("packer.util").float { border = "double" } end, - prompt_border = "single", }, git = { clone_timeout = 6000, -- seconds }, auto_clean = true, compile_on_sync = true, - snapshot = user_snapshot, + snapshot = nil, } +options = nvchad.load_override(options, "wbthomason/packer.nvim") + +packer.init(options) + return packer