From 8f65f878a72fd5fd37f63daa6a38fba97d1c2ef3 Mon Sep 17 00:00:00 2001 From: rootiest Date: Thu, 29 Aug 2024 14:25:29 -0400 Subject: [PATCH] refactor: centralize all keybind definitions into data module --- lua/config/keymaps.lua | 214 ++++++-------------------------------- lua/data/keys.lua | 231 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 261 insertions(+), 184 deletions(-) diff --git a/lua/config/keymaps.lua b/lua/config/keymaps.lua index 1050f32..abbfb31 100644 --- a/lua/config/keymaps.lua +++ b/lua/config/keymaps.lua @@ -1,197 +1,45 @@ -- ╭─────────────────────────────────────────────────────────╮ -- │ Keybinds │ -- ╰─────────────────────────────────────────────────────────╯ --- Initialize which-key -local wk = require("which-key") -local rootiest = require("config.rootiest") -local smartsplits = require("smart-splits") + +-- All keybinds are defined in lua/data/keys.lua +-- This allows both plugin and general keybinds to +-- be defined in a single central location + +-- Load data module local data = require("data") -local all_modes = { "n", "i", "v", "x", "s", "o", "c", "t" } +-- Add keymapper function +local add_keymap = data.func.add_keymap -local add_keymap = require("data").func.add_keymap +-- ━━━━━━━━━━━━━━━━━━━━━━━━━━ General Keybinds ━━━━━━━━━━━━━━━━━━━━━━━ --- General Keybinds -add_keymap("gt", function() - rootiest.toggle_lazygit_term() -end, "LazyGit Terminal") -add_keymap("yo", function() - rootiest.yank_line() -end, "Yank Line-text") -add_keymap("uH", function() - rootiest.toggle_hardmode() -end, "Toggle Hardmode") -add_keymap("Y", "%y", "Yank buffer contents") -add_keymap("", "norm ggVG", "Select all") -add_keymap("|", "Neotree reveal toggle", "Neotree reveal") -add_keymap( - "Ca", - ":lua require('utils.rootiest').code_action_on_selection()", - "Code Action on Selection", - "x" -) - --- Exit Neovim -add_keymap({ - lhs = "Cq", - rhs = function() - data.func.exit() - end, - desc = "Exit Neovim", -}) - --- Group Keybinds -wk.add({ - { - lhs = "I", - group = "IconPicker", - icon = { icon = "󰥸", color = "orange" }, - }, - { - lhs = "gn", - group = "Gists", - icon = { icon = "", color = "orange" }, - }, - { - lhs = "l", - group = "Lazy", - }, - { - "lv", - "Lazy", - desc = "LazyVim", - }, - { - "lx", - "LazyExtras", - desc = "LazyExtras", - }, - { - lhs = "m", - group = "MiniMap", - icon = { icon = "", color = "yellow" }, - }, - { "u,", group = "Font" }, -}) - --- Resizing splits -local resize_splits = { - { - "", - function() - smartsplits.resize_left() - end, - "Resize split left", - }, - { - "", - function() - smartsplits.resize_down() - end, - "Resize split down", - }, - { - "", - function() - smartsplits.resize_up() - end, - "Resize split up", - }, - { - "", - function() - smartsplits.resize_right() - end, - "Resize split right", - }, - { - "", - function() - smartsplits.start_resize_mode() - end, - "Resize split to previous size", - }, -} - -for _, map in ipairs(resize_splits) do - add_keymap(map[1], map[2], map[3], all_modes) +-- Add keymaps for miscellaneous keybinds +for _, item in ipairs(data.keys.misc) do + add_keymap(item) end --- Moving between splits -local move_splits = { - { - "", - function() - smartsplits.move_cursor_left() - end, - "Move cursor left", - }, - { - "", - function() - smartsplits.move_cursor_down() - end, - "Move cursor down", - }, - { - "", - function() - smartsplits.move_cursor_up() - end, - "Move cursor up", - }, - { - "", - function() - smartsplits.move_cursor_right() - end, - "Move cursor right", - }, - { - "", - function() - smartsplits.move_cursor_previous() - end, - "Move cursor to previous split", - }, -} +-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━ Group Keybinds ━━━━━━━━━━━━━━━━━━━━━━━━ -for _, map in ipairs(move_splits) do - add_keymap(map[1], map[2], map[3], all_modes) +-- Add keymaps for menu groups +for _, item in ipairs(data.keys.groups) do + add_keymap(item) end --- Swapping buffers between windows -local swap_buffers = { - { - "", - function() - smartsplits.swap_buf_left() - end, - "Swap buffer left", - }, - { - "", - function() - smartsplits.swap_buf_down() - end, - "Swap buffer down", - }, - { - "", - function() - smartsplits.swap_buf_up() - end, - "Swap buffer up", - }, - { - "", - function() - smartsplits.swap_buf_right() - end, - "Swap buffer right", - }, -} +-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━ Resizing splits ━━━━━━━━━━━━━━━━━━━━━━━━━━━ -for _, map in ipairs(swap_buffers) do - add_keymap(map[1], map[2], map[3], all_modes) +for _, map in ipairs(data.keys.splits.resize) do + add_keymap(map[1], map[2], map[3], data.types.all_modes) +end + +-- ━━━━━━━━━━━━━━━━━━━━━━━━ Moving between splits ━━━━━━━━━━━━━━━━━━━━━━━━ + +for _, map in ipairs(data.keys.splits.move) do + add_keymap(map[1], map[2], map[3], data.types.all_modes) +end + +-- ━━━━━━━━━━━━━━━━━━ Swapping buffers between windows ━━━━━━━━━━━━━━━ + +for _, map in ipairs(data.keys.splits.swap) do + add_keymap(map[1], map[2], map[3], data.types.all_modes) end diff --git a/lua/data/keys.lua b/lua/data/keys.lua index b468638..34ad52f 100644 --- a/lua/data/keys.lua +++ b/lua/data/keys.lua @@ -4,6 +4,9 @@ local M = {} +-- Load utils +local rootiest = require("config.rootiest") + M.alternate = { { -- Toggle Alternate "a", @@ -220,6 +223,39 @@ M.gist = { }, } +M.groups = { + -- Icon picker + { + lhs = "I", + group = "IconPicker", + icon = { icon = "󰥸", color = "orange" }, + }, + -- Gists menu + { + lhs = "gn", + group = "Gists", + icon = { icon = "", color = "orange" }, + }, + -- Lazy menu + { + lhs = "l", + group = "Lazy", + icon = { icon = "󰒲", color = "red" }, + }, + -- MiniMap menu + { + lhs = "n", + group = "MiniMap", + icon = { icon = "", color = "green" }, + }, + -- Code action menu + { + lhs = "C", + group = "CodeActions", + icon = { icon = "", color = "yellow" }, + }, +} + M.gx = { { -- Open URL/Link "gx", @@ -256,6 +292,83 @@ M.lazygit = { }, } +M.misc = { + -- LazyGit + { + lhs = "gt", + rhs = function() + rootiest.toggle_lazygit_term() + end, + desc = "LazyGit Terminal", + }, + -- Yank line (without whitespace) + { + lhs = "yo", + rhs = function() + rootiest.yank_line() + end, + desc = "Yank Line-text", + }, + -- Hardmode + { + lhs = "uH", + rhs = function() + rootiest.toggle_hardmode() + end, + desc = "Toggle Hardmode", + }, + -- Yank buffer + { + lhs = "Y", + rhs = "%y", + desc = "Yank buffer contents", + }, + -- Select all + { + lhs = "", + rhs = "norm ggVG", + desc = "Select all", + }, + -- Neotree + { + lhs = "|", + rhs = "Neotree reveal toggle", + desc = "Neotree toggle", + }, + -- Code action on selection + { + lhs = "Ca", + rhs = ":lua require('utils.rootiest').code_action_on_selection()", + desc = "Code Action on Selection", + mode = "x", + }, + -- Exit Neovim + { + lhs = "Q", + rhs = "lua require('data').func.exit()", + desc = "Exit Neovim", + }, + -- LazyVim + { + lhs = "lv", + rhs = "Lazy", + desc = "LazyVim", + }, + -- LazyExtras + { + lhs = "lx", + rhs = "LazyExtras", + desc = "LazyExtras", + }, + -- De-map 's' to avoid conflicts with mini.surround + { + lhs = "s", + rhs = "", + desc = "Nos", + mode = { "n", "x" }, + }, +} + M.nekifoch = { { -- List Fonts "u,l", @@ -294,7 +407,7 @@ M.precog = { } M.qalc = { - { + { -- Open Qalc "qc", "Qalc", desc = "Qalc", @@ -316,6 +429,113 @@ M.splitjoin = { toggle = "gJ", } +M.splits = { + resize = { + { + "", + function() + require("smart-splits").resize_left() + end, + "Resize split left", + }, + { + "", + function() + require("smart-splits").resize_down() + end, + "Resize split down", + }, + { + "", + function() + require("smart-splits").resize_up() + end, + "Resize split up", + }, + { + "", + function() + require("smart-splits").resize_right() + end, + "Resize split right", + }, + { + "", + function() + require("smart-splits").start_resize_mode() + end, + "Resize split to previous size", + }, + }, + move = { + { + "", + function() + require("smart-splits").move_cursor_left() + end, + "Move cursor left", + }, + { + "", + function() + require("smart-splits").move_cursor_down() + end, + "Move cursor down", + }, + { + "", + function() + require("smart-splits").move_cursor_up() + end, + "Move cursor up", + }, + { + "", + function() + require("smart-splits").move_cursor_right() + end, + "Move cursor right", + }, + { + "", + function() + require("smart-splits").move_cursor_previous() + end, + "Move cursor to previous split", + }, + }, + swap = { + { + "", + function() + require("smart-splits").swap_buf_left() + end, + "Swap buffer left", + }, + { + "", + function() + require("smart-splits").swap_buf_down() + end, + "Swap buffer down", + }, + { + "", + function() + require("smart-splits").swap_buf_up() + end, + "Swap buffer up", + }, + { + "", + function() + require("smart-splits").swap_buf_right() + end, + "Swap buffer right", + }, + }, +} + M.substitute = { { -- Substitute operator in normal mode "x", @@ -356,6 +576,15 @@ M.substitute = { }, } +M.surround = { + { -- De-map 's' to prevent conflicts + "s", + "", + desc = "Nos", + mode = { "n", "x" }, + }, +} + M.toggleterm = { { -- Toggle Terminal "",