Files
faumaray b5f1714ee7 update
Signed-off-by: faumaray <faumaray@gmail.com>
2026-04-20 01:50:04 +03:00

462 lines
18 KiB
Lua

-- keyboard.lua
-- All keymaps centralized. Loaded after plugins so every mapping wins.
local wk = require("which-key")
local utils = require("utils")
-- Helper: merge description into opts
local function mdesc(opt, description)
return vim.tbl_extend("force", opt, { desc = description })
end
local map = function(mode, lhs, rhs, opts, desc)
opts = opts and opts or {}
opts = mdesc(opts, desc)
vim.keymap.set(mode, lhs, rhs, opts)
end
-- =============================================================================
-- Which-key groups
-- =============================================================================
wk.add({
{ "<leader>b", group = "Buffer" },
{ "<leader>c", group = "Code" },
{ "<leader>d", group = "Debug" },
{ "<leader>f", group = "Find" },
{ "<leader>g", group = "Git" },
{ "<leader>l", group = "LSP" },
{ "<leader>s", group = "Search" },
{ "<leader>t", group = "Test/Run" },
{ "<leader>u", group = "UI Toggle" },
{ "<leader>w", group = "Window/Workspace" },
})
-- =============================================================================
-- General
-- =============================================================================
map({ "i", "v", "n", "s" }, "<C-s>", function()
vim.cmd([[w]])
end, {}, "Save file")
map({ "i", "v", "n", "s" }, "<leader>ww", function()
vim.cmd([[w]])
end, {}, "Save file")
-- Better up/down on wrapped lines
map("n", "j", "v:count == 0 ? 'gj' : 'j'", { expr = true, silent = true })
map("n", "k", "v:count == 0 ? 'gk' : 'k'", { expr = true, silent = true })
-- =============================================================================
-- Window management
-- =============================================================================
-- Navigation (normal + terminal)
map({ "t", "i" }, "<C-h>", [[<C-\><C-N><C-w>h]])
map({ "t", "i" }, "<C-j>", [[<C-\><C-N><C-w>j]])
map({ "t", "i" }, "<C-k>", [[<C-\><C-N><C-w>k]])
map({ "t", "i" }, "<C-l>", [[<C-\><C-N><C-w>l]])
map("n", "<C-h>", "<C-w>h", {}, "Go to left window")
map("n", "<C-j>", "<C-w>j", {}, "Go to lower window")
map("n", "<C-k>", "<C-w>k", {}, "Go to upper window")
map("n", "<C-l>", "<C-w>l", {}, "Go to right window")
-- Resize
map("n", "<C-Up>", "<cmd>resize +2<cr>", {}, "Increase window height")
map("n", "<C-Down>", "<cmd>resize -2<cr>", {}, "Decrease window height")
map("n", "<C-Left>", "<cmd>vertical resize -2<cr>", {}, "Decrease window width")
map("n", "<C-Right>", "<cmd>vertical resize +2<cr>", {}, "Increase window width")
-- Splits
map("n", "<leader>wh", "<C-W>s", {}, "Split window below")
map("n", "<leader>wv", "<C-W>v", {}, "Split window right")
map("n", "<leader>wd", "<C-W>c", {}, "Close window")
map("n", "<leader>wo", "<C-W>o", {}, "Close other windows")
map("n", "<leader>w=", "<C-W>=", {}, "Equalize windows")
-- =============================================================================
-- Terminal
-- =============================================================================
map({ "n", "t" }, "<M-t>", function()
vim.cmd.split({ "term://" .. vim.env.SHELL })
vim.cmd.resize({ 15 })
end, {}, "Toggle terminal")
map("t", "<M-t>", [[<C-\><C-n>:bd!<CR>]], {}, "Close terminal")
-- =============================================================================
-- Visual mode indent
-- =============================================================================
map("v", "<Tab>", ">gv", { noremap = true, silent = true }, "Indent selection")
map("v", "<S-Tab>", "<gv", { noremap = true, silent = true }, "Unindent selection")
-- =============================================================================
-- Buffer
-- =============================================================================
map("n", "<leader>bn", "<cmd>bnext<cr>", {}, "Next buffer")
map("n", "<leader>bp", "<cmd>bprevious<cr>", {}, "Previous buffer")
map("n", "<leader>bd", "<cmd>bdelete<cr>", {}, "Delete buffer")
map("n", "<leader>bD", "<cmd>bdelete!<cr>", {}, "Delete buffer (force)")
-- =============================================================================
-- Find (Telescope)
-- =============================================================================
local builtin = require("telescope.builtin")
map("n", "<leader>ff", builtin.find_files, {}, "Find files")
map("n", "<leader>fg", builtin.live_grep, {}, "Live grep")
map("n", "<leader>fb", builtin.buffers, {}, "Buffers")
map("n", "<leader>fh", builtin.help_tags, {}, "Help tags")
map("n", "<leader>fr", builtin.oldfiles, {}, "Recent files")
map("n", "<leader>fB", function()
require("telescope").extensions.file_browser.file_browser()
end, {}, "File browser")
-- =============================================================================
-- Git
-- =============================================================================
map("n", "<leader>gg", "<cmd>Neogit<cr>", {}, "Neogit")
map("n", "<leader>gd", "<cmd>DiffviewOpen<cr>", {}, "Diffview open")
map("n", "<leader>gD", "<cmd>DiffviewFileHistory %<cr>", {}, "Diffview file history")
map("n", "<leader>gq", "<cmd>DiffviewClose<cr>", {}, "Diffview close")
-- =============================================================================
-- Search
-- =============================================================================
map("n", "<leader>S", function()
require("spectre").toggle()
end, {}, "Toggle Spectre")
map("n", "<leader>sw", function()
require("spectre").open_visual({ select_word = true })
end, {}, "Search current word")
map("v", "<leader>sw", function()
require("spectre").open_visual()
end, {}, "Search current word")
map("n", "<leader>sf", function()
require("spectre").open_file_search({ select_word = true })
end, {}, "Search in current file")
-- =============================================================================
-- Todo Comments
-- =============================================================================
map("n", "]t", function()
require("todo-comments").jump_next()
end, {}, "Next todo comment")
map("n", "[t", function()
require("todo-comments").jump_prev()
end, {}, "Previous todo comment")
map("n", "<leader>st", "<cmd>TodoTelescope<cr>", {}, "Search todos")
map("n", "<leader>sT", "<cmd>TodoLocList<cr>", {}, "Search todos (LocList)")
-- =============================================================================
-- Test (Neotest)
-- =============================================================================
local neotest = require("neotest")
map("n", "<leader>tf", function()
neotest.run.run(vim.fn.expand("%"))
end, {}, "Run File")
map("n", "<leader>tt", function()
neotest.run.run()
end, {}, "Run Nearest")
map("n", "<leader>tw", function()
neotest.run.run(vim.loop.cwd())
end, {}, "Run Workspace")
map("n", "<leader>tr", function()
neotest.output.open({ short = true, auto_close = true })
end, {}, "Results (short)")
map("n", "<leader>tR", function()
neotest.output.open({ enter = true })
end, {}, "Results (full)")
-- =============================================================================
-- Run / Overseer
-- =============================================================================
map("n", "<leader>te", function()
require("overseer.template.run_watch_provider").pick_executable_and_run()
end, {}, "Run & Watch executable (Telescope)")
map("n", "<leader>to", "<cmd>OverseerToggle<cr>", {}, "Toggle Overseer")
map("n", "<leader>ts", "<cmd>OverseerRun<cr>", {}, "Run Overseer")
-- =============================================================================
-- Debug (nvim-dap)
-- =============================================================================
local dap = require("dap")
local dapui = require("dapui")
local pb = require("persistent-breakpoints.api")
map("n", "<leader>db", pb.toggle_breakpoint, {}, "Toggle breakpoint")
map("n", "<leader>dB", function()
pb.set_conditional_breakpoint()
end, {}, "Conditional breakpoint")
map("n", "<leader>dc", dap.continue, {}, "Continue")
map("n", "<leader>dO", dap.step_over, {}, "Step over")
map("n", "<leader>di", dap.step_into, {}, "Step into")
map("n", "<leader>do", dap.step_out, {}, "Step out")
map("n", "<leader>dr", dap.run_to_cursor, {}, "Run to cursor")
map("n", "<leader>dd", dapui.toggle, {}, "Toggle DAP UI")
map("n", "<leader>dq", function()
dap.terminate()
end, {}, "Terminate debug")
-- =============================================================================
-- LSP (buffer-local via LspAttach)
-- =============================================================================
vim.api.nvim_create_autocmd("LspAttach", {
group = vim.api.nvim_create_augroup("UserLspKeymaps", { clear = true }),
callback = function(ev)
local bufnr = ev.buf
local client = vim.lsp.get_client_by_id(ev.data.client_id)
if not client then
return
end
local opts = { buffer = bufnr, silent = true }
local autocmd = vim.api.nvim_create_autocmd
-- Hover & Signature
if client.server_capabilities.hoverProvider then
map("n", "K", vim.lsp.buf.hover, opts, "Hover")
end
if client.server_capabilities.signatureHelpProvider then
map("n", "<leader>k", vim.lsp.buf.signature_help, opts, "Signature help")
end
-- Goto
if client.server_capabilities.declarationProvider then
map("n", "gD", vim.lsp.buf.declaration, opts, "Go to declaration")
end
if client.server_capabilities.definitionProvider then
map("n", "gd", function()
if utils.has("lspsaga.nvim") then
vim.cmd([[Lspsaga goto_definition]])
else
vim.lsp.buf.definition()
end
end, opts, "Go to definition")
if utils.has("lspsaga.nvim") then
map("n", "<leader>lp", function()
vim.cmd([[Lspsaga peek_definition]])
end, opts, "Peek definition")
end
end
if client.server_capabilities.typeDefinitionProvider then
map("n", "gt", function()
if utils.has("lspsaga.nvim") then
vim.cmd([[Lspsaga goto_type_definition]])
else
vim.lsp.buf.type_definition()
end
end, opts, "Go to type definition")
if utils.has("lspsaga.nvim") then
map("n", "<leader>lP", function()
vim.cmd([[Lspsaga peek_type_definition]])
end, opts, "Peek type definition")
end
end
if client.server_capabilities.implementationProvider then
map("n", "gi", function()
if utils.has("lspsaga.nvim") then
vim.cmd([[Lspsaga finder imp+def]])
else
vim.lsp.buf.implementation()
end
end, opts, "Go to implementation")
end
if client.server_capabilities.referencesProvider then
map("n", "gr", function()
if utils.has("lspsaga.nvim") then
vim.cmd([[Lspsaga finder ref]])
else
vim.lsp.buf.references({})
end
end, opts, "Go to references")
end
-- Document Highlight (references under cursor)
if client.server_capabilities.documentHighlightProvider then
vim.api.nvim_set_hl(0, "LspReferenceRead", { link = "DiffText" })
vim.api.nvim_set_hl(0, "LspReferenceText", { link = "IncSearch" })
vim.api.nvim_set_hl(0, "LspReferenceWrite", { link = "WildMenu" })
local doc_highlight = vim.api.nvim_create_augroup("lsp_document_highlight", { clear = true })
local enable_highlight = function()
autocmd({ "CursorHold", "CursorHoldI" }, {
group = doc_highlight,
buffer = bufnr,
callback = vim.lsp.buf.document_highlight,
})
autocmd("CursorMoved", {
group = doc_highlight,
buffer = bufnr,
callback = vim.lsp.buf.clear_references,
})
end
local disable_highlight = function()
vim.lsp.buf.clear_references()
vim.api.nvim_clear_autocmds({ buffer = bufnr, group = doc_highlight })
end
map("n", "<leader>uh", function()
utils.toggle("highlight", {}, { enable_highlight, disable_highlight })
local fn = vim.b[vim.fn.bufnr()]["highlight"]
if fn then
fn()
end
end, opts, "Toggle document highlight")
end
-- Symbols
if client.server_capabilities.documentSymbolProvider then
map("n", "<leader>ls", vim.lsp.buf.document_symbol, opts, "Document symbols")
end
if client.server_capabilities.workspaceSymbolProvider then
map("n", "<leader>lS", vim.lsp.buf.workspace_symbol, opts, "Workspace symbols")
end
-- Code Action & Rename
if client.server_capabilities.codeActionProvider then
map({ "n", "v" }, "<leader>ca", function()
if utils.has("lspsaga.nvim") then
vim.cmd([[Lspsaga code_action]])
else
vim.lsp.buf.code_action()
end
end, opts, "Code action")
end
if client.server_capabilities.renameProvider then
map("n", "<leader>cr", vim.lsp.buf.rename, opts, "Rename")
if utils.has("lspsaga.nvim") then
map("n", "<leader>cR", function()
vim.cmd([[Lspsaga project_replace]])
end, opts, "Rename workspace")
end
end
-- Calls
if client.server_capabilities.callHierarchyProvider then
map("n", "<leader>ci", function()
if utils.has("lspsaga.nvim") then
vim.cmd([[Lspsaga incoming_calls]])
else
vim.lsp.buf.incoming_calls()
end
end, opts, "Incoming calls")
map("n", "<leader>co", function()
if utils.has("lspsaga.nvim") then
vim.cmd([[Lspsaga outgoing_calls]])
else
vim.lsp.buf.outgoing_calls()
end
end, opts, "Outgoing calls")
end
-- Formatting
if client.server_capabilities.documentFormattingProvider then
map("n", "<leader>cf", function()
vim.lsp.buf.format({ async = true })
end, opts, "Format buffer")
end
if client.server_capabilities.documentRangeFormattingProvider then
map("v", "<leader>cf", function()
vim.lsp.buf.format({ async = true })
end, opts, "Format selection")
end
-- Diagnostics
map("n", "[d", function()
if utils.has("lspsaga.nvim") then
vim.cmd([[Lspsaga diagnostic_jump_prev]])
else
vim.diagnostic.jump({ count = -1, float = true })
end
end, opts, "Previous diagnostic")
map("n", "]d", function()
if utils.has("lspsaga.nvim") then
vim.cmd([[Lspsaga diagnostic_jump_next]])
else
vim.diagnostic.jump({ count = 1, float = true })
end
end, opts, "Next diagnostic")
map("n", "<leader>lf", vim.diagnostic.open_float, opts, "Line diagnostic")
map("n", "<leader>lq", vim.diagnostic.setloclist, opts, "Set loclist")
-- Lspsaga extras
if utils.has("lspsaga.nvim") then
map("n", "<leader>lw", function()
vim.cmd([[Lspsaga show_workspace_diagnostics]])
end, opts, "Workspace diagnostics")
map("n", "<leader>lb", function()
vim.cmd([[Lspsaga show_buf_diagnostics]])
end, opts, "Buffer diagnostics")
map("n", "<leader>lo", function()
vim.cmd([[Lspsaga outline]])
end, opts, "Outline")
end
-- Workspace folders
map("n", "<leader>wf", function()
vim.print(vim.lsp.buf.list_workspace_folders())
end, opts, "List workspace folders")
map("n", "<leader>wr", function()
local clients = vim.lsp.get_clients()
if #clients > 0 then
local lsp = vim.lsp.status()
if lsp ~= "" then
print(lsp)
return
end
end
print("No LSP status available")
end, opts, "LSP ready check")
end,
})
-- =============================================================================
-- UI Toggles (global)
-- =============================================================================
map("n", "<leader>us", function()
utils.toggle("enable_semantic_tokens", { global = true }, nil)
local enabled = vim.g["enable_semantic_tokens"]
for _, client in ipairs(vim.lsp.get_clients()) do
if client.server_capabilities.semanticTokensProvider then
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
if vim.api.nvim_buf_is_loaded(buf) then
if enabled then
vim.lsp.semantic_tokens.start(buf, client.id)
else
vim.lsp.semantic_tokens.stop(buf, client.id)
end
end
end
end
end
end, {}, "Toggle semantic tokens")
vim.api.nvim_create_autocmd("BufWritePre", {
group = vim.api.nvim_create_augroup("LspFormat", { clear = true }),
callback = function(args)
if vim.g["autoformat"] then
vim.lsp.buf.format({ async = true, bufnr = args.buf })
end
end,
})
-- =============================================================================
-- LSP Document Highlight jumps (lsp-document-highlight.nvim)
-- =============================================================================
map("n", "[[", function()
require("lsp-document-highlight").jump(-vim.v.count1, true)
end, {}, "Previous reference")
map("n", "]]", function()
require("lsp-document-highlight").jump(vim.v.count1, true)
end, {}, "Next reference")