Skip to content

Commit

Permalink
Allow whichkey to not load menus for plugins that aren't loaded
Browse files Browse the repository at this point in the history
Signed-off-by: Micah Halter <micah@balena.io>
  • Loading branch information
mehalter committed Apr 8, 2022
1 parent 79d014a commit 370a0a3
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 124 deletions.
1 change: 1 addition & 0 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ local sources = {
"core.plugins",
"core.autocmds",
"core.mappings",
"configs.which-key-register",
}

for _, source in ipairs(sources) do
Expand Down
153 changes: 153 additions & 0 deletions lua/configs/which-key-register.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
local M = {}

local utils = require "core.utils"

local status_ok, which_key = pcall(require, "which-key")
if not status_ok then
return
end

local opts = {
mode = "n",
prefix = "<leader>",
buffer = nil,
silent = true,
noremap = true,
nowait = true,
}

local mappings = {
["w"] = { "Save" },
["q"] = { "Quit" },
["h"] = { "No Highlight" },

p = {
name = "Packer",
c = { "Compile" },
i = { "Install" },
s = { "Sync" },
S = { "Status" },
u = { "Update" },
},

l = {
name = "LSP",
a = { "Code Action" },
d = { "Hover Diagnostic" },
f = { "Format" },
i = { "Info" },
I = { "Installer Info" },
r = { "Rename" },
},
}

local extra_sections = {
f = "File",
g = "Git",
l = "LSP",
s = "Search",
t = "Terminal",
S = "Session",
}

local function init_table(idx)
if not mappings[idx] then
mappings[idx] = { name = extra_sections[idx] }
end
end

if utils.is_available "neo-tree.nvim" then
mappings.e = { "Toggle Explorer" }
mappings.o = { "Focus Explorer" }
end

if utils.is_available "dashboard-nvim" then
mappings.d = { "Dashboard" }

init_table "f"
mappings.f.n = { "New File" }

init_table "S"
mappings.S.s = { "Save Session" }
mappings.S.l = { "Load Session" }
end

if utils.is_available "Comment.nvim" then
mappings["/"] = { "Comment" }
end

if utils.is_available "vim-bbye" then
mappings.c = { "Close Buffer" }
end

if utils.is_available "gitsigns.nvim" then
init_table "g"
mappings.g.j = { "Next Hunk" }
mappings.g.k = { "Prev Hunk" }
mappings.g.l = { "Blame" }
mappings.g.p = { "Preview Hunk" }
mappings.g.h = { "Reset Hunk" }
mappings.g.r = { "Reset Buffer" }
mappings.g.s = { "Stage Hunk" }
mappings.g.u = { "Undo Stage Hunk" }
mappings.g.d = { "Diff" }
end

if utils.is_available "gitsigns.nvim" then
init_table "g"
mappings.g.t = { "Open changed file" }
mappings.g.b = { "Checkout branch" }
mappings.g.c = { "Checkout commit" }
end

if utils.is_available "nvim-toggleterm.lua" then
init_table "g"
mappings.g.g = { "Lazygit" }

init_table "t"
mappings.t.n = { "Node" }
mappings.t.u = { "NCDU" }
mappings.t.t = { "Htop" }
mappings.t.p = { "Python" }
mappings.t.f = { "Float" }
mappings.t.h = { "Horizontal" }
mappings.t.v = { "Vertical" }
end

if utils.is_available "symbols-outline.nvim" then
init_table "l"
mappings.l.S = { "Symbols Outline" }
end

if utils.is_available "nvim-telescope/telescope.nvim" then
init_table "s"
mappings.s.b = { "Checkout branch" }
mappings.s.h = { "Find Help" }
mappings.s.m = { "Man Pages" }
mappings.s.n = { "Notifications" }
mappings.s.r = { "Registers" }
mappings.s.k = { "Keymaps" }
mappings.s.c = { "Commands" }

init_table "g"
mappings.g.t = { "Open changed file" }
mappings.g.b = { "Checkout branch" }
mappings.g.c = { "Checkout commit" }

init_table "f"
mappings.f.b = { "Find Buffers" }
mappings.f.f = { "Find Files" }
mappings.f.h = { "Find Help" }
mappings.f.m = { "Find Marks" }
mappings.f.o = { "Find Old Files" }
mappings.f.w = { "Find Words" }

init_table "l"
mappings.l.s = { "Document Symbols" }
mappings.l.R = { "References" }
mappings.l.D = { "All Diagnostics" }
end

which_key.register(require("core.utils").user_plugin_opts("which-key.register_n_leader", mappings), opts)

return M
92 changes: 0 additions & 92 deletions lua/configs/which-key.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,99 +57,7 @@ function M.config()
},
}

