breaking change : re-implement custom mappings | simplify it | add whichkey

fixes #1057 , #1047
This commit is contained in:
siduck 2022-05-12 18:26:01 +05:30
parent 4fa0b4ae7d
commit 0844431d37
15 changed files with 442 additions and 240 deletions

View file

@ -1,9 +1,3 @@
-- example file i.e lua/custom/init.lua
-- MAPPINGS
local map = nvchad.map
map("n", "<leader>cc", ":Telescope <CR>")
map("n", "<leader>q", ":q <CR>")
-- require("my autocmds file") or just declare them here
-- load your globals, autocmds here or anything .__.

View file

@ -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

View file

@ -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

37
lua/core/commands.lua Normal file
View file

@ -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()"

View file

@ -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

View file

@ -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<CR>:let @"=@0<CR>', { silent = true })
-- Allow moving the cursor through wrapped lines with j, k, <Up> and <Down>
-- http<cmd> ://www.reddit.com/r/vim/comments/2k4cbr/problem_with_gj_and_gk/
-- empty mode is same as using <cmd> :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("", "<Down>", 'v:count || mode(1)[0:1] == "no" ? "j" : "gj"', { expr = true })
map("", "<Up>", 'v:count || mode(1)[0:1] == "no" ? "k" : "gk"', { expr = true })
-- use ESC to turn off search highlighting
map("n", "<Esc>", "<cmd> :noh <CR>")
-- move cursor within insert mode
map("i", "<C-h>", "<Left>")
map("i", "<C-e>", "<End>")
map("i", "<C-l>", "<Right>")
map("i", "<C-j>", "<Down>")
map("i", "<C-k>", "<Up>")
map("i", "<C-a>", "<ESC>^i")
-- navigation between windows
map("n", "<C-h>", "<C-w>h")
map("n", "<C-l>", "<C-w>l")
map("n", "<C-k>", "<C-w>k")
map("n", "<C-j>", "<C-w>j")
map("n", "<leader>x", function()
nvchad.close_buffer()
end)
map("n", "<C-c>", "<cmd> :%y+ <CR>") -- copy whole file content
map("n", "<S-t>", "<cmd> :enew <CR>") -- new buffer
map("n", "<C-t>b", "<cmd> :tabnew <CR>") -- new tabs
map("n", "<leader>n", "<cmd> :set nu! <CR>")
map("n", "<leader>rn", "<cmd> :set rnu! <CR>") -- relative line numbers
map("n", "<C-s>", "<cmd> :w <CR>") -- ctrl + s to save file
-- terminal mappings
-- get out of terminal mode
map("t", { "jk" }, "<C-\\><C-n>")
-- 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", "<leader>uu", "<cmd> :NvChadUpdate <CR>")
-- 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", "<TAB>", "<cmd> :BufferLineCycleNext <CR>")
map("n", "<S-Tab>", "<cmd> :BufferLineCyclePrev <CR>")
end
i = {
M.comment = function()
map("n", "<leader>/", "<cmd> :lua require('Comment.api').toggle_current_linewise()<CR>")
map("v", "<leader>/", "<esc><cmd> :lua require('Comment.api').toggle_linewise_op(vim.fn.visualmode())<CR>")
end
-- go to beginning and end
["<C-b>"] = { "<ESC>^i", "論 beginning of line" },
["<C-e>"] = { "<End>", "壟 end of line" },
M.lspconfig = function()
-- navigate within insert mode
["<C-h>"] = { "<Left>", " move left" },
["<C-l>"] = { "<Right>", " move right" },
["<C-j>"] = { "<Down>", " move down" },
["<C-k>"] = { "<Up>", " move up" },
},
n = {
-- switch between windows
["<C-h>"] = { "<C-w>h", " window left" },
["<C-l>"] = { "<C-w>l", " window right" },
["<C-j>"] = { "<C-w>j", " window down" },
["<C-k>"] = { "<C-w>k", " window up" },
-- save
["<C-s>"] = { "<cmd> w <CR>", "﬚ save file" },
-- Copy all
["<C-c>"] = { "<cmd> %y+ <CR>", " copy whole file" },
-- line numbers
["<leader>n"] = { "<cmd> set nu! <CR>", " toggle line number" },
["<leader>rn"] = { "<cmd> set rnu! <CR>", " toggle relative number" },
-- update nvchad
["<leader>uu"] = { "<cmd> :NvChadUpdate <CR>", " update nvchad" },
["<leader>tt"] = {
function()
require("base46").toggle_theme()
end,
" toggle theme",
},
},
}
M.bufferline = {
n = {
-- new buffer
["<TAB>n"] = { "<cmd> enew <CR>", "烙 new buffer" },
-- cycle through buffers
["<TAB>"] = { "<cmd> BufferLineCycleNext <CR>", " cycle next buffer" },
["<S-Tab>"] = { "<cmd> BufferLineCyclePrev <CR>", " cycle prev buffer" },
-- close buffer + hide terminal buffer
["<leader>x"] = {
function()
nvchad.close_buffer()
end,
" close buffer",
},
},
}
M.comment = {
-- toggle comment in both modes
n = {
["<leader>/"] = { "<cmd> lua require('Comment.api').toggle_current_linewise()<CR>", "蘒 toggle comment" },
},
v = {
["<leader>/"] = {
"<cmd> lua require('Comment.api').toggle_current_linewise_op(vim.fn.visualmode())<CR>",
"蘒 toggle comment",
},
},
}
M.lspconfig = {
-- See `<cmd> :help vim.lsp.*` for documentation on any of the below functions
map("n", "gD", function()
n = {
["gD"] = {
function()
vim.lsp.buf.declaration()
end)
end,
" lsp declaration",
},
map("n", "gd", function()
["gd"] = {
function()
vim.lsp.buf.definition()
end)
end,
" lsp definition",
},
map("n", "K", function()
["K"] = {
function()
vim.lsp.buf.hover()
end)
end,
" lsp hover",
},
map("n", "gi", function()
["gi"] = {
function()
vim.lsp.buf.implementation()
end)
end,
" lsp implementation",
},
map("n", "<C-k>", function()
["<C-k>"] = {
function()
vim.lsp.buf.signature_help()
end)
end,
" lsp signature_help",
},
map("n", "<leader>D", function()
["<leader>D"] = {
function()
vim.lsp.buf.type_definition()
end)
end,
" lsp definition type",
},
map("n", "<leader>ra", function()
["<leader>ra"] = {
function()
vim.lsp.buf.rename()
end)
end,
" lsp rename",
},
map("n", "<leader>ca", function()
["<leader>ca"] = {
function()
vim.lsp.buf.code_action()
end)
end,
" lsp code_action",
},
map("n", "gr", function()
["gr"] = {
function()
vim.lsp.buf.references()
end)
end,
" lsp references",
},
map("n", "<leader>f", function()
["<leader>f"] = {
function()
vim.diagnostic.open_float()
end)
end,
" floating diagnostic",
},
map("n", "[d", function()
["[d"] = {
function()
vim.diagnostic.goto_prev()
end)
end,
" goto prev",
},
map("n", "d]", function()
["d]"] = {
function()
vim.diagnostic.goto_next()
end)
end,
" goto_next",
},
map("n", "<leader>q", function()
["<leader>q"] = {
function()
vim.diagnostic.setloclist()
end)
end,
" diagnostic setloclist",
},
map("n", "<leader>fm", function()
["<leader>fm"] = {
function()
vim.lsp.buf.formatting()
end)
end,
" lsp formatting",
},
map("n", "<leader>wa", function()
["<leader>wa"] = {
function()
vim.lsp.buf.add_workspace_folder()
end)
end,
" add workspace folder",
},
map("n", "<leader>wr", function()
["<leader>wr"] = {
function()
vim.lsp.buf.remove_workspace_folder()
end)
end,
" remove workspace folder",
},
map("n", "<leader>wl", function()
["<leader>wl"] = {
function()
print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
end)
end
end,
" list workspace folders",
},
},
}
M.nvimtree = function()
map("n", "<C-n>", "<cmd> :NvimTreeToggle <CR>")
map("n", "<leader>e", "<cmd> :NvimTreeFocus <CR>")
end
M.nvimtree = {
M.telescope = function()
map("n", "<leader>fb", "<cmd> :Telescope buffers <CR>")
map("n", "<leader>ff", "<cmd> :Telescope find_files <CR>")
map("n", "<leader>fa", "<cmd> :Telescope find_files follow=true no_ignore=true hidden=true <CR>")
map("n", "<leader>cm", "<cmd> :Telescope git_commits <CR>")
map("n", "<leader>gt", "<cmd> :Telescope git_status <CR>")
map("n", "<leader>fh", "<cmd> :Telescope help_tags <CR>")
map("n", "<leader>fw", "<cmd> :Telescope live_grep <CR>")
map("n", "<leader>fo", "<cmd> :Telescope oldfiles <CR>")
map("n", "<leader>th", "<cmd> :Telescope themes <CR>")
map("n", "<leader>tk", "<cmd> :Telescope keymaps <CR>")
n = {
-- toggle
["<C-n>"] = { "<cmd> NvimTreeToggle <CR>", " toggle nvimtree" },
-- focus
["<leader>e"] = { "<cmd> NvimTreeFocus <CR>", " focus nvimtree" },
},
}
M.telescope = {
n = {
-- find
["<leader>ff"] = { "<cmd> Telescope find_files <CR>", " find files" },
["<leader>fa"] = { "<cmd> Telescope find_files follow=true no_ignore=true hidden=true <CR>", " find all" },
["<leader>fw"] = { "<cmd> Telescope live_grep <CR>", " live grep" },
["<leader>fb"] = { "<cmd> Telescope buffers <CR>", " find buffers" },
["<leader>fh"] = { "<cmd> Telescope help_tags <CR>", " help page" },
["<leader>fo"] = { "<cmd> Telescope oldfiles <CR>", " find oldfiles" },
["<leader>tk"] = { "<cmd> Telescope keys <CR>", " show keys" },
-- git
["<leader>cm"] = { "<cmd> Telescope git_commits <CR>", " git commits" },
["<leader>gt"] = { "<cmd> Telescope git_status <CR>", " git status" },
-- pick a hidden term
map("n", "<leader>W", "<cmd> :Telescope terms <CR>")
end
["<leader>pt"] = { "<cmd> Telescope terms <CR>", " pick hidden term" },
-- theme switcher
["<leader>th"] = { "<cmd> Telescope themes <CR>", " nvchad themes" },
},
}
M.nvterm = {
n = {
-- toggle
["<A-i>"] = {
function()
require("nvterm.terminal").toggle "float"
end,
" toggle floating term",
},
["<A-h>"] = {
function()
require("nvterm.terminal").toggle "horizontal"
end,
" toggle horizontal term",
},
["<A-v>"] = {
function()
require("nvterm.terminal").toggle "vertical"
end,
" toggle vertical term",
},
-- new
["<leader>h"] = {
function()
require("nvterm.terminal").new "horizontal"
end,
" new horizontal term",
},
["<leader>v"] = {
function()
require("nvterm.terminal").new "vertical"
end,
" new vertical term",
},
},
}
return M

View file

@ -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

View file

@ -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()

View file

@ -24,17 +24,6 @@ local options = {
close_on_exit = true,
auto_insert = true,
},
mappings = {
toggle = {
float = "<A-i>",
horizontal = "<A-h>",
vertical = "<A-v>",
},
new = {
horizontal = "<leader>h",
vertical = "<leader>v",
},
},
enable_new_mappings = true,
}

View file

@ -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, <Up> and <Down>
-- 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("", "<Down>", 'v:count || mode(1)[0:1] == "no" ? "j" : "gj"', { expr = true })
map("", "<Up>", 'v:count || mode(1)[0:1] == "no" ? "k" : "gk"', { expr = true })
-- esscape from terminal mode
map("t", "jk", "<C-\\><C-n>")
end
return M

View file

@ -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)

View file

@ -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 = "<c-d>", -- binding to scroll down inside the popup
scroll_up = "<c-u>", -- binding to scroll up inside the popup
},
window = {
border = "none", -- none/single/double/shadow
},
layout = {
spacing = 6, -- spacing between columns
},
hidden = { "<silent>", "<cmd>", "<Cmd>", "<CR>", "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)

View file

@ -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)

View file

@ -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