--[[ Plugin-specific keymaps configuration This file contains all the plugin-specific keybindings, organized by plugin for better readability and maintenance. ]] local M = {} -- Initialize plugin keymaps function M.setup() -- Ensure vim is available in this scope local vim = vim or _G.vim or require('vim') -- Helper function for setting keymaps with proper labels local function map(mode, lhs, rhs, opts) opts = opts or {} opts.noremap = opts.noremap == nil and true or opts.noremap opts.silent = opts.silent == nil and true or opts.silent vim.keymap.set(mode, lhs, rhs, opts) end -- Check if which-key is available local which_key_ok, wk = pcall(require, "which-key") if not which_key_ok then vim.notify("Which-key not found, keybindings will be set without groups", vim.log.levels.WARN) end -------------------------------------------------- -- TELESCOPE -------------------------------------------------- local telescope_builtin_ok, builtin = pcall(require, "telescope.builtin") if telescope_builtin_ok then if which_key_ok then wk.add({ { "f", group = "Find/Files" }, { "ff", function() builtin.find_files() end, desc = "Find files" }, { "fg", function() builtin.live_grep() end, desc = "Live grep" }, { "fb", function() builtin.buffers() end, desc = "Find buffers" }, { "fh", function() builtin.help_tags() end, desc = "Help tags" }, { "fr", function() builtin.oldfiles() end, desc = "Recent files" }, { "fc", function() builtin.grep_string() end, desc = "Grep current string" }, { "fk", function() builtin.keymaps() end, desc = "Find keymaps" }, { "fd", function() builtin.diagnostics() end, desc = "Search diagnostics" }, { "fw", function() builtin.current_buffer_fuzzy_find() end, desc = "Search current buffer" }, }) wk.add({ { "fg", group = "Git Search" }, { "fgc", function() builtin.git_commits() end, desc = "Git commits" }, { "fgb", function() builtin.git_branches() end, desc = "Git branches" }, { "fgs", function() builtin.git_status() end, desc = "Git status" }, { "fgt", function() builtin.git_stash() end, desc = "Git stashes" }, }) wk.add({ { "", function() builtin.buffers() end, desc = "Find buffers (Telescope)" }, }) else map("n", "ff", function() builtin.find_files() end, { desc = "Find files" }) map("n", "fg", function() builtin.live_grep() end, { desc = "Live grep" }) map("n", "fb", function() builtin.buffers() end, { desc = "Find buffers" }) map("n", "fh", function() builtin.help_tags() end, { desc = "Help tags" }) map("n", "fr", function() builtin.oldfiles() end, { desc = "Recent files" }) map("n", "fc", function() builtin.grep_string() end, { desc = "Grep current string" }) map("n", "fk", function() builtin.keymaps() end, { desc = "Find keymaps" }) map("n", "fd", function() builtin.diagnostics() end, { desc = "Search diagnostics" }) map("n", "fw", function() builtin.current_buffer_fuzzy_find() end, { desc = "Search current buffer" }) map("n", "fgc", function() builtin.git_commits() end, { desc = "Git commits" }) map("n", "fgb", function() builtin.git_branches() end, { desc = "Git branches" }) map("n", "fgs", function() builtin.git_status() end, { desc = "Git status" }) map("n", "fgt", function() builtin.git_stash() end, { desc = "Git stashes" }) map("n", "", function() builtin.buffers() end, { desc = "Find buffers (Telescope)" }) end end -------------------------------------------------- -- BUFFERLINE -------------------------------------------------- local bufferline_commands_ok = pcall(require, 'bufferline') if bufferline_commands_ok then if which_key_ok then wk.add({ { "b", group = "Buffers" }, { "bp", "BufferLinePick", desc = "Pick buffer" }, { "bc", "BufferLinePickClose", desc = "Pick buffer to close" }, { "bh", "BufferLineCyclePrev", desc = "Previous buffer" }, { "bl", "BufferLineCycleNext", desc = "Next buffer" }, { "bH", "BufferLineMovePrev", desc = "Move buffer left" }, { "bL", "BufferLineMoveNext", desc = "Move buffer right" }, { "b1", "BufferLineGoToBuffer 1", desc = "Buffer 1" }, { "b2", "BufferLineGoToBuffer 2", desc = "Buffer 2" }, { "b3", "BufferLineGoToBuffer 3", desc = "Buffer 3" }, { "b4", "BufferLineGoToBuffer 4", desc = "Buffer 4" }, { "b5", "BufferLineGoToBuffer 5", desc = "Buffer 5" }, { "b6", "BufferLineGoToBuffer 6", desc = "Buffer 6" }, { "b7", "BufferLineGoToBuffer 7", desc = "Buffer 7" }, { "b8", "BufferLineGoToBuffer 8", desc = "Buffer 8" }, { "b9", "BufferLineGoToBuffer 9", desc = "Buffer 9" }, }) local alt_buffer_items = {} for i = 1, 9 do table.insert(alt_buffer_items, { string.format("", i), string.format("BufferLineGoToBuffer %d", i), desc = string.format("Buffer %d", i) }) end if #alt_buffer_items > 0 then wk.add(alt_buffer_items) end else map("n", "bp", "BufferLinePick", { desc = "Pick buffer" }) map("n", "bc", "BufferLinePickClose", { desc = "Pick buffer to close" }) map("n", "bh", "BufferLineCyclePrev", { desc = "Previous buffer" }) map("n", "bl", "BufferLineCycleNext", { desc = "Next buffer" }) map("n", "bH", "BufferLineMovePrev", { desc = "Move buffer left" }) map("n", "bL", "BufferLineMoveNext", { desc = "Move buffer right" }) for i = 1, 9 do map("n", string.format("b%d", i), string.format("BufferLineGoToBuffer %d", i), { desc = string.format("Buffer %d", i) }) map("n", string.format("", i), string.format("BufferLineGoToBuffer %d", i), { desc = string.format("Buffer %d", i) }) end end end -------------------------------------------------- -- HARPOON -------------------------------------------------- local harpoon_ok, harpoon = pcall(require, "harpoon") if harpoon_ok then if which_key_ok then local harpoon_items = { { "h", group = "Harpoon" }, { "ha", function() harpoon:list():add() end, desc = "Add file" }, { "hm", function() harpoon.ui:toggle_quick_menu(harpoon:list()) end, desc = "Toggle menu" }, } for i = 1, 9 do table.insert(harpoon_items, { string.format("h%d", i), function() harpoon:list():select(i) end, desc = string.format("Jump to file %d", i) }) end wk.add(harpoon_items) wk.add({ { "", function() harpoon:list():next() end, desc = "Harpoon next file" }, { "", function() harpoon:list():prev() end, desc = "Harpoon previous file" }, }) else map("n", "ha", function() harpoon:list():add() end, { desc = "Add file" }) map("n", "hm", function() harpoon.ui:toggle_quick_menu(harpoon:list()) end, { desc = "Toggle menu" }) for i = 1, 9 do map("n", string.format("h%d", i), function() harpoon:list():select(i) end, { desc = string.format("Jump to file %d", i) }) end map("n", "", function() harpoon:list():next() end, { desc = "Harpoon next file" }) map("n", "", function() harpoon:list():prev() end, { desc = "Harpoon previous file" }) end end -------------------------------------------------- -- GIT (LAZYGIT) -------------------------------------------------- local lazygit_commands_ok = pcall(require, 'lazygit') if lazygit_commands_ok then if which_key_ok then wk.add({ { "g", group = "Git" }, { "gg", "LazyGit", desc = "Open LazyGit" }, { "gc", "LazyGitConfig", desc = "LazyGit Config" }, { "gf", "LazyGitCurrentFile", desc = "LazyGit Current File" }, { "gb", "LazyGitFilter", desc = "LazyGit Filter" }, { "gB", "LazyGitFilterCurrentFile", desc = "LazyGit Filter Current File" }, }) else map('n', 'gg', 'LazyGit', { desc = 'Open LazyGit' }) map('n', 'gc', 'LazyGitConfig', { desc = 'LazyGit Config' }) map('n', 'gf', 'LazyGitCurrentFile', { desc = 'LazyGit Current File' }) map('n', 'gb', 'LazyGitFilter', { desc = 'LazyGit Filter' }) map('n', 'gB', 'LazyGitFilterCurrentFile', { desc = 'LazyGit Filter Current File' }) end end -------------------------------------------------- -- COMMENTING -------------------------------------------------- -- Comment.nvim and other commenting tools are already mapped through their setup -------------------------------------------------- -- COPILOT CHAT -------------------------------------------------- local copilot_chat_ok = pcall(require, 'CopilotChat') if copilot_chat_ok then if which_key_ok then -- Normal mode commands wk.add({ { "c", group = "Copilot" }, { "cc", "CopilotChat", desc = "Open Chat" }, { "ce", "CopilotChatExplain", desc = "Explain Code" }, { "cf", "CopilotChatFixCode", desc = "Fix Code" }, { "co", "CopilotChatOptimize", desc = "Optimize Code" }, { "cd", "CopilotChatDocumentation", desc = "Generate Documentation" }, { "ct", "CopilotChatTests", desc = "Generate Tests" }, { "cb", "CopilotChatBestPractices", desc = "Check Best Practices" }, { "cr", "CopilotChatRefactor", desc = "Refactor Code" }, { "cs", "CopilotChatSummarize", desc = "Summarize Code" }, { "ch", "CopilotChatHelp", desc = "Copilot Chat Help" }, }) -- Visual mode commands wk.add({ { "c", group = "Copilot", mode = "v" }, { "ce", ":CopilotChatExplain", desc = "Explain Selected Code", mode = "v" }, { "cf", ":CopilotChatFixCode", desc = "Fix Selected Code", mode = "v" }, { "co", ":CopilotChatOptimize", desc = "Optimize Selected Code", mode = "v" }, { "cd", ":CopilotChatDocumentation", desc = "Document Selected Code", mode = "v" }, { "ct", ":CopilotChatTests", desc = "Generate Tests for Selection", mode = "v" }, { "cr", ":CopilotChatRefactor", desc = "Refactor Selected Code", mode = "v" }, { "cs", ":CopilotChatSummarize", desc = "Summarize Selected Code", mode = "v" }, }) else map("n", "cc", "CopilotChat", { desc = "Open Chat" }) map("n", "ce", "CopilotChatExplain", { desc = "Explain Code" }) map("n", "cf", "CopilotChatFixCode", { desc = "Fix Code" }) map("n", "co", "CopilotChatOptimize", { desc = "Optimize Code" }) map("n", "cd", "CopilotChatDocumentation", { desc = "Generate Documentation" }) map("n", "ct", "CopilotChatTests", { desc = "Generate Tests" }) map("n", "cb", "CopilotChatBestPractices", { desc = "Check Best Practices" }) map("n", "cr", "CopilotChatRefactor", { desc = "Refactor Code" }) map("n", "cs", "CopilotChatSummarize", { desc = "Summarize Code" }) map("n", "ch", "CopilotChatHelp", { desc = "Copilot Chat Help" }) map("v", "ce", ":CopilotChatExplain", { desc = "Explain Selected Code" }) map("v", "cf", ":CopilotChatFixCode", { desc = "Fix Selected Code" }) map("v", "co", ":CopilotChatOptimize", { desc = "Optimize Selected Code" }) map("v", "cd", ":CopilotChatDocumentation", { desc = "Document Selected Code" }) map("v", "ct", ":CopilotChatTests", { desc = "Generate Tests for Selection" }) map("v", "cr", ":CopilotChatRefactor", { desc = "Refactor Selected Code" }) map("v", "cs", ":CopilotChatSummarize", { desc = "Summarize Selected Code" }) end end -------------------------------------------------- -- LEETCODE -------------------------------------------------- local leetcode_ok = pcall(require, 'leetcode') if leetcode_ok then if which_key_ok then wk.add({ { "l", group = "LeetCode" }, { "ll", "Leet", desc = "Open LeetCode" }, { "ld", "Leet daily", desc = "LeetCode daily challenge" }, { "lr", "Leet random", desc = "LeetCode random problem" }, { "ls", "Leet submit", desc = "Submit LeetCode solution" }, { "la", "Leet tabs", desc = "Switch tab" }, }) else map('n', 'll', 'Leet', { desc = 'Open LeetCode' }) map('n', 'ld', 'Leet daily', { desc = 'LeetCode daily challenge' }) map('n', 'lr', 'Leet random', { desc = 'LeetCode random problem' }) map('n', 'ls', 'Leet submit', { desc = 'Submit LeetCode solution' }) map('n', 'la', 'Leet tabs', { desc = 'Switch tab' }) end end return M end return M