local opts = {
mode = "n",
prefix = "<leader>",
buffer = nil,
silent = true,
noremap = true,
nowait = true,
}

local mappings = {
["d"] = { "Dashboard" },
["e"] = { "Explorer" },
["w"] = { "Save" },
["q"] = { "Quit" },
["c"] = { "Close Buffer" },
["h"] = { "No Highlight" },
["/"] = { "Comment" },

p = {
name = "Packer",
c = { "Compile" },
i = { "Install" },
s = { "Sync" },
S = { "Status" },
u = { "Update" },
},

f = {
name = "Find",
b = { "Buffers" },
f = { "Files" },
h = { "Help Tags" },
o = { "Old Files" },
w = { "Grep Words" },
},

g = {
name = "Git",
g = { "Lazygit" },
j = { "Next Hunk" },
k = { "Prev Hunk" },
l = { "Blame" },
p = { "Preview Hunk" },
h = { "Reset Hunk" },
r = { "Reset Buffer" },
s = { "Stage Hunk" },
u = { "Undo Stage Hunk" },
t = { "Open changed file" },
b = { "Checkout branch" },
c = { "Checkout commit" },
d = { "Diff" },
},

l = {
name = "LSP",
a = { "Code Action" },
d = { "Hover Diagnostic" },
D = { "All Diagnostics" },
f = { "Format" },
i = { "Info" },
I = { "Installer Info" },
r = { "Rename" },
R = { "References" },
s = { "Document Symbols" },
S = { "Symbols Outline" },
},

s = {
name = "Search",
b = { "Checkout branch" },
h = { "Find Help" },
m = { "Man Pages" },
n = { "Notifications" },
r = { "Registers" },
k = { "Keymaps" },
c = { "Commands" },
},

t = {
name = "Terminal",
n = { "Node" },
u = { "NCDU" },
t = { "Htop" },
p = { "Python" },
l = { "LazyGit" },
f = { "Float" },
h = { "Horizontal" },
v = { "Vertical" },
},
}

which_key.setup(require("core.utils").user_plugin_opts("plugins.which-key", default_setup))
which_key.register(require("core.utils").user_plugin_opts("which-key.register_n_leader", mappings), opts)
end

return M
70 changes: 38 additions & 32 deletions lua/core/mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ vim.g.mapleader = " "
vim.g.maplocalleader = " "

-- Normal --
-- Better window navigation
map("n", "<C-h>", "<cmd>lua require'smart-splits'.move_cursor_left()<cr>", opts)
map("n", "<C-j>", "<cmd>lua require'smart-splits'.move_cursor_down()<cr>", opts)
map("n", "<C-k>", "<cmd>lua require'smart-splits'.move_cursor_up()<cr>", opts)
map("n", "<C-l>", "<cmd>lua require'smart-splits'.move_cursor_right()<cr>", opts)

-- Resize with arrows
map("n", "<C-Up>", "<cmd>lua require'smart-splits'.resize_up(2)<cr>", opts)
map("n", "<C-Down>", "<cmd>lua require'smart-splits'.resize_down(2)<cr>", opts)
map("n", "<C-Left>", "<cmd>lua require'smart-splits'.resize_left(2)<cr>", opts)
map("n", "<C-Right>", "<cmd>lua require'smart-splits'.resize_right(2)<cr>", opts)
if utils.is_available "smart-splits.nvim" then
-- Better window navigation
map("n", "<C-h>", "<cmd>lua require'smart-splits'.move_cursor_left()<cr>", opts)
map("n", "<C-j>", "<cmd>lua require'smart-splits'.move_cursor_down()<cr>", opts)
map("n", "<C-k>", "<cmd>lua require'smart-splits'.move_cursor_up()<cr>", opts)
map("n", "<C-l>", "<cmd>lua require'smart-splits'.move_cursor_right()<cr>", opts)

-- Resize with arrows
map("n", "<C-Up>", "<cmd>lua require'smart-splits'.resize_up(2)<cr>", opts)
map("n", "<C-Down>", "<cmd>lua require'smart-splits'.resize_down(2)<cr>", opts)
map("n", "<C-Left>", "<cmd>lua require'smart-splits'.resize_left(2)<cr>", opts)
map("n", "<C-Right>", "<cmd>lua require'smart-splits'.resize_right(2)<cr>", opts)
end

-- Navigate buffers
if utils.is_available "bufferline.nvim" then
Expand All @@ -41,9 +43,12 @@ map("n", "<A-k>", "<Esc><cmd>m .-2<CR>==gi", opts)
-- Standard Operations
map("n", "<leader>w", "<cmd>w<CR>", opts)
map("n", "<leader>q", "<cmd>q<CR>", opts)
map("n", "<leader>c", "<cmd>Bdelete!<CR>", opts)
map("n", "<leader>h", "<cmd>nohlsearch<CR>", opts)

if utils.is_available "vim-bbye" then
map("n", "<leader>c", "<cmd>Bdelete!<CR>", opts)
end

-- Packer
map("n", "<leader>pc", "<cmd>PackerCompile<cr>", opts)
map("n", "<leader>pi", "<cmd>PackerInstall<cr>", opts)
Expand All @@ -66,8 +71,6 @@ end
if utils.is_available "dashboard-nvim" then
map("n", "<leader>d", "<cmd>Dashboard<CR>", opts)
map("n", "<leader>fn", "<cmd>DashboardNewFile<CR>", opts)
map("n", "<leader>db", "<cmd>Dashboard<CR>", opts)
map("n", "<leader>bm", "<cmd>DashboardJumpMarks<CR>", opts)
map("n", "<leader>sl", "<cmd>SessionLoad<CR>", opts)
map("n", "<leader>ss", "<cmd>SessionSave<CR>", opts)
end
Expand All @@ -86,24 +89,27 @@ if utils.is_available "gitsigns.nvim" then
end

-- Telescope
map("n", "<leader>fw", "<cmd>Telescope live_grep<CR>", opts)
map("n", "<leader>gt", "<cmd>Telescope git_status<CR>", opts)
map("n", "<leader>gb", "<cmd>Telescope git_branches<CR>", opts)
map("n", "<leader>gc", "<cmd>Telescope git_commits<CR>", opts)
map("n", "<leader>ff", "<cmd>Telescope find_files<CR>", opts)
map("n", "<leader>fb", "<cmd>Telescope buffers<CR>", opts)
map("n", "<leader>fh", "<cmd>Telescope help_tags<CR>", opts)
map("n", "<leader>fo", "<cmd>Telescope oldfiles<CR>", opts)
map("n", "<leader>sb", "<cmd>Telescope git_branches<CR>", opts)
map("n", "<leader>sh", "<cmd>Telescope help_tags<CR>", opts)
map("n", "<leader>sm", "<cmd>Telescope man_pages<CR>", opts)
map("n", "<leader>sn", "<cmd>Telescope notify<CR>", opts)
map("n", "<leader>sr", "<cmd>Telescope registers<CR>", opts)
map("n", "<leader>sk", "<cmd>Telescope keymaps<CR>", opts)
map("n", "<leader>sc", "<cmd>Telescope commands<CR>", opts)
map("n", "<leader>ls", "<cmd>Telescope lsp_document_symbols<CR>", opts)
map("n", "<leader>lR", "<cmd>Telescope lsp_references<CR>", opts)
map("n", "<leader>lD", "<cmd>Telescope diagnostics<CR>", opts)
if utils.is_available "telescope.nvim" then
map("n", "<leader>fw", "<cmd>Telescope live_grep<CR>", opts)
map("n", "<leader>gt", "<cmd>Telescope git_status<CR>", opts)
map("n", "<leader>gb", "<cmd>Telescope git_branches<CR>", opts)
map("n", "<leader>gc", "<cmd>Telescope git_commits<CR>", opts)
map("n", "<leader>ff", "<cmd>Telescope find_files<CR>", opts)
map("n", "<leader>fb", "<cmd>Telescope buffers<CR>", opts)
map("n", "<leader>fh", "<cmd>Telescope help_tags<CR>", opts)
map("n", "<leader>fm", "<cmd>Telescope marks<CR>", opts)
map("n", "<leader>fo", "<cmd>Telescope oldfiles<CR>", opts)
map("n", "<leader>sb", "<cmd>Telescope git_branches<CR>", opts)
map("n", "<leader>sh", "<cmd>Telescope help_tags<CR>", opts)
map("n", "<leader>sm", "<cmd>Telescope man_pages<CR>", opts)
map("n", "<leader>sn", "<cmd>Telescope notify<CR>", opts)
map("n", "<leader>sr", "<cmd>Telescope registers<CR>", opts)
map("n", "<leader>sk", "<cmd>Telescope keymaps<CR>", opts)
map("n", "<leader>sc", "<cmd>Telescope commands<CR>", opts)
map("n", "<leader>ls", "<cmd>Telescope lsp_document_symbols<CR>", opts)
map("n", "<leader>lR", "<cmd>Telescope lsp_references<CR>", opts)
map("n", "<leader>lD", "<cmd>Telescope diagnostics<CR>", opts)
end

-- LSP
map("n", "gD", "<cmd>lua vim.lsp.buf.declaration()<CR>", opts)
Expand Down

0 comments on commit 370a0a3

Please sign in to comment.